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
684 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

785 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