服务端
# include <iostream>
# include <WinSock2.h> # pragma comment ( lib, "ws2_32.lib" )
# pragma warning ( disable: 4996 ) # define SERVICE_IP "192.168.X.X"
# define SERVICE_PORT ( 1024 + 256 )
# define SERVICE_BUFFER_SIZE 1024 int main ( ) { WSADATA data; if ( WSAStartup ( MAKEWORD ( 2 , 2 ) , & data) != 0 ) { std:: cout << "初始化失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } SOCKET listen_socket = socket ( AF_INET, SOCK_STREAM, 0 ) ; if ( listen_socket == - 1 ) { std:: cout << "创建 socket 失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } sockaddr_in addr; addr. sin_family = AF_INET; addr. sin_port = SERVICE_PORT; addr. sin_addr. S_un. S_addr = inet_addr ( SERVICE_IP) ; if ( bind ( listen_socket, ( sockaddr* ) & addr, sizeof ( addr) ) == - 1 ) { std:: cout << "绑定 socket 失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } if ( listen ( listen_socket, 5 ) == - 1 ) { std:: cout << "监听 socket 失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } sockaddr client; int client_len = sizeof ( client) ; SOCKET client_socket = accept ( listen_socket, & client, & client_len) ; if ( client_socket == - 1 ) { std:: cout << "接收客户端 socket 失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } char service_write_buffer[ SERVICE_BUFFER_SIZE] = "服务器已接收客户端请求" ; if ( send ( client_socket, service_write_buffer, sizeof ( service_write_buffer) , 0 ) == - 1 ) { std:: cout << "服务端发送响应信息失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } char service_read_buffer[ SERVICE_BUFFER_SIZE] ; if ( recv ( client_socket, service_read_buffer, SERVICE_BUFFER_SIZE, 0 ) <= 0 ) { std:: cout << "接收客户端发送信息失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } std:: cout << "客户端请求: " << service_read_buffer << std:: endl; WSACleanup ( ) ; return 0 ;
}
客户端
# include <iostream>
# include <WinSock2.h> # pragma comment ( lib, "ws2_32.lib" )
# pragma warning ( disable: 4996 ) # define SERVICE_IP "192.168.X.X"
# define SERVICE_PORT ( 1024 + 256 )
# define CLIENT_BUFFER_SIZE 1024 int main ( ) { WSADATA data; if ( WSAStartup ( MAKEWORD ( 2 , 2 ) , & data) != 0 ) { std:: cout << "初始化失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } SOCKET client_socket = socket ( AF_INET, SOCK_STREAM, 0 ) ; if ( client_socket == - 1 ) { std:: cout << "创建 socket 失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } sockaddr_in addr; addr. sin_family = AF_INET; addr. sin_port = SERVICE_PORT; addr. sin_addr. S_un. S_addr = inet_addr ( SERVICE_IP) ; if ( connect ( client_socket, ( sockaddr* ) & addr, sizeof ( addr) ) == - 1 ) { std:: cout << "连接服务端失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } char client_read_buffer[ CLIENT_BUFFER_SIZE] ; if ( recv ( client_socket, client_read_buffer, CLIENT_BUFFER_SIZE, 0 ) <= 0 ) { std:: cout << "接收服务端响应失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } else { std:: cout << "服务端响应: " << client_read_buffer << std:: endl; } char client_write_buffer[ CLIENT_BUFFER_SIZE] = "客户端已接收到服务端响应" ; if ( send ( client_socket, client_write_buffer, sizeof ( client_write_buffer) , 0 ) == - 1 ) { std:: cout << "向服务端发送信息失败" << std:: endl; WSACleanup ( ) ; return - 1 ; } WSACleanup ( ) ; return 0 ;
}