Solved

A question on socket send() and getsockopt()

Posted on 2009-04-11
4
313 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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