[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Sockets Question

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
jameswalt
Asked:
jameswalt
2 Solutions
 
jameswaltAuthor Commented:
also communicating on the same socket.
0
 
brettmjohnsonCommented:
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
 
jameswaltAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MysidiaCommented:
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
 
jameswaltAuthor Commented:
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
 
jameswaltAuthor Commented:
This is a bit urgent for me, please let me know if you have an answer. Thanks!
0
 
MysidiaCommented:
>>  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
 
sunnycoderCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now