Fail to bind the socket
-
I have created 2 sockets . I fr my server application <192.168.2.20> port 80 I fr my client. IP Address assigned: 192.168.0.51 port 80 Both The applications reside on the same machine. When I execute the server it runs fine But when I run the Client , it shows error code : 10047 Yeah, After surfing the net I got to know that Protocol family not supported ... But the problem iz........ Unless the Connection is not established between the two applications how come it give such error. What Can there be other possibilities? The portion causing error is Enclosed. :rose:With hope to recv earliest suggestions i Thnx in Advance.:rose:
int main() { :: :: sock_creation(); :: :: } int sock_creation() { int rc,k; struct sockaddr_in addr; char *servername; struct WSAData wsaData; printf("\n Count 10 n Resume ...\n"); int wsaret=WSAStartup(0x101,&wsaData); if(wsaret<0) { printf("Unable to Initialize Windows Socket Library"); k=getch(); exit(1); } else { printf("\n Initialized Windows Socket Library"); } conn=socket(AF_INET,SOCK_STREAM,0); servAddr.sin_addr.s_addr=inet_addr("192.168.0.51"); servAddr.sin_port=htons(u_short(8080)); if(conn==INVALID_SOCKET) { printf("\n But Unable to Initialize Windows Socket"); k=getch(); exit(1); } else { printf(" \n Initialized Windows Socket As Well....."); } if(bind(conn,(sockaddr*)&servAddr,sizeof(servAddr))!=0) { printf("\n But Failed to bind !!! Error Code %d\n ",WSAGetLastError()); k=getch(); exit(1); } servAddrSrvr.sin_addr.s_addr=inet_addr("192.168.0.20"); servAddrSrvr.sin_family=AF_INET; servAddrSrvr.sin_port=htons(80); // connect(conn,(struct sockaddr*)&servAddrSrvr,sizeof(servAddrSrvr)); return 0; }
amit mishra -
I have created 2 sockets . I fr my server application <192.168.2.20> port 80 I fr my client. IP Address assigned: 192.168.0.51 port 80 Both The applications reside on the same machine. When I execute the server it runs fine But when I run the Client , it shows error code : 10047 Yeah, After surfing the net I got to know that Protocol family not supported ... But the problem iz........ Unless the Connection is not established between the two applications how come it give such error. What Can there be other possibilities? The portion causing error is Enclosed. :rose:With hope to recv earliest suggestions i Thnx in Advance.:rose:
int main() { :: :: sock_creation(); :: :: } int sock_creation() { int rc,k; struct sockaddr_in addr; char *servername; struct WSAData wsaData; printf("\n Count 10 n Resume ...\n"); int wsaret=WSAStartup(0x101,&wsaData); if(wsaret<0) { printf("Unable to Initialize Windows Socket Library"); k=getch(); exit(1); } else { printf("\n Initialized Windows Socket Library"); } conn=socket(AF_INET,SOCK_STREAM,0); servAddr.sin_addr.s_addr=inet_addr("192.168.0.51"); servAddr.sin_port=htons(u_short(8080)); if(conn==INVALID_SOCKET) { printf("\n But Unable to Initialize Windows Socket"); k=getch(); exit(1); } else { printf(" \n Initialized Windows Socket As Well....."); } if(bind(conn,(sockaddr*)&servAddr,sizeof(servAddr))!=0) { printf("\n But Failed to bind !!! Error Code %d\n ",WSAGetLastError()); k=getch(); exit(1); } servAddrSrvr.sin_addr.s_addr=inet_addr("192.168.0.20"); servAddrSrvr.sin_family=AF_INET; servAddrSrvr.sin_port=htons(80); // connect(conn,(struct sockaddr*)&servAddrSrvr,sizeof(servAddrSrvr)); return 0; }
amit mishraSeems you are trying to bind the client socket also at port 80 which is not necessary .You can not bind two sockets on a same port ( exceptions are there but you wont need it ) So do this way ...Create a server socket , bind it to port 80 of you IP and make it listen for incomin connections . Next create a client socket . *Dont* bind it toany port (or bind it to non-80 port ) and connect toserver at 80 ... Dharani Babu S redindian
-
Seems you are trying to bind the client socket also at port 80 which is not necessary .You can not bind two sockets on a same port ( exceptions are there but you wont need it ) So do this way ...Create a server socket , bind it to port 80 of you IP and make it listen for incomin connections . Next create a client socket . *Dont* bind it toany port (or bind it to non-80 port ) and connect toserver at 80 ... Dharani Babu S redindian
:rose: Thnx fr guiding me :rose: . This being my fst attempt in socket prg. Yeah i cud move ahead all bcoz of u , but caught again midway. In my server program when i use recv() it gives the failed to bind and failed to listen errors. When i m trying to print the acceptance after the accept function it shows the connection. That On other side When i use recv fn in Client application some garbage is printed in . I use it like
{ char buff[512]; // Connection with server servAddrSrvr.sin_addr.s_addr=inet_addr("192.168.0.20"); servAddrSrvr.sin_family=AF_INET; servAddrSrvr.sin_port=htons(80); connect(conn,(struct sockaddr*)&servAddrSrvr,sizeof(servAddrSrvr)); recv(conn,buff,sizeof(buff),0); printf("%s\n",buff); }
Can u say me where i went wrong. amit mishra -
:rose: Thnx fr guiding me :rose: . This being my fst attempt in socket prg. Yeah i cud move ahead all bcoz of u , but caught again midway. In my server program when i use recv() it gives the failed to bind and failed to listen errors. When i m trying to print the acceptance after the accept function it shows the connection. That On other side When i use recv fn in Client application some garbage is printed in . I use it like
{ char buff[512]; // Connection with server servAddrSrvr.sin_addr.s_addr=inet_addr("192.168.0.20"); servAddrSrvr.sin_family=AF_INET; servAddrSrvr.sin_port=htons(80); connect(conn,(struct sockaddr*)&servAddrSrvr,sizeof(servAddrSrvr)); recv(conn,buff,sizeof(buff),0); printf("%s\n",buff); }
Can u say me where i went wrong. amit mishra