Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

I need help


/* server.c */

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 30000;
int *buffer = malloc(bufsize);
struct sockaddr_in address;
time_t s,f;
double t1,t2,temp;
struct timeval tp;



printf("x1B[2J");
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created
");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("Binding Socket
");
listen(create_socket,6);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
if (new_socket > 0){
printf("The Client %s is connected...
",inet_ntoa(address.sin_addr));
for(cont=1;cont<5000;cont++)
printf("x7");
}

int i,g=0,j,u,t=0,y,d=0,q,k;


for(i=0;i<bufsize;i++)
buffer[i]=5;

send(create_socket,buffer,bufsize,0);

close(new_socket);
close(create_socket);
}





/* client.c */

#include<sys/socket.h>
#include
#include
#include
#include
#include
#define RAW 50
#define COL 50
main(int argc,char *argv[])
{
int create_socket;
int bufsize = 15000;
int *buffer = malloc(bufsize);
struct sockaddr_in address;
// BUF_MEM_grow(int *buffer,int 2000 );
printf("x1B[2J");
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The Socket was created
");
address.sin_family = AF_INET;
address.sin_port = htons(15000);
inet_pton(AF_INET,argv[1],&address.sin_addr);

if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("The connection was accepted with the server %s...
",inet_ntoa(address.sin_addr));
int x=0,d,a[RAW][COL],b[RAW][COL],c[RAW][COL],i=0,j,t=0,k,q,w;




recv(create_socket,buffer,bufsize,0);

for(i=0;i<bufsize;i++)
printf("buffer[%d]=%d
",i,buffer[i]);

close(create_socket);
}

the above tcpserver and tcpclient,I recieve all the numbers zeroes or most of them.
I never understood that .
any help will be appresiated.
abdobl


Comments

  • johnsonmejohnsonme Posts: 57Member
    /*Try this.... server.c*/
    #include
    #include
    #include
    #include
    #include
    #include
    /* Read text from the socket and print it out. Continue until the
    socket closes. Return nonzero if the client sent a quit
    message, zero otherwise. */
    int server (int client_socket)
    {
    while (1) {
    int length;
    char* text;
    /* First, read the length of the text message from the socket. If
    read returns zero, the client closed the connection. */
    if (read (client_socket, &length, sizeof (length)) == 0)
    return 0;
    /* Allocate a buffer to hold the text. */
    text = (char*) malloc (length);
    /* Read the text itself, and print it. */
    read (client_socket, text, length);
    printf (%s
    , text);
    /* Free the buffer. */
    free (text);
    /* If the client sent the message quit, were all done. */
    if (!strcmp (text, quit))
    return 1;
    }
    }
    int main (int argc, char* const argv[])
    {
    const char* const socket_name = argv[1];
    int socket_fd;
    struct sockaddr_un name;
    int client_sent_quit_message;
    /* Create the socket. */
    socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
    /* Indicate that this is a server. */
    name.sun_family = AF_LOCAL;
    strcpy (name.sun_path, socket_name);
    bind (socket_fd, &name, SUN_LEN (&name));
    /* Listen for connections. */
    listen (socket_fd, 5);
    /* Repeatedly accept connections, spinning off one server() to deal
    with each client. Continue until a client sends a quit message. */
    do {
    struct sockaddr_un client_name;
    socklen_t client_name_len;
    int client_socket_fd;
    /* Accept a connection. */
    client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
    /* Handle the connection. */
    client_sent_quit_message = server (client_socket_fd);
    /* Close our end of the connection. */
    close (client_socket_fd);
    }
    while (!client_sent_quit_message);
    /* Remove the socket file. */
    close (socket_fd);
    unlink (socket_name);
    return 0;
    }

    /*Client.c*/
    #include
    #include
    #include
    #include
    #include
    /* Write TEXT to the socket given by file descriptor SOCKET_FD. */
    void write_text (int socket_fd, const char* text)
    {
    /* Write the number of bytes in the string, including
    NUL-termination. */
    int length = strlen (text) + 1;
    write (socket_fd, &length, sizeof (length));
    /* Write the string. */
    write (socket_fd, text, length);
    }
    int main (int argc, char* const argv[])
    {
    const char* const socket_name = argv[1];
    const char* const message = argv[2];
    int socket_fd;
    struct sockaddr_un name;
    /* Create the socket. */
    socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
    /* Store the servers name in the socket address. */
    name.sun_family = AF_LOCAL;
    strcpy (name.sun_path, socket_name);
    /* Connect the socket. */
    connect (socket_fd, &name, SUN_LEN (&name));
    /* Write the text on the command line to the socket. */
    write_text (socket_fd, message);
    close (socket_fd);
    return 0;
    }
  • abdoblabdobl Posts: 21Member
    : /*Try this.... server.c*/
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : /* Read text from the socket and print it out. Continue until the
    : socket closes. Return nonzero if the client sent a quit
    : message, zero otherwise. */
    : int server (int client_socket)
    : {
    : while (1) {
    : int length;
    : char* text;
    : /* First, read the length of the text message from the socket. If
    : read returns zero, the client closed the connection. */
    : if (read (client_socket, &length, sizeof (length)) == 0)
    : return 0;
    : /* Allocate a buffer to hold the text. */
    : text = (char*) malloc (length);
    : /* Read the text itself, and print it. */
    : read (client_socket, text, length);
    : printf (%s
    , text);
    : /* Free the buffer. */
    : free (text);
    : /* If the client sent the message quit, were all done. */
    : if (!strcmp (text, quit))
    : return 1;
    : }
    : }
    : int main (int argc, char* const argv[])
    : {
    : const char* const socket_name = argv[1];
    : int socket_fd;
    : struct sockaddr_un name;
    : int client_sent_quit_message;
    : /* Create the socket. */
    : socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
    : /* Indicate that this is a server. */
    : name.sun_family = AF_LOCAL;
    : strcpy (name.sun_path, socket_name);
    : bind (socket_fd, &name, SUN_LEN (&name));
    : /* Listen for connections. */
    : listen (socket_fd, 5);
    : /* Repeatedly accept connections, spinning off one server() to deal
    : with each client. Continue until a client sends a quit message. */
    : do {
    : struct sockaddr_un client_name;
    : socklen_t client_name_len;
    : int client_socket_fd;
    : /* Accept a connection. */
    : client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
    : /* Handle the connection. */
    : client_sent_quit_message = server (client_socket_fd);
    : /* Close our end of the connection. */
    : close (client_socket_fd);
    : }
    : while (!client_sent_quit_message);
    : /* Remove the socket file. */
    : close (socket_fd);
    : unlink (socket_name);
    : return 0;
    : }
    :
    : /*Client.c*/
    : #include
    : #include
    : #include
    : #include
    : #include
    : /* Write TEXT to the socket given by file descriptor SOCKET_FD. */
    : void write_text (int socket_fd, const char* text)
    : {
    : /* Write the number of bytes in the string, including
    : NUL-termination. */
    : int length = strlen (text) + 1;
    : write (socket_fd, &length, sizeof (length));
    : /* Write the string. */
    : write (socket_fd, text, length);
    : }
    : int main (int argc, char* const argv[])
    : {
    : const char* const socket_name = argv[1];
    : const char* const message = argv[2];
    : int socket_fd;
    : struct sockaddr_un name;
    : /* Create the socket. */
    : socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
    : /* Store the servers name in the socket address. */
    : name.sun_family = AF_LOCAL;
    : strcpy (name.sun_path, socket_name);
    : /* Connect the socket. */
    : connect (socket_fd, &name, SUN_LEN (&name));
    : /* Write the text on the command line to the socket. */
    : write_text (socket_fd, message);
    : close (socket_fd);
    : return 0;
    : }
    :
    Hi Johns...
    Thnks , but the example you have sent is in unix domain,but I am using the internet domain. and this is not going to help me.
    with my regards, abdobl.

  • johnsonmejohnsonme Posts: 57Member
    Oops..... I didn't noticed it... Now try this...

    /*This program takes the hostname of the Web server on the command line (not a URLthat is, without the http://). It calls gethostbyname to translate the hostname into a numerical IP address and then connects a stream (TCP) socket to port 80 on that host.Web servers speak the Hypertext Transport Protocol (HTTP), so the program issues the HTTP GET command and the server responds by sending the text of the
    home page.*/

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    /* Print the contents of the home page for the servers socket.
    Return an indication of success. */
    void get_home_page (int socket_fd)
    {
    char buffer[10000];
    ssize_t number_characters_read;
    /* Send the HTTP GET command for the home page. */
    sprintf (buffer, GET /
    );
    write (socket_fd, buffer, strlen (buffer));
    /* Read from the socket. The call to read may not
    return all the data at one time, so keep
    trying until we run out. */
    while (1) {
    number_characters_read = read (socket_fd, buffer, 10000);
    if (number_characters_read == 0)
    return;
    /* Write the data to standard output. */
    fwrite (buffer, sizeof (char), number_characters_read, stdout);
    }
    }
    int main (int argc, char* const argv[])
    {
    int socket_fd;
    struct sockaddr_in name;
    struct hostent* hostinfo;
    /* Create the socket. */
    socket_fd = socket (PF_INET, SOCK_STREAM, 0);
    /* Store the servers name in the socket address. */
    name.sin_family = AF_INET;
    /* Convert from strings to numbers. */
    hostinfo = gethostbyname (argv[1]);
    if (hostinfo == NULL)
    return 1;
    else
    name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
    /* Web servers use port 80. */
    name.sin_port = htons (80);
    /* Connect to the Web server */
    if (connect (socket_fd, &name, sizeof (struct sockaddr_in)) == -1) {
    perror (connect);
    return 1;
    }
    /* Retrieve the servers home page. */
    get_home_page (socket_fd);
    return 0;
    }
  • abdoblabdobl Posts: 21Member
    : Oops..... I didn't noticed it... Now try this...
    :
    : /*This program takes the hostname of the Web server on the command line (not a URLthat is, without the http://). It calls gethostbyname to translate the hostname into a numerical IP address and then connects a stream (TCP) socket to port 80 on that host.Web servers speak the Hypertext Transport Protocol (HTTP), so the program issues the HTTP GET command and the server responds by sending the text of the
    : home page.*/
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : /* Print the contents of the home page for the servers socket.
    : Return an indication of success. */
    : void get_home_page (int socket_fd)
    : {
    : char buffer[10000];
    : ssize_t number_characters_read;
    : /* Send the HTTP GET command for the home page. */
    : sprintf (buffer, GET /
    );
    : write (socket_fd, buffer, strlen (buffer));
    : /* Read from the socket. The call to read may not
    : return all the data at one time, so keep
    : trying until we run out. */
    : while (1) {
    : number_characters_read = read (socket_fd, buffer, 10000);
    : if (number_characters_read == 0)
    : return;
    : /* Write the data to standard output. */
    : fwrite (buffer, sizeof (char), number_characters_read, stdout);
    : }
    : }
    : int main (int argc, char* const argv[])
    : {
    : int socket_fd;
    : struct sockaddr_in name;
    : struct hostent* hostinfo;
    : /* Create the socket. */
    : socket_fd = socket (PF_INET, SOCK_STREAM, 0);
    : /* Store the servers name in the socket address. */
    : name.sin_family = AF_INET;
    : /* Convert from strings to numbers. */
    : hostinfo = gethostbyname (argv[1]);
    : if (hostinfo == NULL)
    : return 1;
    : else
    : name.sin_addr = *((struct in_addr *) hostinfo->h_addr);
    : /* Web servers use port 80. */
    : name.sin_port = htons (80);
    : /* Connect to the Web server */
    : if (connect (socket_fd, &name, sizeof (struct sockaddr_in)) == -1) {
    : perror (connect);
    : return 1;
    : }
    : /* Retrieve the servers home page. */
    : get_home_page (socket_fd);
    : return 0;
    : }
    :
    Hi
    you did not understand me Jhon, I have a tcoserver and tcpclient programms, my application is "MATRIX MULTIPLICATION",when I send teh data to the client, it recieves the first 12287 number as I sent it,but after that the client recieves zeroes "0".


    NOTE

    In the first two times I run the programms,it's successed.after that once successed and once not.I never understood that.

    this is the part of boat programms tcpserver and tcpclient :

    tcpserver:

    for(i=0;i<RAW;i++)
    for(j=0;j<COL;j++)
    c[i][j]=0;


    printf("the result is as follows :

    ");
    //send(new_socket,buffer,bufsize,0);

    for(i=(RAW/2);i<RAW;i++)
    for(j=0;j<COL;j++)
    { //printf("the result is as follows :

    ");
    while(d<(RAW/2))
    { //printf("the result is as follows 10:

    ");
    for(y=0;y<COL;y++)
    {
    buffer[t]=a[d][y];
    //printf("buffer[%d]=%d ",t,buffer[t]);
    t++;
    }

    d++;
    }
    if(d==(RAW/2))
    {
    for(q=0;q<RAW;q++)
    for(y=0;y<COL;y++)
    {
    buffer[t]=b[q][y];
    //printf("buffer[%d]=%d ",t,buffer[t]);

    t++;
    }
    //asd=gettimeofday(&tp, NULL);
    //t1=(double)tp.tv_sec+(1.e-6)*tp.tv_usec;
    //time(&s);
    // printf("the result is as follows :

    ");
    send(new_socket,buffer,bufsize,0);
    d++;
    }

    for(k=0;k<RAW;k++)
    c[i][j]+=a[i][k]*b[k][j];

    }

    t=0;
    recv(new_socket,buffer,bufsize,0);
    for(i=0;i<(RAW/2);i++)
    for( j=0;j<COL;j++)
    {
    c[i][j]=buffer[t];
    t++;
    }
    for(i=0;i<RAW;i++)
    for(j=0;j<COL;j++)
    printf("c[%d][%d]=%d ",i,j,c[i][j]);


    //time(&f);
    //asd=gettimeofday(&tp, NULL);
    //t2=(double)tp.tv_sec+(1.e-6)*tp.tv_usec;
    //temp=t2-t1;

    /*printf("

    %s
    ", ctime(&s));
    printf("%s
    ", ctime(&f));
    printf("%f in sec , %f in sec:usec
    ", difftime(f, s),temp);
    */
    free(buffer);
    close(new_socket);
    close(create_socket);


    tcpclient:

    for(j=0;j<COL;j++)
    {
    a[i][j]=0;
    b[i][j]=-1;
    c[i][j]=0;
    }


    //printf("the result is as follows 1:

    ");
    recv(create_socket,buffer,bufsize,0);


    for(i=0;i<(RAW/2);i++)
    for(j=0;j<COL;j++)
    {
    // printf("buffer[%d]=%d ",t,buffer[t]);
    a[i][j]=buffer[t];
    t++;
    }

    for(i=0;i<RAW;i++)
    for(j=0;j<COL;j++)
    {
    b[i][j]=buffer[t];
    printf("b[%d][%d]=%d ",i,j,b[i][j]);

    t++;
    }

    t=0;
    for(i=0;i<(RAW/2);i++)
    for(j=0;j<COL;j++)
    {
    for(k=0;k<RAW;k++)
    {
    c[i][j]+=a[i][k]*b[k][j];
    }
    //printf("c[%d][%d]=%d ",i,j,c[i][j]);
    buffer[t]=c[i][j];
    t++;
    }

    send(create_socket,buffer,bufsize,0);

    printf("
    The message has been sent to the server.
    ");

    //}while (strcmp(buffer,"/q"));

    free(buffer);
    close(create_socket);

Sign In or Register to comment.