public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* how to read one udp packet with more than one recvfrom() calls?
@ 2010-11-08  7:08 ranjith kumar
  2010-11-08  7:12 ` Changli Gao
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ranjith kumar @ 2010-11-08  7:08 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 582 bytes --]

Hi,

I  have implemented client and server programs using udp
protocol(files are attached).
UDP packet size is 500bytes.

I want to read these 500bytes in two calls to recvfrom(). First time
reading 100bytes and second time 400bytes.
How to do this?

When I tried to change the third argument of recvfrom(size_t len),
from 500 to 100, first 100bytes are read correctly.
But when I call recvfrom() second time with len=400, it is reading the
first 400bytes of "next udp packet".
Why? Isn't it possible to read one udp packet in two calls to
recvfrom()/read()????

Thanks in advance.

[-- Attachment #2: client.c --]
[-- Type: application/octet-stream, Size: 1169 bytes --]

#include<stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#define BUFLEN 500
#define PORT  5000
#define NPACK  5

 
#define SRV_IP "107.109.38.32"
 /* fprintf(stdout,), #includes and #defines like in the server */

 int main(void)
 {
   struct sockaddr_in si_other;
   int s, i, slen=sizeof(si_other);
   char buf[BUFLEN];

   if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
     fprintf(stdout,"socket");

   memset((char *) &si_other, 0, sizeof(si_other));
   si_other.sin_family = AF_INET;
   si_other.sin_port = htons(PORT);
   if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
     fprintf(stderr, "inet_aton() failed\n");
     exit(1);
   }

   for (i=0; i<NPACK; i++) {
     printf("Sending packet %d\n", i);
     sprintf(buf, "This is packet %d\n", i);
//	write(s,buf,BUFLEN);
     if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
      fprintf(stdout,"sendto()");
   }

   close(s);
   return 0;
 }


[-- Attachment #3: server.c --]
[-- Type: application/octet-stream, Size: 1083 bytes --]

#include<stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#define BUFLEN 500
#define PORT  5000
#define NPACK  5



void diep(char *s)
{
	perror(s);
	exit(1);
}

int main(void)
{
	struct sockaddr_in si_me, si_other;
	int s, i, slen=sizeof(si_other);
	char buf[BUFLEN];

	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
		diep("socket");

	memset((char *) &si_me, 0, sizeof(si_me));
	si_me.sin_family = AF_INET;
	si_me.sin_port = htons(PORT);
	si_me.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(s, &si_me, sizeof(si_me))==-1)
		diep("bind");

	for (i=0; i<NPACK; i++) {
		if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
			diep("recvfrom()");
//	read(s,buf,BUFLEN);
		printf("Received packet from %s:%d\nData: %s\n\n", 
				inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
	}

	close(s);
	return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-11-09  5:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-08  7:08 how to read one udp packet with more than one recvfrom() calls? ranjith kumar
2010-11-08  7:12 ` Changli Gao
2010-11-08  7:51 ` Michael Tokarev
2010-11-08 18:31 ` Rick Jones
2010-11-09  5:35 ` Varun Chandramohan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox