Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Fail to bind the socket

Fail to bind the socket

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadminquestioncareer
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitranjanmishra
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • A amitranjanmishra

      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

      D Offline
      D Offline
      dharani
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • D dharani

        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

        A Offline
        A Offline
        amitranjanmishra
        wrote on last edited by
        #3

        :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

        M 1 Reply Last reply
        0
        • A amitranjanmishra

          :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

          M Offline
          M Offline
          markkuk
          wrote on last edited by
          #4

          You're ignoring the length of received data (returned by recv()), you're not checking if all data is read in a single call to recv() and you're assuming that the data placed in buf by recv() is zero-terminated.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups