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
689 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
ID: 11688634
URGENT PLZ..............
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 11691031
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
ID: 11691046
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

840 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