Help needed in a simple UDP socket program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vitali Gontsharuk

    Help needed in a simple UDP socket program

    Hi!

    I have a problem programming a simple client-server game, which is
    called pingpong ;-)
    The final program will first be started as a server (nr. 2) and then as
    a client. The client then sends the message "Ping" to the server, which
    reads it and answers with a "Pong".
    The game is really simple and the coding should be also very simple! But
    for me it isn't.
    By the way, the program uses datagram sockets (UDP). And, I'm using
    Cygwin (don't think the problem occurs due to that).
    What the program now does is, upon the client start, to send the "Ping"
    message to the server, which in turn sends an answer to the client. The
    "sendto" invoked in the start_server() function deliveres the length of
    the used buffer "mesg" as a result - but the message can't be received
    by the client, the execution blocks!
    It must be an easy to solve problem, but I'm now at the end of my nerves
    and can't see what the problem is.
    If you could help me, I would really appreciate that!

    Thanks in advance,
    Vitali Gontsharuk

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <netdb.h>

    void error(char *msg);
    void debug_s(char *);
    void debug_i(int);
    char *get_ip_for_hos tname(char *hostname);

    void server_start();
    void client_start(ch ar *server_ip_addr );

    /*int main(int argc, char *argv[]);*/

    #include "pingpong.h "

    #define CLI_MOD 1
    #define SERV_MOD 2
    #define MAX_MSG_SIZE 512
    char mesg[MAX_MSG_SIZE] = "";
    char debug_buf[256]="";

    const int cli_port = 2001;
    const int serv_port = 2002;
    const char *ping = "Ping";
    const char *pong = "Pong";

    void error(char *msg)
    {
    perror(msg);
    exit(1);
    }

    void debug_s(char *msg)
    {
    printf("DEBUG: %s\n", msg);
    }

    void debug_i(int number)
    {
    printf("DEBUG: %d\n", number);
    }

    void print_serv(char *msg)
    {
    printf("SERVER: '%s'\n", msg);
    }

    void print_cli(char *msg)
    {
    printf("CLIENT: '%s'\n", msg);
    }

    char *get_ip_for_hos tname(char *hostname)
    {
    struct hostent *my_hostent = gethostbyname(h ostname);
    return my_hostent->h_addr;
    }

    void server_start(ch ar *client_hostnam e)
    {
    int temp = 0;
    int sockfd, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    /*
    * Make the initializations
    */
    memset(mesg, 0, MAX_MSG_SIZE);
    memset(&serv_ad dr, 0, sizeof(serv_add r));
    memset(&cli_add r, 0, sizeof(cli_addr ));

    /*
    * Create an unnamed socket
    */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    /*
    * Initialize the server address structure and bind it to the
    * previously created socket
    */
    serv_addr.sin_f amily = AF_INET;
    serv_addr.sin_a ddr.s_addr = INADDR_ANY;
    serv_addr.sin_p ort = htons(serv_port );
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_add r)) <
    0)
    error("ERROR on binding");
    /*
    * Initialize the client address structure
    */
    memset(&cli_add r, 0, sizeof(cli_addr ));

    cli_addr.sin_fa mily = AF_INET;
    cli_addr.sin_ad dr.s_addr =
    inet_addr(get_i p_for_hostname( client_hostname ));
    cli_addr.sin_po rt = htons(cli_port) ;
    clilen = sizeof(cli_addr );

    /*
    * while(-1 < recv(sockfd,buf fer,sizeof(buff er),0)) {
    */
    while((temp=
    recvfrom(sockfd , mesg, MAX_MSG_SIZE, 0,
    (struct sockaddr *) &cli_addr,
    (socklen_t *) & clilen))>-1) {
    sprintf(debug_b uf,"%d chars received from client",temp);
    print_serv(debu g_buf);
    print_serv(mesg );
    if (0 != strcmp(mesg, ping))
    error(strcat("G ot the wrong string from the client: ", mesg));
    print_serv("Got Ping!");
    memset(mesg, 0, MAX_MSG_SIZE);
    strcpy(mesg, pong);
    sprintf(debug_b uf,"The following message will be sent to the client: %s",mesg);
    print_serv(debu g_buf);
    print_serv("sen ding pong to the client...");
    temp =
    sendto(sockfd, mesg, MAX_MSG_SIZE, 0,
    (struct sockaddr *) &cli_addr, clilen);
    sprintf(debug_b uf,"%d chars sent to client",temp);
    print_serv(debu g_buf);
    if (-1 == temp)
    error("Could not send Pong to the client!");
    }
    printf("Server ended!");
    }

    void client_start(ch ar *server_hostnam e)
    {
    int temp = 0;
    int sockfd, servlen, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    struct hostent *server;
    /*
    * Make the initializations
    */
    memset(mesg, 0, MAX_MSG_SIZE);
    memset(&serv_ad dr, 0, sizeof(serv_add r));
    memset(&cli_add r, 0, sizeof(cli_addr ));
    print_serv(mesg );

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    error("ERROR opening socket");
    if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
    }
    /*
    * Initialize the client address structure
    */
    /*
    cli_addr.sin_fa mily = AF_INET;
    cli_addr.sin_ad dr.s_addr = INADDR_ANY;
    cli_addr.sin_po rt = htons(cli_port) ;
    if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr )) < 0)
    error("ERROR on binding");
    */
    /*
    * Initialize the server address structure
    */
    memset(&serv_ad dr, 0, sizeof(serv_add r));
    serv_addr.sin_f amily = AF_INET;
    serv_addr.sin_a ddr.s_addr =
    inet_addr(get_i p_for_hostname( server_hostname ));
    serv_addr.sin_p ort = htons(serv_port );
    servlen = sizeof(serv_add r);
    if ((connect
    (sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_add r))) < 0)
    error("ERROR connecting");
    strcpy(mesg,pin g);
    while ((temp = write(sockfd, mesg, MAX_MSG_SIZE)) != -1) {
    sprintf(debug_b uf, "%d chars sent to server.", temp);
    print_cli(debug _buf);
    print_cli("Rece iving pong from server...");
    temp=recvfrom(s ockfd, mesg, MAX_MSG_SIZE, 0,
    (struct sockaddr *) &serv_addr, &servlen);
    sprintf(debug_b uf, "%d received from server",temp);
    print_cli(debug _buf);
    if(temp==-1)
    error("Could not receive pong from server!");
    print_cli("Got pong!");
    if (strcmp(mesg, pong) != 0) {
    error("Client: kein Pong als Antwort!");
    }
    memset(mesg, 0, MAX_MSG_SIZE);
    strcpy(mesg,pin g);
    }
    printf("Client ended!");
    }

    int main(int argc, char *argv[])
    {
    if (argc != 3) {
    printf(
    "Usage: %s <mode: 1(client)|2(ser ver)> <hostname (of the other side)>\n",
    argv[0]);
    exit(1);
    }
    int mode = atoi(argv[1]);
    char *hostname = argv[2];
    if (mode == CLI_MOD) {
    client_start(ho stname);
    } else if (mode == SERV_MOD) {
    server_start(ho stname);
    }
    }

  • santosh

    #2
    Re: Help needed in a simple UDP socket program

    Vitali Gontsharuk wrote:> Hi![color=blue]
    > I have a problem programming a simple client-server game, which is
    > called pingpong ;-)
    > The final program will first be started as a server (nr. 2) and then as
    > a client. The client then sends the message "Ping" to the server, which
    > reads it and answers with a "Pong".
    > The game is really simple and the coding should be also very simple! But
    > for me it isn't.
    > By the way, the program uses datagram sockets (UDP). And, I'm using[/color]
    .... snip ...

    All this is well beyond the scope of this group which deals with ISO C.
    Your question would be better answered in a group like
    comp.unix.progr amming etc.

    Comment

    • Vitali Gontsharuk

      #3
      Re: Help needed in a simple UDP socket program

      santosh wrote:[color=blue]
      > Vitali Gontsharuk wrote:> Hi!
      >[color=green]
      >> I have a problem programming a simple client-server game, which is
      >> called pingpong ;-)
      >> The final program will first be started as a server (nr. 2) and then as
      >> a client. The client then sends the message "Ping" to the server, which
      >> reads it and answers with a "Pong".
      >> The game is really simple and the coding should be also very simple! But
      >> for me it isn't.
      >> By the way, the program uses datagram sockets (UDP). And, I'm using
      >>[/color]
      > ... snip ...
      >
      > All this is well beyond the scope of this group which deals with ISO C.
      > Your question would be better answered in a group like
      > comp.unix.progr amming etc.
      >
      >[/color]

      Hello!
      Thanks, i will try it there!

      Comment

      Working...