Comments are available to members only. Sign up or Log in to view these comments.
Main Topics
Browse All TopicsHi
I have written the following UDP echo client / server code using recvmsg and sendmsg. But unfortunately no words are echoed back. How should I modify the code
//UDP echo client code
#include "unp.h"
#include <stdio.h>
#include <sys/socket.h>
#undef MAXLINE
#define MAXLINE 256
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc != 2)
err_quit("usage: udpcli <IPaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n, i;
char sendline[MAXLINE], recvline[MAXLINE + 1];
struct msghdr msg;
struct iovec iov[100],iov1[1];
while (Fgets(sendline, MAXLINE, fp) != NULL)
{
msg.msg_name = (void *)pservaddr;
msg.msg_namelen = servlen ;
iov1[0].iov_base = recvline;
iov1[0].iov_len = MAXLINE;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
sendmsg(sockfd, &msg, 0);
n=recvmsg(sockfd, &msg, 0);
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
}
}
//UDP echo server code
#include "unp.h"
#include <stdio.h>
#include <sys/socket.h>
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr));
}
void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char recvline[MAXLINE];
struct msghdr msg;
struct iovec iov[100],iov1[1];
for ( ; ; ) {
msg.msg_name = (void *)pcliaddr;
msg.msg_namelen = clilen;
iov1[0].iov_base = recvline;
iov1[0].iov_len = MAXLINE ;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
n = recvmsg(sockfd ,&msg, 0);
sendmsg(sockfd, &msg, 0);
}
}
Error: The input I give isn't being echoed.
Pls help and let me know what modifications I need to make to make the program run.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: grg99Posted on 2007-06-27 at 08:40:59ID: 19373340
Comments are available to members only. Sign up or Log in to view these comments.