Send Function
Int send (socket S, const char far * Buf, int Len, int flags );
Both the client and server applications use the send function to send data to the other end of the TCP connection.
The client program generally uses the send function to send requests to the server, while the server uses the send function to send responses to the client program.
The first parameter of this function specifies the sender socket descriptor;
The second parameter specifies a buffer for storing the data to be sent by the application;
The third parameter specifies the number of bytes of data to be sent;
The fourth parameter is usually set to 0.
Only the execution process of the send function for Synchronous socket is described here. When this function is called, send first compares the length of the data to be sent Len and socket sSending BufferIf Len is greater than the sending buffer length of S, this function returns socket_error; if Len is less than or equal to the sending buffer length of S, then, send checks whether the protocol is sending data in the sending buffer of S. If so, wait for the Protocol to finish sending the data, if the Protocol has not started sending data in the sending buffer of S or there is no data in the sending buffer of S, send will compare the remaining space in the sending buffer of S and Len, if Len is larger than the size of the remaining space, send will wait for the Protocol to finish sending data in the s sending buffer, if Len is smaller than the size of the remaining space, send only copies the data in the Buf to the remaining space (note that it is not sending the data in the sending buffer of S to the other end of the connection, it is transmitted through the Protocol,Send only copies the data in the Buf to the remaining space in the sending buffer zone of S.). If the send function successfully copies the data, the actual number of bytes of copy is returned. If an error occurs during sending data copy, the send function returns socket_error; if the network is disconnected when sending data while waiting for the Protocol to send data, the send function also returns socket_error.
Note that the send function successfully copies the data in the Buf to the remaining space of the s sending buffer, and then returns the data. However, the data is not necessarily uploaded to the other end of the connection immediately.. If a network error occurs during subsequent transmission, the next socket function will return socket_error. (Each socket function except send must wait for the data in the socket sending buffer to be transmitted by the protocol before execution. If a network error occurs while waiting, then the socket function will return socket_error)
Note: In Unix systems, if the network is disconnected when sending data while waiting for the send protocol, the process that calls send receives a sigpipe signal. The process processes the signal by default and terminates the process.
Recv Function
Int Recv (socket S, char far * Buf, int Len, int flags );
Both the client and server applications use the Recv function to receive data from the other end of the TCP connection.
The first parameter of this function specifies the receiver socket descriptor;
The second parameter specifies a buffer that is used to store the data received by the Recv function;
The third parameter specifies the length of the Buf;
The fourth parameter is usually set to 0.
Only the execution process of the Recv function for Synchronous socket is described here. When the application calls the Recv function, the Recv waits for the data in the s sending buffer to be transmitted by the Protocol. If the Protocol encounters a network error when sending data in the s sending buffer, then the Recv function returns socket_error. If no data is in the sending buffer of s or the data is successfully sent by the protocol, the Recv first checks the receiving buffer of socket S, if there is no data in the s receiving buffer or the Protocol is receiving data, the Recv waits until the Protocol receives the data. When the Protocol receives the data, the Recv function copies the data in the s receiving buffer to the Buf (note that the data received by the protocol may be larger than the length of the Buf, in this case, you need to call the Recv function several times to copy the data in the s receiving buffer.The Recv function only copies data, and the protocol is used to actually receive data.), The Recv function returns the actual number of bytes of copy. If a Recv error occurs during copy, socket_error is returned. If the Recv function is interrupted while waiting for the Protocol to receive data, 0 is returned.
Note: In Unix systems, if the network is disconnected when the Recv function is waiting for the Protocol to receive data, the process that calls the Recv will receive a sigpipe signal, the process processes this signal by default.
UseInet_addr ()The program converts an I P address in the form of "132.241.5.10" to an unsigned integer.
Ina. sin_addrs_addr = inet_addr ("132.241.5.10 ");
If an error occurs, the inet_addr () program returns-1.
You can also call inet_ntoa () to convert the address into numbers and periods:
Printf ("% s", inet_ntoa (INA. sin_addr ));
This will print the I P address. It returns a pointer to a string.
Socket ()
We use the system to call socket () to obtain the file descriptor:
# Include
# Include
Int socket (INT domain, int type, int Protocol );
Set the first parameter domain to "af_inet ".
The second parameter is the set interface type: sock_dgram.
The third parameter is set to 0.
The system calls socket () and returns only one set of interface descriptors. If an error occurs,-1 is returned.
BIND ()
Once you have an interface set, the next step is to bind the interface set to a port on the local computer. However, it is unnecessary if you only want to use connect.
The following is how the system calls BIND:
# Include
# Include
Int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen );
The first parameter sockfd is the set of interface file descriptors returned by the socket () call.
The second parameter my_addr is a pointer to the sockaddr data structure. The data structure sockaddr contains information about your address, port, and IP address.
The third parameter addrlen can be set to sizeof (struct sockaddr ). The following is an example:
# Include
# Include
# Include
# Define myport 3490
Main ()
{
Int sockfd;
Struct sockaddr_in my_addr; // describes the sock address structure.
Sockfd = socket (af_inet, sock_stream, 0);/* basically build UDP socket, it is best to perform some checks */
My_addr.sin_family = af_inet;/* sets the Protocol set based on Internet Protocol */
My_addr.sin_port = htons (myport); // port number
My_addr. sin_addr.s_addr = inet_addr ("132.241.5.10"); // converts a string to a standard address format.
Bzero (& (my_addr.sin_zero), 8);/* zero the rest of the struct */
/* Don't forget your error checking for BIND ():*/
BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr ));
// Bind the listening process to the socket
If an error occurs, BIND () also returns-1.
If you use the connect () system call, you do not have to know the port number you are using. When you call connect (), it checks whether the set interface is bound. If not, it will allocate an idle port.
Sendto () and recvfrom ()
Because the datagram set interface is not connected to a remote host, we must first provide the destination address before sending data packets. Please refer
Int sendto (INT sockfd, const void * MSG, int Len, unsigned int flags,
Const struct sockaddr * To, int tolen );
Except for the two parameters, the other parameters are the same as when the system calls s e n d. The t o parameter is a pointer to the data structure s o c k a d r that contains the destination I P address and port number. The t o l e n parameter can be set to sizeof (struct sockaddr ).
The system calls sendto () to return the actual number of bytes sent. If an error occurs,-1 is returned.
The usage of the system call recvfrom () is also very similar to that of r e c v:
Int recvfrom (INT sockfd, void * Buf, int Len, unsigned int flags
Struct sockaddr * From, int * fromlen );
Sockfd: Description
Buff: pointer to the input buffer
Nbytes: Read byte size
Flag: 0
From: Peer protocol address
Addrlen: Protocol address length of the peer
Function return value: the length of the read data, which can be 0.
The from parameter is a pointer to the sockaddr Data Structure on the local computer that contains the source I P address and port number. The fromlen parameter is set to sizeof (struct sockaddr ).
The system calls recvfrom () to return the number of received bytes. If an error occurs,-1 is returned.
Close () and Shutdown ()
You can use close () to call the set interface file descriptor for closing the connection:
Close (sockfd );
In this way, no read/write operations can be performed on this interface.
You can use the system call Shutdown () to gain more control. It allows you to cut off the communication in a certain direction, or cut off the communication between the two parties:
Int Shutdown (INT sockfd, int how );
The first parameter is the set interface file descriptor that you want to disconnect communication. The value of h o w in the second parameter is as follows:
0-further specified es are disallowed
1-further sends are disallowed
2-further sends and Es are disallowed (like close ())
If Shutdown () succeeds, 0 is returned. If it fails,-1 is returned.