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
Solved

Sockets Question

Posted on 2004-09-18
8
234 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

856 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