Solved

sockets - send(), recv(), TCP, UDP

Posted on 2004-09-11
2
1,993 Views
Last Modified: 2008-03-10
Hi !!

I had a few queries regarding the send(), recv() functions, and was hoping

someone could help me out ... :)


Blocking Sockets
----------------

(1)  I do a send on a TCP Connection. suppose i'm sending 10240 bytes. will

the send return before all the bytes have been sent (will it return with a

value of 2400 for example, meaning that it sent only 2400 bytes), or will it

always return only after all 10240 bytes have been sent ? (it might return

with a smaller value if the socket was closed before everything could be

sent, but will it return with a smaller value in any other scenario?)

(2) for point (1), What about UDP? will the function only return after all

the data has been sent? and what about the recv function in case of udp? does

it receive all the data at one shot (assuming i pass in a sufficiently large

buffer)?

(3) suppose in one thread, i call send with a huge amount of data to be sent

(e.g. 2 mb) (TCP socket). while this send is progressing, i call send on the

same socket from another thread. will any error be thrown? or will the data

in the second thread be sent after the send returns in the first thread?

(4) for point (3), what will happen if i use a UDP socket instead?

(5) i'm on a 10/100 mbps LAN. I will be calling send quite frequently, say

once every 4 seconds) If i use UDP instead of TCP, will it be faster or

slower? (all the destination PCs are on the same LAN, and in case of TCP, i

will be creating the socket and connect()-ing only once, during

initialization, and not every time i need to do a send.) (i know, udp is less

reliable, but would this unreliability be very much pronounced on a 10/100

mbps LAN?)

thanks :)

muskad202
0
Comment
Question by:muskad202
2 Comments
 
LVL 1

Accepted Solution

by:
bsnh99 earned 50 total points
ID: 12036773
1. For TCP, it should generally send all of the data without returning a short count. You should still check the return value to make sure you didn't get some kind of error (e.g. if remote closes connection before data finishes sending).

2. No. UDP is unreliable. If you try to send too much data at once, you'll overwhelm the socket and it will almost certainly drop some of the data. I think (not sure on this one), that if recv() gets the UDP packet, it will contain the entire packet. Note that it is possible that recv() will miss a packet, and it is possible for recv() to get packets out of order. Your protocol should handle these possibilities.

3. I'd suggest using a separate socket for each thread. You don't say what platform you're on, and socket libraries aren't necessarily thread-safe. You could check the documentation for your socket library.

4. Same as #3.

5. The decision of UDP vs TCP can depend on a number of variables. What kind of protocol are you using? Will you have to retransmit UDP packets if they are dropped? If you can describe the nature of your application a little more, I might be able to suggest one over the other.

Hope this helps.
0
 
LVL 2

Author Comment

by:muskad202
ID: 12037104
thanks :)
0

Featured Post

New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
libcurl and C++ - Post JSON Data 8 1,194
C hashtable library 3 82
Detect CR LF to each line 12 152
C Programming - If Statement 8 72
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now