bind() and connect() issues [Newbie] #2

  • Follow


Hi,

I have a client which executes a bind() call before a connect() call. Is 
this ok to do?

Most books I read have the steps for server as 
socket->bind->listen->accept->recv->send->close and for client its 
socket->connect->send->recv->close. I want my client bound to a known port, 
because later I need some other process to connect to my client which brings 
me to my second question - on the client side once I do 
socket->bind->connect, now how do I close this connection? I do not want to 
do close() because later in the client I want to be able to listen to 
incoming requests on the port that I have just bound the client socket to.

Also, what is the effect of executing a recv() call expecting more bytes 
than have been send(). For example if A sends B 10 bytes and B does a recv() 
for 30 bytes, whats going to happen at B - What is the default behaviour in 
this case? Will it block? Will it just read the 10 bytes and proceed?



0
Reply Rookie 10/4/2004 7:41:41 AM

"Rookie" <dominicjoseph@rediffmail.com> writes:

>I have a client which executes a bind() call before a connect() call. Is 
>this ok to do?

Yes; this is what you typically do when you want to connect from
a certain port/range of ports.

>Most books I read have the steps for server as 
>socket->bind->listen->accept->recv->send->close and for client its 
>socket->connect->send->recv->close. I want my client bound to a known port, 
>because later I need some other process to connect to my client which brings 
>me to my second question - on the client side once I do 
>socket->bind->connect, now how do I close this connection? I do not want to 
>do close() because later in the client I want to be able to listen to 
>incoming requests on the port that I have just bound the client socket to.

You can't; you need to make a new socket for that.  stream sockets cannot
be reused as endpoints.

>Also, what is the effect of executing a recv() call expecting more bytes 
>than have been send(). For example if A sends B 10 bytes and B does a recv() 
>for 30 bytes, whats going to happen at B - What is the default behaviour in 
>this case? Will it block? Will it just read the 10 bytes and proceed?

It will read 10 bytes.  Note that since you use tcp, 2 10 byte messages may show
up as one 20 byte message (or a 15 and 5 byte message)

Casper
-- 
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
0
Reply Casper 10/4/2004 8:51:09 AM


1 Replies
401 Views

(page loaded in 0.046 seconds)

Similiar Articles:













7/21/2012 2:53:44 AM


Reply: