A question on socket send() and getsockopt()

I have the source code of a project, after creating two cliient sockets socket1, socket2 to connect to the server, the send and recv parts in the source code are listed below:

1. In SendMessages()
-- getsockopt(socket1, SOL_SOCKET, SO_SNDBUF, (char*)&bufSize, &optionSize )
-- ret = send( socket2, &mSendbuf[prev_lensbuf - lensbuf], bufSize < lensbuf ? bufSize : lensbuf, 0) ;

 
2. In ReceiveMessages
-- getsockopt (socket2, SOL_SOCKET, SO_RCVBUF, (char FAR *) &rbuflen, &gar ) || rbuflen <= 0 )
-- nchars = recv(socket2, (char FAR *)mBuf, ( ( mend + rbuflen ) <= MAXRECVBUF ) ? rbuflen : ( MAXRECVBUF - mend ), 0 );


But in the project description, it says this program always uses socket1 for sending and socket2 for receiving.

My question is, in SendMessages(), it uses socket1 in getsockopt(...), but indeed, in send(), it uses socket2, why it says SendMessages() is always using socket1? Is socket1 really used in SendMessages()? Thanks for any hint.
heyday2004Asked:
Who is Participating?
 
peter41Connect With a Mentor Commented:
I dont believe that there can be better performance when two separate sockets are used (one for receiving and second one for sending) in TCP connections.

Communication through socket is natively full-duplex (of course network medium can be half-duplex, but I mean system functions and structures relating to SOCKET descriptor, and calling send/recv from multiple threads). Every socket has two internal buffers - one is for received data and second one for sent data, so after socket is created, both are automatically prepared for transferring data in both directions.
It is only on programmer if he will use socket only for receiving or sending data.

But depending on how is your C++ classes organized (or C structured code),
there can be suitable to create separate sockets (one for using in send() and second one for using in recv() )  and put such SOCKETs like class members into two separate class instances,
maybe this is your case...


0
 
peter41Commented:
In 2. - there are syntax error after getsockopt(), there is "..|| rbuflen <= 0 )" and there should be ";"

One socket can be used for both: sending and receiving or separate two sockets can be used.
But more sockets can be bound to one address only if SO_REUSE option is set by setsockopt().

It seems that writer of this code wanted originaly to implement what he wrotes in doc, but for some reason he changed it and used only socket2.

In my socket programming I always used the same socket for sending and receiving.
0
 
heyday2004Author Commented:
Thanks a lot. Could you let me know what you don't use separate sockets for send and recv, isn't it correct that use separate sockets for send and recv will have better performance than using send and recv in the same socket?
0
 
Infinity08Connect With a Mentor Commented:
>> isn't it correct that use separate sockets for send and recv will have better performance than using send and recv in the same socket?

What makes you think that ?
Connections over a socket are bi-directional, and are designed to allow traffic in both directions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.