LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2004, 10:32 PM   #1
pantera
Member
 
Registered: May 2004
Posts: 80

Rep: Reputation: 15
sockets


i have the following code for receiving a message from udpclient
what i want to do is store all the incoming message into a single variable and then process it......for now i am only getting individual message

iam using C in red hat

int main(int argc, char *argv[]) {

int sd, rc, n, cliLen,i;
struct sockaddr_in cliAddr, servAddr;

FILE *fp;
struct stat buff;

sd=socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}


servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(LOCAL_SERVER_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0) {
printf("%s: cannot bind port number %d \n",
argv[0], LOCAL_SERVER_PORT);
exit(1);
}

printf("%s: waiting for data on port UDP %u\n",
argv[0],LOCAL_SERVER_PORT);


while(1) {


memset(msg,0x0,MAX_MSG);


/* receive message */
cliLen = sizeof(cliAddr);
n = recvfrom(sd, msg, MAX_MSG, 0,
(struct sockaddr *) &cliAddr, &cliLen);

if(n<0) {
printf("%s: cannot receive data \n",argv[0]);
continue;
}

printf("%s: from %s:UDP%u : %s \n",
argv[0],inet_ntoa(cliAddr.sin_addr),
ntohs(cliAddr.sin_port),msg);

}



return 0;

}
 
Old 10-13-2004, 06:43 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
May I just ask why you are using UDP in that case? If you want a streamed connection, TCP is generally favoured except where very fast latency is important.

There's no easy way to do this; UDP is a packeted protocol. There is nothing to stop a packet becoming corrupted, dropped, incorrectly ordered or even duplicated in transmission, especially on a big or wireless network. TCP is there to solve all of these problems.

Edit: This is not to say that UDP is unreliable; it's just that the standard doesn't guarentee reliable data transfer. Btw, duplicated packets occur when the acknowledgement for a packet gets dropped and so the server (or a router) resends the packet. UDP tends to be very reliable on wired LANs (with switches or crossovers rather than hubs to reduce cross-talk) when there's no other traffic, but if you have several continuous streams at once then you may get into trouble.

If you want to do streamed UDP then (at the most basic level) you need to set up an array for each client you want to accept connections from, and then copy the entire message into an element in the appropriate place in the array. This should be based on the order of transmission, not reception; you normally put a sequence number into the TCP packet as a sort-of header to say where in the array it should go. But be very careful not to overwrite the end of the array, as this is a major security headache.

You can either process the messages when the array gets full, when you receive a special packet marked with an appropriate flag in the message (note that, once transmitted, this packet may arrive none or more times), or after a certain timeout. In any case, be careful not to fill the array without processing it as it may force you to drop further packets, and not to have a half-empty array that will never get processed after the client stops transmitting. (The timeout option is generally safest.)

Don't be tempted to use a variable-length structure to hold the incoming packets, like a linked list, because you'll be very vunerable to packet-flooding attacks that will eat up all your memory.

Hope that helps,

— Robert J. Lee

Last edited by rjlee; 10-14-2004 at 06:53 AM.
 
Old 10-13-2004, 01:54 PM   #3
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
i have to disagree. everyone has this big fear of udp just because it unreliable. on our local lan i ran tests and only had a drop rate of 0.1% and actually never had any duplicated messages. if your app is for your own personal use then go with your app. plus if you want the whole message and not have to worry about streaming it and knowing its size then udp is the way to go. i still don't understand your problem though. how do you know how many message to stop to process. your question is a little vague of what you want to process or how many messages before you start processing.
 
Old 11-23-2004, 04:16 PM   #4
daveyroy
LQ Newbie
 
Registered: Sep 2004
Location: Manchester
Distribution: Linux Red Hat 9
Posts: 7

Rep: Reputation: 0
Can any one help? As anyone got any working examples of client/server program that
implements a reliable simplex communications protocol using UDP sockets in C.

Design issues!

1. An acknowledgement messages must be sent back to the
sender from a receiver on successful receipt of a message. Further, if the
sender does not receive an acknowledgement within a timeout then the message
should be sent again. An error message to the effect that the remote
host is down or unreachable should be printed if a message is repeatedly sent
and times out without receiving an acknowledgment. Your sending program should also print out the number of packets that were retransmitted at the end of successfully sending a file.

2. There are a number of design options for the implementation of a reliable
simplex communications protocol and your report should briefy describe 2
possible designs.

3. Attempt to measure the performance of the protocol using the
time command when sending from one PC to another. The maximum packet
size should be adjusted from 1 byte upto 64KByte, when a large file residing on the local
disk filesystem (i.e. /tmp) of approximately 10MByte is sent over the link.
You should repeat each experiment a few times to ascertain if the results are repeatable
 
Old 11-23-2004, 07:17 PM   #5
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
yeah i got a reliable udp library if it didn't sound like you had a hw assignment. so, no you don't get it
 
Old 11-24-2004, 03:32 AM   #6
daveyroy
LQ Newbie
 
Registered: Sep 2004
Location: Manchester
Distribution: Linux Red Hat 9
Posts: 7

Rep: Reputation: 0
Fair enough, but my argument with the Lecturer was I have very little knowledge of programming in C. So how does he expect me to put a program together! Hence I asked him as long as I reference working examples and carry out the test on them. i.e. checking reliability of the protocol would that be ok and he said yes!

Thanks for your responce
 
Old 11-24-2004, 06:28 AM   #7
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
What langugae are you familar with? I think if you understood the fundementals and could design it in java/your lanugage I could help you port it over to C. I understand this situation since I had to go from object oriented java to C, which was a pain.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c and sockets Matir Programming 4 03-09-2005 04:15 PM
Sockets on RH 9.0 rjs2006 Linux - Newbie 1 01-17-2005 10:16 PM
Sockets sibtay Programming 4 10-08-2004 09:33 AM
sockets santiagosilva Linux - Networking 0 02-14-2004 07:09 PM
sockets andox Programming 4 07-05-2003 07:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration