Find the answer to your Linux question:
Results 1 to 4 of 4
Hello. I have a problem with a simple client/server socket program. the connection is done through 2 connections a udp connection and a tcp connection. the flow is that the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4

    socket programing problem


    Hello.
    I have a problem with a simple client/server socket program.
    the connection is done through 2 connections a udp connection and a tcp connection.
    the flow is that the client sends a udp packet to the server and the server returns it through a tcp connection.
    this is done in a loop.
    my code works fine on an ubuntu server, when I try to use it on redhat it sometimes get stuck.
    the server is stuck on recvfrom and the client waits on the recv.
    if i add a delay in the server or run the client through strace everything works fine.
    I am really stuck here,
    any ideas ?

    I am attaching my code so you can review

    server code:


    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>

    #include <sys/types.h>
    #include <sys/time.h>


    #define MAXPENDING 5 /* Max connection requests */
    #define BUFFSIZE 100

    #define LOCAL_SERVER_PORT 30001
    #define MAX_MSG 100

    int Timebuf[256];
    struct timeval start_time;
    struct timeval end_time;



    /* T.R udp stuff */
    int sd, rc, n, cliLen, flags,TempRes;
    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];
    int RetVal = 0;

    static int LoopCount = 0;
    int TotalTime = 0;
    int reuse_addr = 1;
    int no_delay = 1;
    struct linger l;




    void Die(char *mess) { perror(mess); exit(1); }




    void HandleTcpUdp(int sock)
    {
    char buffer[BUFFSIZE];
    int received = -1;
    /* Receive UDP message */
    /* init buffer */
    memset(msg,0xff,MAX_MSG);
    gettimeofday(&start_time,0);
    /* receive message */
    cliLen = sizeof(cliAddr);

    // flags |=MSG_WAITALL;
    n = recvfrom(sd, msg, MAX_MSG, flags,
    (struct sockaddr *) &cliAddr, &cliLen);

    if(n<0)
    {
    printf("cannot receive data \n");
    return;
    }

    gettimeofday(&end_time,0);
    Timebuf[LoopCount] = end_time.tv_usec - start_time.tv_usec;
    TotalTime += Timebuf[LoopCount];

    /* Send the message back to TCP client */
    // memcpy(buffer,msg,MAX_MSG);

    /* Send back received data */
    TempRes = 0;
    n = 0;
    while (TempRes < 100)
    {

    TempRes = send(sock, &msg[n], 100-n, 0);
    if (TempRes < 0)
    {
    Die("Failed to send bytes to client");
    }
    n += TempRes;
    }

    }


    int main(int argc, char *argv[])
    {
    int serversock, clientsock;
    struct sockaddr_in echoserver, echoclient;


    start_time.tv_sec = 0;
    start_time.tv_usec = 0;
    end_time.tv_sec = 0;
    end_time.tv_usec = 0;

    l.l_onoff = 1;
    l.l_linger = 0;


    if (argc != 2) {
    fprintf(stderr, "USAGE: echoserver <port>\n");
    exit(1);
    }
    /* Create the TCP socket */
    if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
    Die("Failed to create socket");
    }
    if ((RetVal = setsockopt(serversock, SOL_SOCKET, SO_LINGER, &l, sizeof (l))) < 0)
    {
    printf( "Failed to set linger on TCP socket %d!\n", RetVal);
    }


    if ((RetVal = setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse_addr, sizeof(reuse_addr))) < 0)
    {
    printf( "Failed to set reuse-addr to TCP socket %d!\n", RetVal);
    }

    if ((RetVal = setsockopt(serversock, IPPROTO_TCP, TCP_NODELAY, (char*)&no_delay, sizeof(no_delay))) < 0)
    {
    printf(" Failed to set no-delay to TCP socket !\n");
    }

    /* Construct the server sockaddr_in structure */
    memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
    echoserver.sin_family = AF_INET; /* Internet/IP */
    echoserver.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */
    echoserver.sin_port = htons(atoi(argv[1])); /* server port */

    /* Bind the server socket */
    if (bind(serversock, (struct sockaddr *) &echoserver,
    sizeof(echoserver)) < 0)
    {
    Die("Failed to bind the server socket");
    }
    /* Listen on the server socket */
    if (listen(serversock, MAXPENDING) < 0)
    {
    Die("Failed to listen on server socket");
    }

    unsigned int clientlen = sizeof(echoclient);
    /* Wait for client connection */
    if ((clientsock =
    accept(serversock, (struct sockaddr *) &echoclient,
    &clientlen)) < 0)


    {
    Die("Failed to accept client connection");
    }
    else
    {
    fprintf(stdout, "TCP Client connected: %s\n",
    inet_ntoa(echoclient.sin_addr));
    l.l_onoff = 1;
    l.l_linger = 0;
    if ((RetVal = setsockopt(clientsock, SOL_SOCKET, SO_LINGER, &l, sizeof (l)))< 0)
    {
    printf ("could not set linger on tcp sock \n");
    }
    }



    /* socket creation */
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd<0)
    {
    printf("cannot open udp socket \n");
    exit(1);
    }
    reuse_addr = 1;

    if ((RetVal = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse_addr, sizeof(reuse_addr))) < 0)
    {
    printf( "Failed to set reuse-addr to udp socket %d!\n", RetVal);
    }

    /* bind local server port */
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(LOCAL_SERVER_PORT);
    rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
    if(rc<0)
    {
    printf("cannot bind port number %d \n", LOCAL_SERVER_PORT);
    exit(1);
    }


    while (1)
    {

    HandleTcpUdp(clientsock);
    LoopCount++;

    if (LoopCount == 100) break;
    }

    for (LoopCount = 1;LoopCount < 100; LoopCount++)
    {
    printf("time diff %d packet %d\n",Timebuf[LoopCount],LoopCount);
    }
    printf ("Total time = %d, Avarege Time = %d\n", TotalTime, TotalTime / 99);


    /* clean and close sockets */


    close(clientsock);
    close(sd);
    close(serversock);
    }


    client code:




    #include <stdio.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>

    #define BUFFSIZE 100

    #define REMOTE_SERVER_PORT 30001
    #define MAX_MSG 100

    #define SOCKET_ERROR -1



    char const UdpServerIp[30] = "192.168.1.6";



    void Die(char *mess) { perror(mess); exit(1); }

    int main(int argc, char *argv[])
    {
    int sock;
    struct sockaddr_in echoserver;
    char buffer[BUFFSIZE];
    unsigned int echolen;

    /* UDP */
    int sd, rc, i, n, echoLen, flags, error, timeOut, temp, tmpres;
    struct sockaddr_in cliAddr, remoteServAddr, echoServAddr;
    struct hostent *h;
    char msg[MAX_MSG];
    struct linger l;
    int RetVal;

    int received = 0;

    l.l_onoff = 1;
    l.l_linger = 0;

    if (argc != 3)
    {
    fprintf(stderr, "USAGE: tcpudpclient <server_ip> <port>\n");
    exit(1);
    }
    /* Create the TCP socket */
    if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
    Die("Failed to create socket");

    }


    /* Construct the server sockaddr_in structure */
    memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
    echoserver.sin_family = AF_INET; /* Internet/IP */
    echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */
    echoserver.sin_port = htons(atoi(argv[2])); /* server port */
    /* Establish connection */
    if (connect(sock,
    (struct sockaddr *) &echoserver,
    sizeof(echoserver)) < 0)
    {
    Die("Failed to connect with server");
    }

    /* t.r handle UDP */

    /* get server IP address (no check if input is IP address or DNS name */
    h = gethostbyname(UdpServerIp);
    if(h==NULL)
    {
    printf("%s: unknown host '%s' \n", argv[0], argv[1]);
    exit(1);
    }


    remoteServAddr.sin_family = AF_INET; /* Internet/IP */
    remoteServAddr.sin_addr.s_addr = inet_addr(argv[1]);
    remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); /* server port */

    /* socket creation */
    sd = socket(AF_INET,SOCK_DGRAM,0);
    if(sd<0)
    {
    printf("cannot open UDP socket \n");
    exit(1);
    }
    /* bind any port */
    cliAddr.sin_family = AF_INET;
    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    cliAddr.sin_port = htons(0);

    rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
    if(rc<0)
    {
    printf("%s: cannot bind port\n", argv[0]);
    exit(1);
    }

    flags = 0;

    timeOut = 100; // ms

    for ( i=0; i < 100; i++)
    {

    memset(msg,i,100);
    rc = sendto(sd, msg, 100, flags,
    (struct sockaddr *) &remoteServAddr,
    sizeof(remoteServAddr));


    if(rc<0)
    {
    printf("%s: cannot send data %d \n",argv[0],i-1);
    close(sd);
    exit(1);
    }

    /* init buffer */
    memset(buffer,0xff,MAX_MSG);


    /* receive echoed message */
    echoLen = sizeof(echoServAddr);
    n = 0;
    tmpres = 0;

    while (n < 100 )
    {

    tmpres = recv(sock, &buffer[n], 100-n, 0);

    if(tmpres == -1)
    {
    printf("problem with recv\n");
    }

    n+= tmpres;
    }

    }


    close(sock);
    close(sd);
    exit(0);
    }





    .

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    please put code inside of the code tags, it makes it so much easier to read and deal with

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4
    ok, sorry for that.
    I attached the files.
    I have some more information.
    i can see in the sniffer and tcpdump that the first udp packet is sent from client to server.
    the server replies with icmp packet that says udp port 30001 unreachable
    Attached Files Attached Files

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2009
    Posts
    4
    ok, sorry again, just added the code again inside a code quote

    server code:


    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>

    #include <sys/types.h>
    #include <sys/time.h>


    #define MAXPENDING 5 /* Max connection requests */
    #define BUFFSIZE 100

    #define LOCAL_SERVER_PORT 30001
    #define MAX_MSG 100

    int Timebuf[256];
    struct timeval start_time;
    struct timeval end_time;



    /* T.R udp stuff */
    int sd, rc, n, cliLen, flags,TempRes;
    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];
    int RetVal = 0;

    static int LoopCount = 0;
    int TotalTime = 0;
    int reuse_addr = 1;
    int no_delay = 1;
    struct linger l;




    void Die(char *mess) { perror(mess); exit(1); }




    void HandleTcpUdp(int sock)
    {
    char buffer[BUFFSIZE];
    int received = -1;
    /* Receive UDP message */
    /* init buffer */
    memset(msg,0xff,MAX_MSG);
    gettimeofday(&start_time,0);
    /* receive message */
    cliLen = sizeof(cliAddr);

    // flags |=MSG_WAITALL;
    n = recvfrom(sd, msg, MAX_MSG, flags,
    (struct sockaddr *) &cliAddr, &cliLen);

    if(n<0)
    {
    printf("cannot receive data \n");
    return;
    }

    gettimeofday(&end_time,0);
    Timebuf[LoopCount] = end_time.tv_usec - start_time.tv_usec;
    TotalTime += Timebuf[LoopCount];

    /* Send the message back to TCP client */
    // memcpy(buffer,msg,MAX_MSG);

    /* Send back received data */
    TempRes = 0;
    n = 0;
    while (TempRes < 100)
    {

    TempRes = send(sock, &msg[n], 100-n, 0);
    if (TempRes < 0)
    {
    Die("Failed to send bytes to client");
    }
    n += TempRes;
    }

    }


    int main(int argc, char *argv[])
    {
    int serversock, clientsock;
    struct sockaddr_in echoserver, echoclient;


    start_time.tv_sec = 0;
    start_time.tv_usec = 0;
    end_time.tv_sec = 0;
    end_time.tv_usec = 0;

    l.l_onoff = 1;
    l.l_linger = 0;


    if (argc != 2) {
    fprintf(stderr, "USAGE: echoserver <port>\n");
    exit(1);
    }
    /* Create the TCP socket */
    if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
    Die("Failed to create socket");
    }
    if ((RetVal = setsockopt(serversock, SOL_SOCKET, SO_LINGER, &l, sizeof (l))) < 0)
    {
    printf( "Failed to set linger on TCP socket %d!\n", RetVal);
    }


    if ((RetVal = setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse_addr, sizeof(reuse_addr))) < 0)
    {
    printf( "Failed to set reuse-addr to TCP socket %d!\n", RetVal);
    }

    if ((RetVal = setsockopt(serversock, IPPROTO_TCP, TCP_NODELAY, (char*)&no_delay, sizeof(no_delay))) < 0)
    {
    printf(" Failed to set no-delay to TCP socket !\n");
    }

    /* Construct the server sockaddr_in structure */
    memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
    echoserver.sin_family = AF_INET; /* Internet/IP */
    echoserver.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */
    echoserver.sin_port = htons(atoi(argv[1])); /* server port */

    /* Bind the server socket */
    if (bind(serversock, (struct sockaddr *) &echoserver,
    sizeof(echoserver)) < 0)
    {
    Die("Failed to bind the server socket");
    }
    /* Listen on the server socket */
    if (listen(serversock, MAXPENDING) < 0)
    {
    Die("Failed to listen on server socket");
    }

    unsigned int clientlen = sizeof(echoclient);
    /* Wait for client connection */
    if ((clientsock =
    accept(serversock, (struct sockaddr *) &echoclient,
    &clientlen)) < 0)


    {
    Die("Failed to accept client connection");
    }
    else
    {
    fprintf(stdout, "TCP Client connected: %s\n",
    inet_ntoa(echoclient.sin_addr));
    l.l_onoff = 1;
    l.l_linger = 0;
    if ((RetVal = setsockopt(clientsock, SOL_SOCKET, SO_LINGER, &l, sizeof (l)))< 0)
    {
    printf ("could not set linger on tcp sock \n");
    }
    }



    /* socket creation */
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd<0)
    {
    printf("cannot open udp socket \n");
    exit(1);
    }
    reuse_addr = 1;

    if ((RetVal = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse_addr, sizeof(reuse_addr))) < 0)
    {
    printf( "Failed to set reuse-addr to udp socket %d!\n", RetVal);
    }

    /* bind local server port */
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servAddr.sin_port = htons(LOCAL_SERVER_PORT);
    rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
    if(rc<0)
    {
    printf("cannot bind port number %d \n", LOCAL_SERVER_PORT);
    exit(1);
    }


    while (1)
    {

    HandleTcpUdp(clientsock);
    LoopCount++;

    if (LoopCount == 100) break;
    }

    for (LoopCount = 1;LoopCount < 100; LoopCount++)
    {
    printf("time diff %d packet %d\n",Timebuf[LoopCount],LoopCount);
    }
    printf ("Total time = %d, Avarege Time = %d\n", TotalTime, TotalTime / 99);


    /* clean and close sockets */


    close(clientsock);
    close(sd);
    close(serversock);
    }


    client code:




    #include <stdio.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>

    #define BUFFSIZE 100

    #define REMOTE_SERVER_PORT 30001
    #define MAX_MSG 100

    #define SOCKET_ERROR -1



    char const UdpServerIp[30] = "192.168.1.6";



    void Die(char *mess) { perror(mess); exit(1); }

    int main(int argc, char *argv[])
    {
    int sock;
    struct sockaddr_in echoserver;
    char buffer[BUFFSIZE];
    unsigned int echolen;

    /* UDP */
    int sd, rc, i, n, echoLen, flags, error, timeOut, temp, tmpres;
    struct sockaddr_in cliAddr, remoteServAddr, echoServAddr;
    struct hostent *h;
    char msg[MAX_MSG];
    struct linger l;
    int RetVal;

    int received = 0;

    l.l_onoff = 1;
    l.l_linger = 0;

    if (argc != 3)
    {
    fprintf(stderr, "USAGE: tcpudpclient <server_ip> <port>\n");
    exit(1);
    }
    /* Create the TCP socket */
    if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
    Die("Failed to create socket");

    }


    /* Construct the server sockaddr_in structure */
    memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
    echoserver.sin_family = AF_INET; /* Internet/IP */
    echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */
    echoserver.sin_port = htons(atoi(argv[2])); /* server port */
    /* Establish connection */
    if (connect(sock,
    (struct sockaddr *) &echoserver,
    sizeof(echoserver)) < 0)
    {
    Die("Failed to connect with server");
    }

    /* t.r handle UDP */

    /* get server IP address (no check if input is IP address or DNS name */
    h = gethostbyname(UdpServerIp);
    if(h==NULL)
    {
    printf("%s: unknown host '%s' \n", argv[0], argv[1]);
    exit(1);
    }


    remoteServAddr.sin_family = AF_INET; /* Internet/IP */
    remoteServAddr.sin_addr.s_addr = inet_addr(argv[1]);
    remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT); /* server port */

    /* socket creation */
    sd = socket(AF_INET,SOCK_DGRAM,0);
    if(sd<0)
    {
    printf("cannot open UDP socket \n");
    exit(1);
    }
    /* bind any port */
    cliAddr.sin_family = AF_INET;
    cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    cliAddr.sin_port = htons(0);

    rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
    if(rc<0)
    {
    printf("%s: cannot bind port\n", argv[0]);
    exit(1);
    }

    flags = 0;

    timeOut = 100; // ms

    for ( i=0; i < 100; i++)
    {

    memset(msg,i,100);
    rc = sendto(sd, msg, 100, flags,
    (struct sockaddr *) &remoteServAddr,
    sizeof(remoteServAddr));


    if(rc<0)
    {
    printf("%s: cannot send data %d \n",argv[0],i-1);
    close(sd);
    exit(1);
    }

    /* init buffer */
    memset(buffer,0xff,MAX_MSG);


    /* receive echoed message */
    echoLen = sizeof(echoServAddr);
    n = 0;
    tmpres = 0;

    while (n < 100 )
    {

    tmpres = recv(sock, &buffer[n], 100-n, 0);

    if(tmpres == -1)
    {
    printf("problem with recv\n");
    }

    n+= tmpres;
    }

    }


    close(sock);
    close(sd);
    exit(0);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •