Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
739 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

876 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