ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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///////////////////////////////////
/////////////////////////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");
}
/////////////////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)
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.
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.
//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;
}
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.