I am trying to echo the string from the server to the client with UDP sockets but it is failing?










1















I have made one server and one client communicating through UDP sockets. The work that I am trying to do is that client will pass a string in the arguments and that string will be send to the server using UDP sockets. After receiving the string server will again echo(send) the string back to the client.Below are the codes for both:



code for echoClient.c :



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=3)

printf("echoClient <Ip addr. of the server> <String to be echoed> n");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565); // setting up the port
servAddr.sin_addr.s_addr = inet_addr(argv[1]); // using the given ip address of the server

printf("%sn", argv[2]);

if((sendto(sockfd,(const char *)argv[2],strlen(argv[2]), MSG_CONFIRM,(struct sockaddr *) &servAddr, sizeof(servAddr))!=-1))

printf("data is sent to the servern");

else

printf("can't send the data to the servern");
exit(3);


int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));

if(n==-1)

printf("Can't receive the data from the servern");
exit(4);


recvLine[n] = ''; // to terminate the received string

printf("%sn",recvLine);


return 0;



code for echoServer.c:



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr,clientAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=1)

printf("echoServern");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));
memset(&clientAddr,0,sizeof(clientAddr));


// filling the details of the server ip and port
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565);
servAddr.sin_addr.s_addr = INADDR_ANY; // using the given ip address of the server

if(bind(sockfd,(struct sockaddr * )&servAddr,sizeof(servAddr))<0)

printf("Binding of the socket failedn");
exit(1);


printf("Server is Up... Waiting for the client...n");

int len;
int n = recvfrom(sockfd,(char *) recvLine,MAXLINE,MSG_WAITALL,(struct sockaddr * )&clientAddr,&len);

if(n==-1)

printf("can't get the message from the clientn");
exit(2);


recvLine[n] = '';

printf("Message received from the client is %sn",recvLine);

if(sendto(sockfd,(char *) recvLine,n,MSG_CONFIRM,(struct sockaddr *)&clientAddr,len)<0)

printf("can't send the message to the clientn");
exit(3);


return 0;



Now the actual problen is that when I am executing the above codes client is able to send the string to the server but server is unable to send the string back to the client.Server gives the error can't send the message to the client.



I am not able to figure out the error which is stopping the server to send the message to the client.Please help me with this.



I am running the echoClient.c with the command :
./a.out 127.0.0.1 hellofromclientside










share|improve this question






















  • @AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

    – cisco
    Nov 14 '18 at 13:56















1















I have made one server and one client communicating through UDP sockets. The work that I am trying to do is that client will pass a string in the arguments and that string will be send to the server using UDP sockets. After receiving the string server will again echo(send) the string back to the client.Below are the codes for both:



code for echoClient.c :



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=3)

printf("echoClient <Ip addr. of the server> <String to be echoed> n");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565); // setting up the port
servAddr.sin_addr.s_addr = inet_addr(argv[1]); // using the given ip address of the server

printf("%sn", argv[2]);

if((sendto(sockfd,(const char *)argv[2],strlen(argv[2]), MSG_CONFIRM,(struct sockaddr *) &servAddr, sizeof(servAddr))!=-1))

printf("data is sent to the servern");

else

printf("can't send the data to the servern");
exit(3);


int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));

if(n==-1)

printf("Can't receive the data from the servern");
exit(4);


recvLine[n] = ''; // to terminate the received string

printf("%sn",recvLine);


return 0;



code for echoServer.c:



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr,clientAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=1)

printf("echoServern");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));
memset(&clientAddr,0,sizeof(clientAddr));


// filling the details of the server ip and port
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565);
servAddr.sin_addr.s_addr = INADDR_ANY; // using the given ip address of the server

if(bind(sockfd,(struct sockaddr * )&servAddr,sizeof(servAddr))<0)

printf("Binding of the socket failedn");
exit(1);


printf("Server is Up... Waiting for the client...n");

int len;
int n = recvfrom(sockfd,(char *) recvLine,MAXLINE,MSG_WAITALL,(struct sockaddr * )&clientAddr,&len);

if(n==-1)

printf("can't get the message from the clientn");
exit(2);


