Thread: UDO socket programming in C

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    UDP socket programming in C

    Hello Everyone
    I am trying to test my client-server socket program wherein the client connects to the server,client sends a message, server receives it and echo back to the client.So far, in the program server receives the message from the client, prints it BUT when it tries to send the message back to the client it shows an error.

    sendto(): Invalid argument.
    I am new to socket programming and any help would be much appreciated. Thank you


    Code:
    //Server
    
    #include<stdio.h> //printf
    #include<string.h> //memset
    #include<stdlib.h> //exit(0)
    #include<netinet/in.h>
    #include<sys/socket.h>
    #define BUFLEN 512 //Max length of buffer
    #define PORT 10003 //The port on which to listen for incoming data
    
    
    //error check fucntion
    void die(char *sockS)
    {
    perror(sockS);
    exit(1);
    }
    
    
    
    int main(void)
    {
    
    struct sockaddr_in S_Serv, S_Cli;
    
    int sockS,slen=sizeof(S_Cli),recv_len;
    char buf[BUFLEN];
    char hostname[128];
    
    // To get host name
    gethostname(hostname, sizeof hostname);
    printf("My hostname: %s\n", hostname);
    
    //create a UDP server socket
    
    if((sockS=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1)
    {
    die("socket");
    }
    
    //zero out the structure
    memset((char *)&S_Serv,0,sizeof(S_Serv));
    
    S_Serv.sin_family= AF_INET;
    S_Serv.sin_port= htons(PORT);
    S_Serv.sin_addr.s_addr=htonl(INADDR_ANY);
    
    
    //bind socket to port
    if (bind(sockS, (struct sockaddr *)&S_Serv,sizeof(S_Serv))==-1)
    {
    
    die("bind");
    }
    
    
    //keep listening for data
    while(1)
    {
    printf("waiting for data.......");
    fflush(stdout);
    
    
    //try to receive some data (blocking call)
    if ((recv_len=recvfrom(sockS,buf,BUFLEN,0,(struct sockaddr *)&S_Cli,&slen)) == -1)
    
    
    {
    die("recvfrom()");
    
    }
    
    //print details of the client/peer and the data received
    printf("Received packet from client %d:%d\n",inet_ntoa(S_Cli.sin_addr),ntohs(S_Cli.sin_port));
    printf("data: %s\n",buf);
    
    //reply the client with the same data
    if(sendto(sockS, buf, recv_len,0,(struct sockaddr *) &S_Serv, slen) == -1)
    {
    die("sendto()");
    
    }
    
    
    }
    close(sockS);
    return 0;
    
    }
    
    
    
    Client
    
    #include<stdio.h> //printf
    #include<string.h> //memset
    #include<stdlib.h> //exit(0)
    #include<netinet/in.h>
    #include<sys/socket.h>
    #define BUFLEN 512 //Max length of buffer
    #define PORT 10003 //The port on which to send data
    #define SERVER "127.0.0.1"
    
    void die(char *sockC)
    {
    perror(sockC);
    exit(0);
    }
    
    
    int main(void)
    {
    struct sockaddr_in S_Cli;
    int sockC,slen=sizeof(S_Cli);
    char buf[BUFLEN];
    char message[BUFLEN];
    
    if ((sockC=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1)
    {
    die("socket");
    }
    
    
    memset((char *)& S_Cli,0,sizeof(S_Cli));
    S_Cli.sin_family=AF_INET;
    S_Cli.sin_port=htons(PORT);
    
    if(inet_aton(SERVER,&S_Cli.sin_addr)==0)
    {
    fprintf(stderr,"inet_aton() failed \n");
    exit(1);
    }
    
    
    while(1)
    {
    
    printf("enter message: ");
    gets(message);
    
    
    //send the message
    
    if(sendto(sockC,message,strlen(message),0,(struct sockaddr *)&S_Cli, slen)==-1)
    {
    die("sendto()");
    
    }
    
    //receive a reply and print it
    //clear the buffer by filling null, as it might have previous received data
    
    memset(buf,'0',BUFLEN);
    
    //try to receive some data
    if (recvfrom(sockC,buf, BUFLEN,0,(struct sockaddr *)&S_Cli,&slen)==-1)
    {
    die("recvfrom()");
    
    }
    
    puts(buf);
    }
    
    close(sockC);
    return 0;
    
    }
    Last edited by biswa; 12-18-2014 at 05:21 AM.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by biswa View Post
    Hello Everyone
    I am trying to test my client-server socket program wherein the client connects to the server,client sends a message, server receives it and echo back to the client.So far, in the program server receives the message from the client, prints it BUT when it tries to send the message back to the client it shows an error.

    sendto(): Invalid argument.
    I am new to socket programming and any help would be much appreciated. Thank you
    The server send the answer back to himself …
    Quote Originally Posted by biswa View Post
    Code:
    […]
    //reply the client with the same data
         if(sendto(sockS, buf, recv_len,0,(struct sockaddr *) &S_Serv, slen) == -1)
        {
            die("sendto()");
        }
    […]
    … but you want sent it back to client.
    Code:
    […]
    //reply the client with the same data
         if(sendto ( sockS, buf, recv_len, 0, (struct sockaddr *) &S_Cli, sizeof(S_Cli)) == -1)
        {
            die("sendto()");
        }
    […]
    Addition:
    If you want send strings, then include the terminating zero.
    Code:
    […]
        memset( &S_Cli, 0, sizeof(S_Cli));
    […]
        //send the message
        if(sendto( sockC, message, strlen(message) + 1 , 0, (struct sockaddr *) &S_Cli, slen) == -1)
        {
            die("sendto()");
        }
    […]
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    Wink

    Thank WoodSTokk for your kind suggestion. It pushed me towards the right direction..I finally managed to single out the problem. I am now posting the full new working code...


    Code:
    #include<stdio.h>		//printf
    #include<string.h>		//memset
    #include<stdlib.h>		//exit(0)
    #include<netinet/in.h>
    #include<sys/socket.h>
    #include<arpa/inet.h>
    #define BUFLEN 512  //Max length of buffer
    #define PORT 10005   //The port on which to listen for incoming data
    
    
    //error check fucntion
    void die(char *sockS)
    {
    perror(sockS);
    exit(1);
    }
    
    
    int main(void)
    {
    
    struct sockaddr_in S_Serv, S_Cli;
    
    int  sockS,recv_len;
    socklen_t slen=sizeof(S_Cli);
    char buf[BUFLEN];
    char hostname[128];
    
    //////////////////////// To get host name /////////////////////////////////////////
        gethostname(hostname, sizeof hostname);
        printf("My hostname: %s\n", hostname);
    
    ///////////////////////create a UDP server socket///////////////////////////////////
    
    if((sockS=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1)
    {
    die("socket");
    }
    
    /////////////////////////zero out the structure////////////////////////////////
    memset((char *)&S_Serv,0,sizeof(S_Serv));
    
    S_Serv.sin_family= AF_INET;
    S_Serv.sin_port= htons(PORT);
    S_Serv.sin_addr.s_addr=htonl(INADDR_ANY);
    
    
    /////////////////////////bind socket to port/////////////////////////////////////
    if (bind(sockS, (struct sockaddr*)&S_Serv,sizeof(S_Serv))==-1)
    {
      
      die("bind");
    }
    
    
    
    //////////////////////////keep listening for data//////////////////////////////
    
    
    
    
    while(1)
    {
      
      printf("waiting for data.......\n\n");
      fflush(stdout);
      
      
    ///////////try to receive some data (blocking call)/////////////////
    if (recv_len=recvfrom(sockS,buf,BUFLEN,0,(struct sockaddr*)&S_Cli,&slen) == -1)
       
      
      {
        die("recvfrom()");
        
      }
    
    ///////////print details of the client/peer and the data received/////
      printf("Received packet from client %d:%d\n",inet_ntoa(S_Cli.sin_addr),ntohs(S_Cli.sin_port));
      printf("data: %s\n",buf);
    
      
    ////////////reply the client with the same data//////////////////////struct sockaddr_in
      if(sendto(sockS, buf, BUFLEN,0,(struct sockaddr*) &S_Cli, slen) == -1)
      {
        die("sendto()");
        
      }
      
    
    }
    close(sockS);
    return 0;
    
    }


    GOOOd byee

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    Also the code on the client side is

    Code:
    #include<stdio.h>		//printf
    #include<string.h>		//memset
    #include<stdlib.h>		//exit(0)
    #include<netinet/in.h>		
    #include<sys/socket.h>
    #define BUFLEN 512 		//Max length of buffer
    #define PORT 10005		//The port on which to send data
    #define SERVER "127.0.0.1"
    
    void die(char *sockC)
    {
      perror(sockC);
      exit(0);
    }
    
    
    int main(void)
    {
      struct sockaddr_in S_Cli;
      int sockC,slen=sizeof(S_Cli);
      char buf[BUFLEN];
      char message[BUFLEN];
      
      if ((sockC=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==-1)
      {
        die("socket");
      }
      
      
      memset((char *)& S_Cli,0,sizeof(S_Cli));
      S_Cli.sin_family=AF_INET;
      S_Cli.sin_port=htons(PORT);
      
      if(inet_aton(SERVER,&S_Cli.sin_addr)==0)
      {
        fprintf(stderr,"inet_aton() failed \n");
        exit(1);
      }
      
      
      while(1)
      {
        
        printf("enter message: ");
        gets(message);
        
        
    ////////////////send the message///////////////////////////
        
        if(sendto(sockC,message,strlen(message)+1,0,(struct sockaddr *)&S_Cli, slen)==-1)
        {
          die("sendto()");
          
        }
    
    /////////////////receive a reply and print it//////////////
    ////////////////clear the buffer by filling null, as it might have previous received data/////////
    
    memset(buf,'0',BUFLEN);
    
    //////////////try to receive some data////////////////////
    if (recvfrom(sockC,buf, BUFLEN,0,(struct sockaddr *)&S_Cli,&slen)==-1)
    {
      die("recvfrom()");
      
    }
        
        puts(buf);
      }
      
      close(sockC);	
      return 0;  
      
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  3. Socket Programming
    By pobri19 in forum C Programming
    Replies: 6
    Last Post: 06-01-2008, 07:01 AM
  4. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM
  5. socket programming
    By mwagiru in forum C Programming
    Replies: 2
    Last Post: 05-22-2003, 06:56 AM

Tags for this Thread