Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-25-2005, 11:15 AM   #1
Registered: Nov 2003
Posts: 49

Rep: Reputation: 15
consecutive 'write's or 'send's on a socket

Hi all,
I have a server and a client. The server has done a 'socket', 'bind', 'listen' and an 'accept'. The client has done a 'socket', 'bind' and a 'connect' to the server address. When i do a 'send' of some buffer like "Hello World" to the server, the server 'recv's correctly. But when i do a consecutive 'send's, the server does not receive the second buffer properly.

    // Client    
    send (sockd, "Hello World1", sizeof (sendbuff), 0);
    send (sockd, "Hello World2", sizeof (sendbuff), 0);
    close (sockd);
    // Server
    recv (newsockd, newrecvbuff, sizeof (recvbuff), 0);
    printf ("recvd buffer = %s\n", recvbuff);
    recv (newsockd, newrecvbuff, sizeof (recvbuff), 0);
    printf ("recvd buffer = %s\n", newrecvbuff);
    close (newsockd);
The first 'printf' prints correctly, but the second one prints nothing. Also, if i put a 'sleep' of say 5 seconds between the first send and second send in the client, i can see the expected results. Why is this?
Old 09-25-2005, 03:43 PM   #2
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Well .. if I do it on my machine .. it works fine without the need of "sleep" .. your problem is you're making assumptions on the data transfer between the client and the server (which is a "no-no") . When your server perfroms recv(); you have no way of knowing you'll even get half of "hello world 1" .. you might get a "h" .. you might get a "hell" .. you might get the whole message etc. .. but there's no certanty as to which of these will happen ..
Old 09-26-2005, 04:51 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
why send twice?
you are doing something that doesn't make sense.
client server is a two way conversation,

client speaks, server receives
server replies, client receives

sending twice doesn't make sense without an acknowledgement.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
socket: Socket type not supported monindra Fedora - Installation 4 01-14-2008 08:25 AM
getpeername: Socket operation on non-socket KevStA Linux - General 5 04-26-2006 03:11 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 08:28 PM
Add consecutive numbers to a Chinese file tcma Linux - General 0 10-19-2004 04:25 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration