LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2014, 07:11 AM   #1
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Rep: Reputation: Disabled
UDP Client-Server scoket programming


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


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;

}


The output(Server side)
My hostname: bis
waiting for data.......Received packet from client *.*.*.*:47883
data: bis
sendto(): Invalid argument

The output(Client side)
enter message: bis

When i try to compile this is the warning i get, which i suspect might be the reason for not receiving any message on the client side.


warning: passing argument 6 of ‘sendto’ makes integer from pointer without a cast [enabled by default]
if(sendto(sockS, buf, recv_len,0,(struct sockaddr *) &S_Serv, &slen) == -1)



Looking forward to any reply.
 
Old 12-17-2014, 08:23 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
If you look in the man pages you'll see the sixth argument to sendto is "socklen_t addrlen" which is likely a signed integer type. You're passing the address of your variable.

If you wrap your code in [code][/code] tags your formatting will be preserved.
 
Old 12-17-2014, 09:36 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
old:
sendto (sockS, buf, recv_len, 0, (struct sockaddr *)&S_Serv, &slen)

new: 
sendto (sockS, buf, recv_len, 0, (struct sockaddr *)&S_Serv, slen)
 
Old 12-17-2014, 10:02 AM   #4
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank u SoftSprocket && NevemTeve for your quick responses...I did change the system call to
sendto (sockS, buf, recv_len, 0, (struct sockaddr *)&S_Serv, slen)

The error still remains
My hostname: bis
waiting for data.......Sock port 10004
Received packet from client ****:38187
data: bis
sendto(): Bad file descriptor

Please correct my logic if I am wrong::the sendto() will echo back the message to client through server.So effectively, if i send bis on client terminal, i am expecting to print out bis on server terminal and the server sends me back the same message.

Thanks
 
Old 12-17-2014, 10:25 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start, paste the current state of your program(s) between [code] and [/code] tags.
 
Old 12-17-2014, 10:29 AM   #6
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
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;

}
Silly me...learned a new trick

Last edited by bislinux; 12-17-2014 at 10:38 AM.
 
Old 12-17-2014, 10:42 AM   #7
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
When you're running the compiler you can increase the warnings with the -Wall option. Read the warnings and try to understand them.

Your server is still passing a pointer for the sixth argument of sendto.
 
Old 12-18-2014, 02:49 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
First, fix every compilation warning. Options -W -Wall -Wextra -pedantic -Werror will help
 
Old 12-18-2014, 05:36 AM   #9
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
I am at my wits ends....are there any more suggestions on solving this problem ?
thanks
 
Old 12-18-2014, 08:12 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Have you fixed the warnings?
Have you tried yet?
Do you know what that means?
Why not?
 
Old 12-18-2014, 11:17 AM   #11
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you every one for your kind support....Yes i did manage to fix the bug...(eventually)...Sometimes the solution lies in front of your eyes while you are looking everywhere else....I am posting the new updated code for everybodies reference....Have a good day


Server side
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;

}
Client
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;  
  
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket programming - UDP broadcast client - how to receive responses - Python 0x53h Programming 2 12-16-2013 02:16 PM
Problem with RAW UDP SCOKET raju.poranki Programming 2 07-06-2010 10:55 AM
Problem While Doing Scoket Programming ruud Programming 1 04-28-2010 07:40 AM
Server/Client UDP FaresH Programming 3 12-02-2008 05:52 PM
Server Client program using UDP frostmagic Programming 9 11-14-2003 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration