Talk about C++ Socket programming
- 2020-05-27 06:39:21
- OfStack
There are three types of programming for sockets: streaming socket (SOCK_STREAM), datagram socket (SOCK_DGRAM), original socket (SOCK_RAW). TCP-based socket programming is using streaming sockets.
Steps of server-side programming:
1: load the socket library and create the socket (WSAStartup()/socket());
2: bind the socket to 1 IP address and 1 port (bind());
3: set the socket to listen mode and wait for the connection request (listen());
4: after the request arrives, accept the connection request and return a new socket (accept()) corresponding to the connection;
5: use the returned socket to communicate with the client (send()/recv());
6: return and wait for another connection request;
7: close the socket and close the loaded socket library (closesocket()/WSACleanup()).
Steps of client programming:
1: load the socket library and create the socket (WSAStartup()/socket());
2: make a connection request to the server (connect());
3: communicate with the server (send()/recv());
4: close the socket and close the loaded socket library (closesocket()/WSACleanup()).
Type 1: load/release Winsock library:
1. Loading method:
WSADATA wsa;
/* Initialize the socket resources */
if (WSAStartup(MAKEWORD(1,1),&wsa) != 0)
{
return; // On behalf of the failure
}
2. Release method:
WSACleanup();
Formula 2: construct SOCKET:
1. Server side: construct listening SOCKET and streaming SOCKET.
SOCKET Listen_Sock = socket(AF_INET, SOCK_STREAM, 0)
2. Client: construct communication SOCKET and streaming SOCKET.
SOCKET Client _Sock = socket(AF_INET, SOCK_STREAM, 0)
Type 3: configure the listening address and port:
1. Server: SOCKADDR_IN
serverAddr
ZeroMemory((char *)& serverAddr ,sizeof( serverAddr ));
serverAddr .sin_family = AF_INET ;
serverAddr .sin_port = htons( 1234 ); /* Local listening port :1234*/
serverAddr .sin_addr.s_addr = htonl( INADDR_ANY ); /* There are IP*/
Type 4: bind SOCKET:
1. Server side: binding listens to SOCKET.
bind( Listen_Sock, (struct sockaddr *)& serverAddr, sizeof( serverAddr ))
Type 5: server/client connection:
1. Server: wait for the client to access.
SOCKET Command_Sock = accept( Listen_Sock , ...)
2. Client: request connection with the server.
int ret = connect( Client_Sock , ...)
Type 6: receiving/sending data:
1. Server: wait for the client to access.char buf[1024].
Received data :recv(Command_Sock,buf,...)
or
Send data :send(Command_Sock,buf,...)
2. Client: request connection with server.char buf[1024].
Send data :send(Client _Sock,buf,...)
or
Received data :recv(Client _Sock,buf,...)
Type 7: closing SOCKET:
1. Server: close SOCKET.
closesocket( Listen_Sock )
closesocket( Command_Sock )
2. Client: close SOCKET.
closesocket( Client_Sock )
#include <stdio.h>
#include <Winsock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
send(sockClient,"hello",strlen("hello")+1,0);
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
closesocket(sockClient);
WSACleanup();
}