Solved

A question on socket send() and getsockopt()

Posted on 2009-04-11
4
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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