Tuesday, 15 May 2012

Listening TCP Socket

TCP is a connection-oriented transport protocol which means that end points have to establish a connection prior to sending any (payload) data. Server opens one socket only for listening for incoming connection requests that come from clients. When placing a socket into a listening state, a maximum number of pending incoming connections is set. Those connections are waiting to be accepted and when backlog gets full no new connections are accepted.

Operations on listening socket are:
Listening socket never gets into connected state and shutdown() called on it yields socket error 10057 - WSAENOTCONN (Socket is not connected).

Here is the timeline of one simple dialogue between the client and the server:

Client Server
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) sockListen= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
- sockaddr_in sSockAddr;
sSockAddr.sin_family = AF_INET;
sSockAddr.sin_addr.s_addr = inet_addr("192.168.0.24");
sSockAddr.sin_port = htons(43501);

bind(sockListen, (SOCKADDR*)&sSockAddr, sizeof(sSockAddr));
- listen(sockListen, MAXPENDING)
struct sockaddr_in sockAddrRemoteNode;
memset(&sockAddrRemoteNode, 0, sizeof(sockAddrRemoteNode));
sockAddrRemoteNode.sin_family = AF_INET;
sockAddrRemoteNode.sin_addr.s_addr = inet_addr("192.168.0.24");
sockAddrRemoteNode.sin_port = htons(43501);

connect(sock, (struct sockaddr*) &sockAddrRemoteNode, sizeof(sockAddrRemoteNode));
struct sockaddr sockAddrClt;
int nSockAddrLen = sizeof(sockAddrClt);

sockClient = accept(sockListen, (struct sockaddr*) &sockAddrClt, &nSockAddrLen);
send(sock, buffSend, SEND_BUFF_SIZE, 0) int recvPacketLen = recv(sockClient, buffRecv, RECV_BUFF_SIZE, 0)
shutdown(sock, SD_BOTH) shutdown(sockClient, SD_BOTH)
closesocket(sock) closesocket(sockClient)
- closesocket(sockListen)

2 comments:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com

Selva said...

you have written an excellent blog.. keep sharing your knowledge...
JMeter Training in Chennai
JMeter Online Training
JMeter Training Institute in Chennai