LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-25-2005, 10:15 AM   #1
ananthbv
Member
 
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.

Code:
    // Client    
    send (sockd, "Hello World1", sizeof (sendbuff), 0);
    send (sockd, "Hello World2", sizeof (sendbuff), 0);
    close (sockd);
Code:
    // 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, 02:43 PM   #2
introuble
Member
 
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, 03:51 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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
etc
etc

sending twice doesn't make sense without an acknowledgement.
 
  


Reply


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 07:25 AM
getpeername: Socket operation on non-socket KevStA Linux - General 5 04-26-2006 02:11 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
Add consecutive numbers to a Chinese file tcma Linux - General 0 10-19-2004 03:25 PM


All times are GMT -5. The time now is 11:38 PM.

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