C socket: Connection refused[Errno 111] -


i'm writing simple socket server/client. here 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 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 server first , run client like: ./client 127.0.0.1, connection failed errno 111.

i'm using ubuntu 12.04 system.

in server code, have:

if(listenfd = socket(af_inet,sock_stream,0) < 0) 

the problem precedence of c operators. because < comparison has higher precedence assignment, statement set listenfd boolean value of x < y bit meaning, because socket succeed, set 0 (false), hence standard input (file descriptor 0).

if must use c shortcuts (i know they're handy they're less readable alternatives), should use variant:

if ((listenfd = socket (af_inet, sock_stream, 0)) < 0) 

as have done in client code:

if ((socketfd = socket (af_inet, sock_stream, 0)) < 0) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -