Question

I'm writing a simple socket server/client. Here is the server part:

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>

using namespace std;

int main() {
int listenfd;
int connfd;
sockaddr_in servaddr;
char buf[100];
time_t ticks;

if(listenfd = socket(AF_INET,SOCK_STREAM,0) < 0)
    cout << "listenfd" << endl;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(10000);

bind(listenfd,(const struct sockaddr*)&servaddr,sizeof(servaddr));

listen(listenfd,5);

for(;;) {
    connfd = accept(listenfd,(struct sockaddr *)NULL,NULL);
    //cout << "accept link" << endl;
    ticks = time(NULL);
    snprintf(buf,sizeof(buf),"%.24s\r\n",ctime(&ticks));
    //cout << buf << endl;
    write(connfd,buf,strlen(buf));
    close(connfd);
}
}

And here is the client part:

#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
using namespace std;
#define MAX 100
int main(int argc,char **argv) {
    int socketfd;
    int n;
    char buf[MAX+1];
    sockaddr_in servaddr;

    if(argc !=2 )
        cout << "stdin error " << endl;

    if((socketfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
         cout << " socekt error " << endl;
     bzero(&servaddr,sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = htons(10000);
     if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0)
         cout << "inet_pton error" << endl;
     cout << "prepare linking" << endl;
     if(connect(socketfd,(const struct sockaddr*)&servaddr,sizeof(struct sockaddr)) < 0)
     {
         cout << " connet error" << endl;
         cout << strerror(errno);
     }
     while((n = read(socketfd,buf,MAX)) >0) {
         buf[n] = 0;
         if(fputs(buf,stdout) == EOF)
             cout << "cout error" << endl;
     }
     if(n < 0)
          cout << "read error" << endl;
     exit(0);
}

I start the server first and run the client like: ./client 127.0.0.1, but connection failed with errno 111.

I'm using Ubuntu 12.04 system.

Was it helpful?

Solution

In your server code, you have:

if(listenfd = socket(AF_INET,SOCK_STREAM,0) < 0)

The problem is to do with the precedence of C operators. Because the < comparison has a higher precedence than assignment, your statement will set listenfd to the boolean value of the x < y bit meaning, because the socket will most likely succeed, it will most likely be set to 0 (false), hence standard input (file descriptor 0).

If you must use the C shortcuts (I know they're handy but sometimes they're less readable than the alternatives), you should use the variant:

if ((listenfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)

as you have already done in your client code:

if ((socketfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top