Thread: UDP Client/Server in C

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    Question UDP Client/Server in C

    Hello, I'm currently working on an assignment that requires C but I only have experience with C++ so I'm having some trouble.

    The assignment is asking me to implement a client/server that communicates with each other over UDP. Basically the client is supposed to send over a number as a command line input(up to 128 characters) and the server needs to check if it is indeed a number. If it is, then it adds up the individual digits until there is only one digit and then sends the result to the client.

    Example

    Enter string: 123456789101234567891012345678910
    From server: 138
    From server: 12
    From server: 3

    Here is what I have so far:


    Server
    Code:
    /* Creates a datagram server.  The port   number is passed as an argument.  This
       server runs forever */
    
    
    #include <sys/types.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <ctype.h>
    
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    
    int main(int argc, char *argv[])
    {
       int sock, length, n;
       socklen_t fromlen;
       struct sockaddr_in server;
       struct sockaddr_in from;
       char buf[1024];
       int sum = 0;
    
    
    
    
       if (argc < 2) {
          fprintf(stderr, "ERROR, no port provided\n");
          exit(0);
       }
    
    
       sock=socket(AF_INET, SOCK_DGRAM, 0);
       if (sock < 0) error("Opening socket");
       length = sizeof(server);
       bzero(&server,length);
       server.sin_family=AF_INET;
       server.sin_addr.s_addr=INADDR_ANY;
       server.sin_port=htons(atoi(argv[1]));
       if (bind(sock,(struct sockaddr *)&server,length)<0)
           error("binding");
       fromlen = sizeof(struct sockaddr_in);
       while (1) {
           n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
           if (n < 0) error("recvfrom");
           write(1,"Received a datagram: ",21);
           write(1,buf,n);
    
    
    
           for(int i = 0; i < strlen(buf); i++){
                    if(!isdigit(buf[i])){
                            n = sendto(sock,"We can't compute!\n",19,0,(struct sockaddr *)&from,fromlen);
                    }
            }
           n = sendto(sock,"We can compute!\n",17,0,(struct sockaddr *)&from,fromlen);
    
    
           snprintf(buf, sizeof(buf), "%c", sum);
           n = sendto(sock,buf,128,0,(struct sockaddr *)&from,fromlen);
    
    
    
    
           if (n  < 0) error("sendto");
       }
       return 0;
     }
    Client
    Code:
    /* UDP client in the internet domain */
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    
    void error(const char *);
    int main(int argc, char *argv[])
    {
       int sock, n;
       unsigned int length;
       struct sockaddr_in server, from;
       struct hostent *hp;
       char buffer[256];
    
    
       if (argc != 3) { printf("Usage: server port\n");
                        exit(1);
       }
       sock= socket(AF_INET, SOCK_DGRAM, 0);
       if (sock < 0) error("socket");
    
    
       server.sin_family = AF_INET;
       hp = gethostbyname(argv[1]);
       if (hp==0) error("Unknown host");
    
    
       bcopy((char *)hp->h_addr,
            (char *)&server.sin_addr,
             hp->h_length);
       server.sin_port = htons(atoi(argv[2]));
       length=sizeof(struct sockaddr_in);
       printf("Please enter the message: ");
       bzero(buffer,256);
       fgets(buffer,255,stdin);
       n=sendto(sock,buffer,
                strlen(buffer),0,(const struct sockaddr *)&server,length);
       if (n < 0) error("Sendto");
    
    
       n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);
       if (n < 0) error("recvfrom");
       write(1,"From server: ",13);
       write(1,buffer,n);
    
    
       close(sock);
       return 0;
    }
    
    
    void error(const char *msg)
    {
        perror(msg);
        exit(0);
    }
    The part I'm stuck on is this part from the server code
    Code:
     while (1) {
           n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
           if (n < 0) error("recvfrom");
           write(1,"Received a datagram: ",21);
           write(1,buf,n);
    
    
    
           for(int i = 0; i < strlen(buf); i++){                
                    if(isdigit(buf[i])){
                            sum += buf[i] - '0';
                            printf("%d", sum);
                    }
                    else{
                            n = sendto(sock,"We can't compute!\n",19,0,(struct sockaddr *)&from,fromlen);
                            break;
                    }
    
    
            }
    
    
    
    
            //snprintf(buf, sizeof(buf), "%c", sum);
            //n = sendto(sock,buf,128,0,(struct sockaddr *)&from,fromlen);
    
    
    
    
           if (n  < 0) error("sendto");
       }
    I can't seem to even get the server to recognize a number even when I send a simple number, like 10. This part was working before but I must've changed something and I can't figure it out. I always get the "We can't compute!" response from the server.

    Also I am completely stumped on how to convert an integer to a char[]. I'm able to get the buffer from the client and convert it into an integer but I don't know how to convert it back to a char[] to send the sum back to the client. This is my attempt so far using a hard-coded buffer.
    Code:
    int main(void) {
      char buf[128] = "123456789";
      
      int sum;
    
    
      for(int i = 0; i < 128; i++){
        if(isdigit(buf[i])){
          sum += buf[i] - '0';
        
        }
      }  
      printf("%d\n", sum); 
        
     return 0; 
    }
    
    Any help would be appreciated.
    Last edited by daniel212; 04-28-2019 at 12:14 AM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Also I am completely stumped on how to convert an integer to a char[]
    The most neatest and portable way would be to use sprintf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-16-2018, 08:30 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. Client - Server TCP/IP MFC app..... Help!
    By amedinadiaz in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2005, 11:57 AM
  4. client - server
    By Micko in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-12-2004, 02:49 AM
  5. client / server
    By PutoAmo in forum C Programming
    Replies: 4
    Last Post: 05-24-2002, 05:09 PM

Tags for this Thread