?
Solved

A question on socket send() and getsockopt()

Posted on 2009-04-11
4
Medium Priority
?
319 Views
Last Modified: 2013-11-13
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.
0
Comment
Question by:heyday2004
  • 2
4 Comments
 
LVL 6

Expert Comment

by:peter41
ID: 24124679
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
 

Author Comment

by:heyday2004
ID: 24126868
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 400 total points
ID: 24136217
>> 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
 
LVL 6

Accepted Solution

by:
peter41 earned 1600 total points
ID: 24136233
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 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