CoderInNetwork
CoderInNetwork

Reputation: 3083

simple client sever program in c++ not working

I am trying to create a simple client server program in c++ .The server and client execute until termination but The data doesn't transfer from server to client. My server code is :

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>

int main(void) {



    int sid=socket(AF_INET,SOCK_STREAM,0); 

    int  clilen;

    int clisocket;

    char buffer[256];

    int portnumber=atoi("8888");

    struct sockaddr_in serverinfo,cliinfo;
    memset(&serverinfo, '0', sizeof(serverinfo));
    memset(&cliinfo, '0', sizeof(cliinfo));

    serverinfo.sin_family=AF_INET;
    serverinfo.sin_port=htons(portnumber);
    serverinfo.sin_addr.s_addr=INADDR_ANY;


    bind(sid,(struct sockaddr *)&serverinfo,sizeof(serverinfo));

    listen(sid,1000);


    clilen=sizeof(clilen);


    clisocket=accept(sid,(struct sockaddr*)&cliinfo,(socklen_t*)clilen);

    bzero(buffer,256);


    printf("before server \n");
    write(clisocket,"hello",5);
    printf("after server \n");


    close(clisocket);

    close(sid);

    return 0;
}

and my client code is :

#include <stdio.h>
#include <stdlib.h>


#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>

int main(void) {

    char buffer[256];
    bzero(buffer,256);

    int cid=socket(AF_INET,SOCK_STREAM,0);

    int port=atoi("8888");

    struct sockaddr_in serverinfo;

    memset(&serverinfo, '0', sizeof(serverinfo));

    struct hostent* host;

    memset(&host, '0', sizeof(host));

    host=gethostbyname("localhost");

    serverinfo.sin_family=host->h_addrtype;
    memcpy((char *) &serverinfo.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
    serverinfo.sin_port=htons(port);


    int statusconnect=connect(cid,(sockaddr*)&serverinfo,sizeof(serverinfo));


    printf("before read client \n");
    read(cid,buffer,255);
    printf("after client read");
    printf(buffer); //not print this!!!

    printf(buffer);
    close(cid);

    return 0;
}

I would appreciate for any bit of help.

Upvotes: 0

Views: 315

Answers (1)

Joni
Joni

Reputation: 111259

You are lacking all error handling. When something goes wrong you have no idea why or how to fix it. Always check for error conditions.

In this particular case the problem seems to be with the accept call in the server: you are passing the socket length as the last parameter, when you should pass a pointer to the socket length. Also the socket length is being set to the size of an int initially. The code should be:

clilen = sizeof(struct sockaddr_in);
clisocket = accept(sid,(struct sockaddr*)&cliinfo,(socklen_t*)&clilen);

A more subtle error is the fact that the server closes the socket with no guarantees of whether any of the data was actually sent. Fixing this is more complicated though.

Upvotes: 3

Related Questions