Solved

There is a problem with my program in which am converting a UDP client and UDP server which uses sendto and recvfrom functions to UDP client and server which uses sendmsg and recvmsg fucntions ...

Posted on 2004-08-01
3
665 Views
Last Modified: 2008-02-01
hi guys,

  Below is the program for echo server which uses UDP.

 Actual program which used recvfrom and sendto functions worked fine and i got the output fine....

 But when am using sendmsg and recvmsg instead of sendto and recfrom, am getting a problem . I can echo only the first message and from second message, it cant be echoed. what is the problem with my code.... can u guys refer to it an tell me

newudpclient.c
=============
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#define MAXLINE 256



// THE COMMENTED LINES IN THE BELOW PROGRAM ARE WHEN I USED SENDTO AND RECVFROM FUNCTIONS. IT WORKED FINE FOR THAT CODE..

void
dg_cli(FILE *fp, int sockfd, const struct sockaddr *pservaddr, socklen_t servlen)
{


  int n, i;
  char sendline[MAXLINE], recvline[MAXLINE+1];
  struct msghdr sndmsg,rcvmsg;
  struct iovec iov[100],iov1[1];

  iov1[0].iov_base           = recvline;
  iov1[0].iov_len            = MAXLINE;
  rcvmsg.msg_iov             = iov;
  rcvmsg.msg_iovlen          = 1;
  rcvmsg.msg_name            = (void *)pservaddr;
  rcvmsg.msg_namelen         = servlen ;


  while(fgets(sendline,MAXLINE,fp)!=NULL)
    {
      printf("%s", sendline);
      printf("bye!");
      iov[0].iov_base          = sendline;
      iov[0].iov_len           = MAXLINE;
      sndmsg.msg_iov           = iov;
      sndmsg.msg_iovlen        = 1;
      sndmsg.msg_name          = (void *)pservaddr ;
      sndmsg.msg_namelen       = servlen;

      sendmsg(sockfd, &sndmsg, 0);
      printf("sendmsg successful");
     
      sleep(10);
      n=recvmsg(sockfd, &rcvmsg, 0);
      for(i=0; i<rcvmsg.msg_iovlen; i++)
      {
        fputs((char*)rcvmsg.msg_iov[i].iov_base,stdout);
      }
    }
 
 
 
 
  //  int             n;
  //  char    sendline[MAXLINE], recvline[MAXLINE + 1];
 
  //  connect( sockfd, (struct sockaddr *) pservaddr, servlen);
 
  //  while (fgets(sendline, MAXLINE, fp) != NULL)
  //    {
     
  //      write(sockfd, sendline, strlen(sendline));
     
  //      n = read(sockfd, recvline, MAXLINE);
     
  //      recvline[n] = 0;        /* null terminate */
  //      fputs(recvline, stdout);
  //    }
}



int
main(int argc, char **argv)
{
  int                     sockfd;
  struct sockaddr_in      servaddr;
 
  if (argc != 2)
    perror("usage: udpcli <IPaddress>");
 
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(9786);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
 
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 
  dg_cli(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
 
  exit(0);
}

newudpserver.c
============

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#define  MAXLINE 256


void dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{

  //---------------------------------------------------------------------------------
  int n, i=0;
  socklen_t len;
  char recvline[MAXLINE];
  struct msghdr sndmsg,rcvmsg;
  struct cmsghdr control;
  struct iovec iov[100],iov1[1];

  for(;;)
    {
      len=clilen;
     
      iov1[0].iov_base         = recvline;
      iov1[0].iov_len          = MAXLINE;
      rcvmsg.msg_iov             = iov;
      rcvmsg.msg_iovlen          = 1;
      rcvmsg.msg_name            = pcliaddr;
      rcvmsg.msg_namelen         = clilen;

     
      n = recvmsg(sockfd ,&rcvmsg, 0);
     
      iov1[0].iov_base         = recvline;
      iov1[0].iov_len          = MAXLINE;
      rcvmsg.msg_iov             = iov;
      rcvmsg.msg_iovlen          = 1;
      rcvmsg.msg_name            = pcliaddr;
      rcvmsg.msg_namelen         = clilen;

     
      sendmsg(sockfd, &rcvmsg, 0);
     
    }
 
 
  //=================================================================================




 
  //  int n;
  //  socklen_t len;
  //  char mesg[MAXLINE];
 
  //  for( ; ; )
  //    {
  //      len = clilen;
     
  //      n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
     
  //      sendto(sockfd, mesg, n, 0, pcliaddr, len);
     
  //    }
}



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(9786);
 
  bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
 
  dg_echo(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
}



to compile :
=========

cc -o newudpclient newudpclient.c -lnsl -lrt
cc -o newudpserver newudpserver.c -lnsl -lrt

to run:
=====

./newudpserver

./newudpclient 127.0.0.1


ouput will be:=

if i type hi, hi will be displayed, but when i type more message like hello, it just gets stuck there.....

example:==

hi
hi
hello





thats it..... nothin happens


can u guys help me out in this....

thanks
aman
0
Comment
Question by:amankhan
  • 2
3 Comments
 

Author Comment

by:amankhan
Comment Utility
URGENT PLZ..............
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
You are not being able to send even the first message ... The echo you see on the screen is due to the printf and not server echo

  while(fgets(sendline,MAXLINE,fp)!=NULL)
    {
      printf("%s", sendline);

you are not checking return value of any system call ... A single error in setting a parameter in a struct can make a call fail which you would never know of .... Add error checking to the program, check return values from every system call and you should be able to spot the error in no time
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Ok ... I added the error checking in your code here are the errors

recvmsg: Bad address
sendmsg: No buffer space available

correcting them now should be easy
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now