Solved

Sockets Question

Posted on 2004-09-18
8
230 Views
Last Modified: 2010-04-15
I have a server/client setup communicating on the same port.

if I call from the server a send(), but the client gets to the recv() AFTER the send is called, what happens to the data sent? is it lost? if not, how do I get the data I need?
0
Comment
Question by:jameswalt
8 Comments
 

Author Comment

by:jameswalt
ID: 12092350
also communicating on the same socket.
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 20 total points
ID: 12092415
The message is "queued" in memory (either a buffer in the TCP/IP network stack
or in the socket library) waiting to be received.
0
 

Author Comment

by:jameswalt
ID: 12092432
so when the recv call is hit, it will get the data, right?

in otherwords, when I call a send, as long as the data actually gets across the network and to the buffer, the recv. is guaranteed to get the data.

is this correct?
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 45 total points
ID: 12092958
The data should be available by recv eventually.

There's no absolute guarantee that it will get there: the connection could be dropped.

Sends are buffered as well and network / system conditions can certainly delay recv
getting the information.

It may also take multiple times that blocks of the sent information comes in and multiple
calls to recv to get all the data.

You need to check recv's return value to find out how much data was read, if that's what
you're asking (it's not a certain thing)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:jameswalt
ID: 12093285
alright here's my scenario:

I'm getting a machine to send a hostname of another connected machine.

so machine A sends the hostname of machine B to machine C.

the sending is simple...
send(hostname on socket s, strlen(hostname)...)

the receiving gives me something i dont know how to receive correctly.

len = recv(s, buf, 32, 0)
buf[len] = '\0'
strcopy(thehost, buf)

thehost looks something like this:  "hostname.domainhostname.domainhostname.domain"
where the hostname and domain are copied multiple times into thehost string var.

How do I correct this problem?
0
 

Author Comment

by:jameswalt
ID: 12093512
This is a bit urgent for me, please let me know if you have an answer. Thanks!
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12093776
>>  len = recv(s, buf, 32, 0)
>> buf[len] = '\0'
Perhaps try:

recv(s, buf, 32, MSG_WAITALL)

from the Linux Programmer's Manual:
"This  flag  requests  that  the  operation  block until the full request is satisfied.  However,
 the call may still  return  less data  than  requested if a signal is caught, an error or discon-
nect occurs, or the next data to be received is of  a  different type than that returned."

In particular,  if recv fails for some reason, and it may return an error on a non-blocking socket
if the data isn't quite ready to be read yet, it may return -1

buf[-1] = '\0'   would not cut the buffer down at all

temporarily use a check to see if that's what is happening

len = recv(s, buf, 32, 0)
if (len == -1) {
       perror("recv");
}


if that's not what is happening, then I don't see why text should be repeating

You may need to use select or poll to wait for the socket to be readable
or if you are, perhaps the problem is with your select loop on the receiving end...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12099497
Can you post some more source code ... I suspect that you might be sending twice in a loop or recving twice in a loop
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

707 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

14 Experts available now in Live!

Get 1:1 Help Now