udp connection problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saee
    New Member
    • Feb 2009
    • 24

    #1

    udp connection problem

    hello group,
    i'm trying to send a socket over udp. i'm doing it on same machine with loopback address. i wrote a server code n a client code. when i run them both, there should be a connection when i send text from client to server. client window shows sent complete n server window says waiting for connection that means recvfrom() is hanged or blocked. it does not go beyond recvfrom(). i tried connect() also from client. no luck.
    my pseudo code is,

    //client.c
    while(1){
    create server socket
    create client socket
    s = socket(AF_INET, SOCK_DGRAM, 0);
    bind() //client socket
    sendto(s,buf,10 ,0,server,sizeo f());
    }


    //server.c

    creat server socket
    s= socket()
    bind(server)
    while(1){
    "waiting for connection"
    recvfrom(s,buf, 10,0,client,len gth);
    }

    please tell wer am i doing wrong.
    n one more,
    i have such a code for TCp also. it works fine with loopback addr. two windows on same machine talk to each other. but it does not work with network addr. like if i replace 127.0.0.1 with 192.168.1.85 it gives error.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    not sure why you have bind() in the client
    Code:
    s = socket(AF_INET, SOCK_DGRAM, 0);
    bind() //client socket
    sendto(s,buf,10,0,server,sizeof());
    unless you are going to send datagrams both ways only the receiver uses bind()

    for examples of TCP and UCP clients and servers see

    Comment

    • saee
      New Member
      • Feb 2009
      • 24

      #3
      i tried without bind() also. it does not work..

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        could you post your code?

        Comment

        • saee
          New Member
          • Feb 2009
          • 24

          #5
          sure. thanks for ur reply.

          //cleint.cpp
          #define TAR_IPAddress "192.128.1. 85"

          int main()
          {
          WORD wVersionRequest ed;
          WSADATA wsaData;
          SOCKADDR_IN server, client; //Socket address information
          SOCKET s;
          struct hostent *hp;
          int err, b;
          int bytesSent, bytesRecv;
          char buf[100] = "hi hello", k[2], recvbuf[100] ;
          char host_name[156]; //host name of this machine
          int addr_len = sizeof(struct sockaddr);



          //--- INITIALIZATION -----------------------------------
          wVersionRequest ed = MAKEWORD( 1, 1 );
          err = WSAStartup( wVersionRequest ed, &wsaData );

          if ( err != 0 ) {
          printf("WSAStar tup error %ld", WSAGetLastError ());
          WSACleanup();
          return false;
          }
          //---------------------------------------------------------


          //---- creat a server struct.--------
          server.sin_fami ly = AF_INET; // address family Internet
          server.sin_port = htons (SERVER_PORT); //Port to connect on
          server.sin_addr .s_addr = inet_addr (IPAddress); //Target IP

          //--------------------------------------------------------

          //---- creat a client struct---------------------------------
          client.sin_fami ly = AF_INET; // address family Internet
          client.sin_port = htons (0); //Port to connect on
          client.sin_addr .s_addr = inet_addr(IPAdd ress); //host IP
          //--------------------------------------------------------


          while(1) {
          // ---- create SOCKET--------------------------------------
          s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
          if (s == INVALID_SOCKET)
          {
          printf("socket error %ld" , WSAGetLastError () );
          _getch();
          WSACleanup();
          return false; //Couldn't create the socket
          }
          //---------------------------------------------------------

          //---- SEND bytes -------------------------------------------

          printf("trans buf:('q' to quit)\n");
          gets(buf);
          if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
          break;
          bytesSent = sendto (s, buf, 10, 0, (struct sockaddr*) &server, sizeof(sockaddr ));
          if(bytesSent < 0 ){
          printf("sendto fails with : %ld\n ", WSAGetLastError ());
          // _getch();
          WSACleanup();
          }
          else
          printf("bytes sent : %d\n" ,bytesSent );
          //-------------------------------------------------------------------------


          // WSACleanup();
          }
          return 0;
          }

          //server.cpp

          #define SERVER_PORT 1167
          #define BUF_SIZE 4096 // block transfer size
          #define QUEUE_SIZE 10
          #define IPAddress "127.0.0.1" // Local to the system - Loop back address


          int main()
          {
          int b, on = 1;
          char recvbuf[10] = { }, buf[10];
          SOCKET s;
          struct sockaddr_in server, client; // holds IP address
          WORD wVersionRequest ed;
          WSADATA wsaData;
          int err;
          int bytesRecv, bytesSent;
          int addr_len = sizeof(struct sockaddr);


          //--- INITIALIZATION -----------------------------------
          wVersionRequest ed = MAKEWORD( 1, 1 );
          err = WSAStartup( wVersionRequest ed, &wsaData );

          if ( err != 0 ) {
          printf("WSAStar tup error %ld", WSAGetLastError ());
          WSACleanup();
          return false;
          }
          //---------------------------------------------------------

          //------------ server socket ----------------------------
          server.sin_addr .s_addr = inet_addr (IPAddress); //Target IP
          server.sin_fami ly = AF_INET; // address family Internet
          server.sin_port = htons (SERVER_PORT); //Port to connect on
          //-----------------------------------------------------------------

          while (1) {

          // ---- create SOCKET--------------------------------------
          s = socket(AF_INET, SOCK_DGRAM, 0); //0 specifies to use appropriate protocol according to request
          if (s < 0) {
          printf("socket error %ld",WSAGetLast Error() );
          WSACleanup();
          return false;
          }
          //--------------------------------------------------------


          //---- BIND socket ----------------------------------------
          b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr ));
          if (b == -1) {
          printf("bind error\n");
          printf("socket error %ld",WSAGetLast Error() );
          WSACleanup();
          _getch();
          return false;
          }

          //----------------------------------------------------------

          printf("waiting for a connection\n");

          //-----------receive bytes -------------------------------------------------------------

          bytesRecv = recvfrom(s, recvbuf, 10, 0, (struct sockaddr * )&client, &addr_len );
          if(bytesRecv < 0){
          printf("recvfro m error : %ld", WSAGetLastError ());
          WSACleanup();
          }

          printf("connect ion ");
          if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
          printf( "Closed.\n" );
          WSACleanup();
          }
          else printf("accepte d\n");
          printf("receive d from: %s %d\n ", inet_ntoa(clien t.sin_addr), ntohs(client.si n_port));

          printf( " Bytes Recv: %s \n ", recvbuf );

          //-------------------------------------------------------------


          }
          WSACleanup();

          return 0;
          }

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            your server attempted to bind after receiving a message so I modified the server slightly
            Code:
            #include <iostream>
            #include <winsock2.h>
            
            
            using namespace std;
            //server.cpp
            
            #define SERVER_PORT 1167
            #define BUF_SIZE 4096 // block transfer size
            #define QUEUE_SIZE 10
            #define IPAddress "127.0.0.1" // Local to the system - Loop back address
            
            
            int main()
            {
            int b, on = 1;
            char recvbuf[10] = { }, buf[10];
            SOCKET s;
            struct sockaddr_in server, client; // holds IP address
            WORD wVersionRequested;
            WSADATA wsaData;
            int err;
            int bytesRecv, bytesSent;
            int addr_len = sizeof(struct sockaddr);
            
            
            //--- INITIALIZATION -----------------------------------
            wVersionRequested = MAKEWORD( 1, 1 );
            err = WSAStartup( wVersionRequested, &wsaData );
            
            if ( err != 0 ) {
            printf("WSAStartup error %ld", WSAGetLastError());
            WSACleanup();
            return false;
            }
            //---------------------------------------------------------
            
            //------------ server socket ----------------------------
            server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
            server.sin_family = AF_INET; // address family Internet
            server.sin_port = htons (SERVER_PORT); //Port to connect on
            //-----------------------------------------------------------------
            
            while (1) {
            
            // ---- create SOCKET--------------------------------------
            s = socket(AF_INET, SOCK_DGRAM, 0); //0 specifies to use appropriate protocol according to request
            if (s < 0) {
            printf("socket error %ld",WSAGetLastError() );
            WSACleanup();
            return false;
            }
            //--------------------------------------------------------
            
            
            //---- BIND socket ----------------------------------------
            b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr));
            if (b == -1) {
            printf("bind error\n");
            printf("socket error %ld",WSAGetLastError() );
            WSACleanup();
            getchar();
            return false;
            }
            
            //----------------------------------------------------------
            while(1)
            {
            printf("waiting for data\n");
            
            //-----------receive bytes -------------------------------------------------------------
            
            bytesRecv = recvfrom(s, recvbuf, 10, 0, (struct sockaddr * )&client, &addr_len );
            if(bytesRecv < 0){
            printf("recvfrom error : %ld", WSAGetLastError());
            WSACleanup();
            }
            
            printf("connection ");
            if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
            printf( "Closed.\n");
            WSACleanup();
            }
            else printf("accepted\n");
            printf("received from: %s %d\n ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
            
            printf( " Bytes Recv: %s \n ", recvbuf );
            }
            //-------------------------------------------------------------
            
            
            }
            WSACleanup();
            
            return 0;
            }
            the client I made a few modifications so it would compile on my machine
            Code:
            #include <iostream>
            #include <winsock2.h>
            
            
            using namespace std;
            //cleint.cpp
            #define TAR_IPAddress "192.128.1.85"
            
            #define SERVER_PORT 1167
            #define BUF_SIZE 4096 // block transfer size
            #define QUEUE_SIZE 10
            #define IPAddress "127.0.0.1" // Local to the system - Loop back address
            
            
            int main()
            {
            WORD wVersionRequested;
            WSADATA wsaData;
            SOCKADDR_IN server, client; //Socket address information
            SOCKET s;
            struct hostent *hp;
            int err, b;
            int bytesSent, bytesRecv;
            char buf[100] = "hi hello", k[2], recvbuf[100] ;
            char host_name[156]; //host name of this machine
            int addr_len = sizeof(struct sockaddr);
            
            
            
            //--- INITIALIZATION -----------------------------------
            wVersionRequested = MAKEWORD( 1, 1 );
            err = WSAStartup( wVersionRequested, &wsaData );
            
            if ( err != 0 ) {
            printf("WSAStartup error %ld", WSAGetLastError());
            WSACleanup();
            return false;
            }
            //---------------------------------------------------------
            
            
            //---- creat a server struct.--------
            server.sin_family = AF_INET; // address family Internet
            server.sin_port = htons (SERVER_PORT); //Port to connect on
            server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
            
            //--------------------------------------------------------
            
            //---- creat a client struct---------------------------------
            client.sin_family = AF_INET; // address family Internet
            client.sin_port = htons (0); //Port to connect on
            client.sin_addr.s_addr = inet_addr(IPAddress); //host IP
            //--------------------------------------------------------
            
            
            while(1) {
            // ---- create SOCKET--------------------------------------
            s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
            if (s == INVALID_SOCKET)
            {
            printf("socket error %ld" , WSAGetLastError() );
            getchar();
            WSACleanup();
            return false; //Couldn't create the socket
            }
            //---------------------------------------------------------
            
            //---- SEND bytes -------------------------------------------
            
            printf("trans buf:('q' to quit)\n");
            gets(buf);
            if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
            break;
            bytesSent = sendto (s, buf, 10, 0, (struct sockaddr*) &server, sizeof(sockaddr));
            if(bytesSent < 0 ){
            printf("sendto fails with : %ld\n ", WSAGetLastError());
            // _getch();
            WSACleanup();
            }
            else
            printf("bytes sent : %d\n" ,bytesSent );
            //-------------------------------------------------------------------------
            
            
            // WSACleanup();
            }
            return 0;
            }
            it then ran OK - the client output was
            Code:
            trans buf:('q' to quit)
            hello 
            bytes sent : 10
            trans buf:('q' to quit)
            this is message 2
            bytes sent : 10
            trans buf:('q' to quit)
            message3
            bytes sent : 10
            trans buf:('q' to quit)
            goodbye
            bytes sent : 10
            trans buf:('q' to quit)
            q
            and the server
            Code:
            waiting for data
            connection accepted
            received from: 127.0.0.1 1200
              Bytes Recv: hello 
             waiting for data
            connection accepted
            received from: 127.0.0.1 1202
              Bytes Recv: this is meÿÿ)NÃwBNÃwPÍ@ 
             waiting for data
            connection accepted
            received from: 127.0.0.1 1204
              Bytes Recv: message3 
             waiting for data
            connection accepted
            received from: 127.0.0.1 1206
              Bytes Recv: goodbye 
             waiting for data
            clearly still a problem with buffer lengths
            this only works with the client using localhost 127.0.0.1 not sure why

            Comment

            • saee
              New Member
              • Feb 2009
              • 24

              #7
              Thank you. It works fine.

              Comment

              • saee
                New Member
                • Feb 2009
                • 24

                #8
                horace1, can you please tell me why is it not working with network ip address?
                Instead of loop back address i will have give another machines IP address. n it shoulld communicate. but server is not responding. it gets hung on Recvfrom().

                Comment

                Working...