Basic concepts of network programming:
1. Basic Structure
Struct sockaddr {
Unsigned short sa_family;
Char sa_data [14];
};
Struct sockaddr_in {
Short int sin_family;
Unsigned short int sin_port;
Struct in_addr sin_addr;
Unsigned char sin_zero [8];
};
Struct in_addr {
Unsiged long s_addr;
};
Note: The sockaddr structure is used for function parameters. In fact, the internal data of sockaddr_in is the same as that of sockaddr, but it is defined differently. The sockaddr_in structure is defined only for programming to fill in the address family. The IP address and port are convenient, when you call a socket function, you must forcibly convert the type to sockaddr. In this case, it is estimated that the sockaddr structure is defined earlier, so it cannot be discarded.
2. Basic conversion functions
* Network byte sequence: The network adopts the big tail mode and inter386 adopts the Small Tail mode.
* Network digital conversion
Htos host to network short
Hotl host to network long
Ntos network to host short
Ntol network to host long
* Network Address Translation
Inet_addr () converts a string-type IP address to an unsigned long int
Inet_ntoa () converts an IP address number to a string
3. Basic socket Functions
Only a few of the important functions are described here.
Socket (ip_family, data_type, protocol );
Bind (socket, struct sockaddr, len );
Specify a local port for communication. Use the local ip address and port to fill the structure.
Connect (socket, struct sockaddr, len );
Specify an unused port, call bind internally for binding, and fill the structure with remote ip address and port
Listen (socket, backlog );
The maximum number of unprocessed connection request queues in the backlog.
Accept (listen_socket, out struct sockaddr, len );
Accept takes out the listen function and puts it into the first message in the queue for processing. Then, it returns the management socket of the message.
Note: on the server side, the listen function queues requests sent from function connect on the client side and submits them to accept for processing. Therefore, the function accept returns the Client Communication socket and the client IP address, communication Port and other information; on the client side, the connect function can specify an unused port internally and bind it to communicate with the server.
If the accept cannot receive the request, it will be blocked.
If an accept request is received, the three handshakes of TCP are completed, and data can be sent and accepted using the send and recv functions.
4. Sample Code
Client code:
# Include
# Include
# Include
# Pragma comment (lib, "ws2_32.lib ")
Int main ()
{
WSADATA WSData;
SOCKET ConnectSocket;
If (WSAStartup (MAKEWORD (2, 2), & WSData )! = 0)
{
Printf ("socket initial error! ");
Return 1;
}
ConnectSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
If (ConnectSocket = SOCKET_ERROR)
{
Printf ("create socket error! ");
WSACleanup ();
Return 1;
}
Sockaddr_in Client;
Client. sin_family = AF_INET;
Client. sin_addr.s_addr = inet_addr ("192.168.8.21 ");
Client. sin_port = htons (4600 );
// Connect to server
If (connect (ConnectSocket, (sockaddr *) & Client, sizeof (Client ))! = 0)
{
Printf ("connect error! ");
Return 1;
}
// Translate data
Char SendBuf [100] = "hi ";
Send (ConnectSocket, SendBuf, lstrlenA (SendBuf) + 1, 0 );
Char RecvBuf [101];
Recv (ConnectSocket, RecvBuf, lstrlenA (RecvBuf) + 1, 0 );
Printf ("% s", RecvBuf );
// Close socket
Closesocket (ConnectSocket );
WSACleanup ();
Return 0;
}
Server code:
# Include
# Include
# Include
# Pragma comment (lib, "ws2_32.lib ")
Int main ()
{
WSADATA wsaData;
SOCKET ListenSocket;
Int iResult;
// Initialize the socket
IResult = WSAStartup (MAKEWORD (2, 2), & wsaData );
If (iResult! = 0)
{
Printf ("WSAStartup failed: % d", iResult );
Return 1;
}
// Create a socket
ListenSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP );
If (ListenSocket = INVALID_SOCKET)
{
Printf ("Error at socket (): % d", WSAGetLastError ());
WSACleanup ();
Return 1;
}
// Band socket
Sockaddr_in service;
Service. sin_family = AF_INET;
Service. sin_addr.s_addr = inet_addr ("192.168.8.21 ");
Service. sin_port = htons (4600 );
If (bind (ListenSocket, (sockaddr *) & service, sizeof (service) = SOCKET_ERROR)
{
Printf ("bind () failed .");
Closesocket (ListenSocket );
Return 1;
}
// Listen socket
If (listen (ListenSocket, SOMAXCONN) = SOCKET_ERROR)
{
Printf ("listen () failed .");
Closesocket (ListenSocket );
Return 1;
}
// Accept a socket and use it recv or send
Sockaddr_in Client;
SOCKET ConnectSocket;
Int len = sizeof (sockaddr_in );
While (1)
{
ConnectSocket = accept (ListenSocket, (sockaddr *) & Client, & len );
Char RecvBuf [100];
Recv (ConnectSocket, RecvBuf, lstrlenA (RecvBuf) + 1, 0 );
Printf ("% s", RecvBuf );
Char SendBuf [100] = "hello man! ";
Send (ConnectSocket, SendBuf, lstrlenA (SendBuf) + 1, 0 );
}
// Close socket
Closesocket (ConnectSocket );
Closesocket (ListenSocket );
WSACleanup ();
Return 0;
}