Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
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 …
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.:
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…
Suggested Courses

705 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