recvLine[n] = '';

printf("Message received from the client is %sn",recvLine);

if(sendto(sockfd,(char *) recvLine,n,MSG_CONFIRM,(struct sockaddr *)&clientAddr,len)<0)

printf("can't send the message to the clientn");
exit(3);


return 0;



Now the actual problen is that when I am executing the above codes client is able to send the string to the server but server is unable to send the string back to the client.Server gives the error can't send the message to the client.



I am not able to figure out the error which is stopping the server to send the message to the client.Please help me with this.



I am running the echoClient.c with the command :
./a.out 127.0.0.1 hellofromclientside










share|improve this question






















  • @AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

    – cisco
    Nov 14 '18 at 13:56













1












1








1








I have made one server and one client communicating through UDP sockets. The work that I am trying to do is that client will pass a string in the arguments and that string will be send to the server using UDP sockets. After receiving the string server will again echo(send) the string back to the client.Below are the codes for both:



code for echoClient.c :



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=3)

printf("echoClient <Ip addr. of the server> <String to be echoed> n");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565); // setting up the port
servAddr.sin_addr.s_addr = inet_addr(argv[1]); // using the given ip address of the server

printf("%sn", argv[2]);

if((sendto(sockfd,(const char *)argv[2],strlen(argv[2]), MSG_CONFIRM,(struct sockaddr *) &servAddr, sizeof(servAddr))!=-1))

printf("data is sent to the servern");

else

printf("can't send the data to the servern");
exit(3);


int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));

if(n==-1)

printf("Can't receive the data from the servern");
exit(4);


recvLine[n] = ''; // to terminate the received string

printf("%sn",recvLine);


return 0;



code for echoServer.c:



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr,clientAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=1)

printf("echoServern");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));
memset(&clientAddr,0,sizeof(clientAddr));


// filling the details of the server ip and port
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565);
servAddr.sin_addr.s_addr = INADDR_ANY; // using the given ip address of the server

if(bind(sockfd,(struct sockaddr * )&servAddr,sizeof(servAddr))<0)

printf("Binding of the socket failedn");
exit(1);


printf("Server is Up... Waiting for the client...n");

int len;
int n = recvfrom(sockfd,(char *) recvLine,MAXLINE,MSG_WAITALL,(struct sockaddr * )&clientAddr,&len);

if(n==-1)

printf("can't get the message from the clientn");
exit(2);


recvLine[n] = '';

printf("Message received from the client is %sn",recvLine);

if(sendto(sockfd,(char *) recvLine,n,MSG_CONFIRM,(struct sockaddr *)&clientAddr,len)<0)

printf("can't send the message to the clientn");
exit(3);


return 0;



Now the actual problen is that when I am executing the above codes client is able to send the string to the server but server is unable to send the string back to the client.Server gives the error can't send the message to the client.



I am not able to figure out the error which is stopping the server to send the message to the client.Please help me with this.



I am running the echoClient.c with the command :
./a.out 127.0.0.1 hellofromclientside










share|improve this question














I have made one server and one client communicating through UDP sockets. The work that I am trying to do is that client will pass a string in the arguments and that string will be send to the server using UDP sockets. After receiving the string server will again echo(send) the string back to the client.Below are the codes for both:



code for echoClient.c :



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=3)

printf("echoClient <Ip addr. of the server> <String to be echoed> n");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565); // setting up the port
servAddr.sin_addr.s_addr = inet_addr(argv[1]); // using the given ip address of the server

printf("%sn", argv[2]);

if((sendto(sockfd,(const char *)argv[2],strlen(argv[2]), MSG_CONFIRM,(struct sockaddr *) &servAddr, sizeof(servAddr))!=-1))

printf("data is sent to the servern");

else

printf("can't send the data to the servern");
exit(3);


int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));

if(n==-1)

printf("Can't receive the data from the servern");
exit(4);


recvLine[n] = ''; // to terminate the received string

printf("%sn",recvLine);


return 0;



code for echoServer.c:



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MAXLINE 4096

int main(int argc, char const *argv)

