Win socket programming learning and understanding

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Socket is a means of communication between different processes. Just like a phone call is a way of communication between friends is the same. Personal understanding: the so-called "communication", data is sent between. Someone to understand socket is a means of communication between different computers, this is not exact. The two process, whether it is running on the same computer, or run on different computers, can communicate through socket technology.
The use of socket sockets requires network support, so socket is generally used to communication between different machines, and if the two processes on the same computer communication, usually use more efficient shared memory technology to achieve.
If I have a process for socket communication with a process on another computer, that in this process of how to specify another process? There also needs to be said about another concept -- port, if the operating system to a house, the port is the window of the house, is a system with internal external communication channel.

In the socket implementation, we do not specify another process, butThe specified send message or port number for receiving messages. For example now process A to process B messages, we willThe port N messages to a process running on the B computer, and the process of B was monitored port N, this process B will receive the data sent by A process, The same process B also sends a message to the port, the process A can also from the port to receive and process B data sent, of course, This requires the client and the server is an agreement on the port number, joint operation with a port. If the client sends a message to the port N1, and server monitoring is the port N2, the communication must not be successful. The port number is 65535, no bigger than this, but in our own procedures as far as possible not to use less than the port number 1024, Less than 1024 of the port are used by system, such as the 23 used by telnet.

Below are the core function is important:

(1) SOCKET socket(int af, int type, int protocol);

Both the client and server side, this function must be used, but also the first to use. This function is to tell the system, ready for me in an socket channel, I and other process communication.. The function returns an value it is important to note, it said, the socket channel system ready for us, will be used for each socket function in the future, If the value is equal to SOCKET_ERROR, said; function execution failed. Function parameters: PF_INET, we respectively to SOCK_STREAM and IPPROTO_TCP.

(2)int bind(SOCKET s, const sockaddr *addr, int namelen);
This function is only the server using
, Role is to bind with a socket channel. CanTo use this function returns value judgment result how, if equal to SOCKET_ERROR, it is failed.. The first parameter is the s, socket () function returnsvalue, In the structure of addr, we have given a port number, Namelen is equal to the structure size of SOCKADDR.

(3)int listen(SOCKET s, int backlog);

This function is only the server using, Role is to monitor the port. Returns value as with the bind function.

(4)int accept(SOCKET s, sockaddr *addr, int *addrlen);
This function is only the server using
, Role is to connect the response to the client.. Returns value as with the bind function.

(5)int connect(SOCKET s, const sockaddr *name, int namelen);
This function only client applications
, AsWith the establishment of a port is connected to the client and a computer. Returns value as with the bind function. The first parameter is the s, socket () function returns the value name; in the structure, we have given a port number and purpose machine name; namelen is equal to the structure size of the SOCKADDR.

(6)

int send(SOCKET s, char *buf, int len, int flags);
int recv(SOCKET s, char *buf, int len, int flags);
The two function is to send data and receive data, The client and server programs can use, Which route which receive needless to say? Ha-ha.
From the function returns value can check the function execution success. Parameters of buf is the pointer to send or receive data, len is the data length. We give a 0 to flags(in fact, I do not know the specific meaning).


The use of these functions in the Linux system can be successfully established a socket communication link, But if in the windows system, but also use another function:
int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
In the windows system, the first to perform this function, so take this function on the socket () function in front of.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Avivi at December 24, 2013 - 4:15 AM