?
Solved

Sockets Question

Posted on 2004-09-18
8
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

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

Accepted Solution

by:
brettmjohnson earned 80 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 180 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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

771 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