Solved

Sockets Question

Posted on 2004-09-18
8
231 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
Why won’t your email signature format correctly?

Struggling to get your corporate email signatures to format correctly? Does the logo keep resizing? Is the text appearing too big? What can you do to prevent this? Find out how you can save your signatures today.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 28
libcurl and C++ - Post JSON Data 8 1,182
C dll call freezes 5 93
C Programming - If Statement 8 72
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…
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 opening and writing to files 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.

919 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

22 Experts available now in Live!

Get 1:1 Help Now