int sockfd;
struct sockaddr_in servAddr,clientAddr;
char sendLine[MAXLINE],recvLine[MAXLINE];

if(argc!=1)

printf("echoServern");
exit(1);


if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)

printf("Error in creating the socketn");
exit(2);



memset(&servAddr,0,sizeof(servAddr));
memset(&clientAddr,0,sizeof(clientAddr));


// filling the details of the server ip and port
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(6565);
servAddr.sin_addr.s_addr = INADDR_ANY; // using the given ip address of the server

if(bind(sockfd,(struct sockaddr * )&servAddr,sizeof(servAddr))<0)

printf("Binding of the socket failedn");
exit(1);


printf("Server is Up... Waiting for the client...n");

int len;
int n = recvfrom(sockfd,(char *) recvLine,MAXLINE,MSG_WAITALL,(struct sockaddr * )&clientAddr,&len);

if(n==-1)

printf("can't get the message from the clientn");
exit(2);


recvLine[n] = '';

printf("Message received from the client is %sn",recvLine);

if(sendto(sockfd,(char *) recvLine,n,MSG_CONFIRM,(struct sockaddr *)&clientAddr,len)<0)

printf("can't send the message to the clientn");
exit(3);


return 0;



Now the actual problen is that when I am executing the above codes client is able to send the string to the server but server is unable to send the string back to the client.Server gives the error can't send the message to the client.



I am not able to figure out the error which is stopping the server to send the message to the client.Please help me with this.



I am running the echoClient.c with the command :
./a.out 127.0.0.1 hellofromclientside







c sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:38









ciscocisco

83




83












  • @AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

    – cisco
    Nov 14 '18 at 13:56

















  • @AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

    – cisco
    Nov 14 '18 at 13:56
















@AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

– cisco
Nov 14 '18 at 13:56





@AravindVoggu I tried with n+1 but it is not helping. But I think this might not be the problem because n is only telling the length to be sent.

– cisco
Nov 14 '18 at 13:56












1 Answer
1






active

oldest

votes


















1














In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change



 int len;


to



 int len = sizeof clientAddr;


Similarly in the client, change



 int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));


to



 int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);





share|improve this answer




















  • 1





    Thanks!! it worked.

    – cisco
    Nov 14 '18 at 15:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301570%2fi-am-trying-to-echo-the-string-from-the-server-to-the-client-with-udp-sockets-bu%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change



 int len;


to



 int len = sizeof clientAddr;


Similarly in the client, change



 int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));


to



 int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);





share|improve this answer




















  • 1





    Thanks!! it worked.

    – cisco
    Nov 14 '18 at 15:35















1














In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change



 int len;


to



 int len = sizeof clientAddr;


Similarly in the client, change



 int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));


to



 int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);





share|improve this answer




















  • 1





    Thanks!! it worked.

    – cisco
    Nov 14 '18 at 15:35













1












1








1







In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change



 int len;


to



 int len = sizeof clientAddr;


Similarly in the client, change



 int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));


to



 int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);





share|improve this answer















In the server, you overlooked that the argument len to recvfrom() is a value-result argument, which before the call you have to initialize to the size of the clientAddr in order to get this address, so change



 int len;


to



 int len = sizeof clientAddr;


Similarly in the client, change



 int n = recvfrom(sockfd,(char * ) recvLine,MAXLINE,0,(struct sockaddr * )&servAddr,sizeof(servAddr));


to



 int len = sizeof servAddr;
int n = recvfrom(sockfd, recvLine, MAXLINE, 0, (struct sockaddr *)&servAddr, &len);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 14:29

























answered Nov 14 '18 at 14:22









ArmaliArmali

7,1851137101




7,1851137101







  • 1





    Thanks!! it worked.

    – cisco
    Nov 14 '18 at 15:35












  • 1





    Thanks!! it worked.

    – cisco
    Nov 14 '18 at 15:35







1




1





Thanks!! it worked.

– cisco
Nov 14 '18 at 15:35





Thanks!! it worked.

– cisco
Nov 14 '18 at 15:35

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301570%2fi-am-trying-to-echo-the-string-from-the-server-to-the-client-with-udp-sockets-bu%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin