Solved

A question on socket send() and getsockopt()

Posted on 2009-04-11
4
304 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 100 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 400 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 107
noX challenge 17 77
C# code editing and collaboration 3 49
C++ mouse_event mouse look 7 27
This article will show, step by step, how to integrate R code into a R Sweave document
Creating an OSPF network that automatically (dynamically) reroutes network traffic over other connections to prevent network downtime.
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now