基于Windows平台的Socket网络编程,用的QT,这个例子里一个服务端和一个客户端,两个应用程序之间进行通信,所以创建两个项目,目录结构如下:
直接贴代码:
服务端:
1 // main.cpp 2 #include <QCoreApplication> 3 #include <iostream> 4 #include <QDebug> 5 #include <cstring> 6 #include <WinSock2.h> 7 #pragma comment(lib, "ws2_32.lib") 8 #ifdef _WIN32 9 typedef int socklen_t; 10 #endif 11 12 13 int main(int argc, char *argv[]) 14 { 15 QCoreApplication a(argc, argv); 16 17 #ifdef _WIN32 18 WSADATA wsaData; 19 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { 20 std::cerr << "Failed to initialize WinSock" << std::endl; 21 return -1; 22 } 23 #endif 24 25 // std::cout << "Hello World!" << std::endl; 26 // qDebug() << "Hello World!" ; 27 28 //创建套接字 29 int serverSocket = socket(AF_INET, SOCK_STREAM, 0); 30 if (serverSocket == -1) 31 { 32 std::cerr << "Error creating socket" << std::endl; 33 return -1; 34 } 35 36 //准备地址结构 37 sockaddr_in serverAddress; 38 serverAddress.sin_family = AF_INET; 39 serverAddress.sin_port = htons(8888); 40 serverAddress.sin_addr.s_addr = INADDR_ANY; 41 42 //绑定套接字 43 if (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) 44 { 45 std::cerr << "Error binding socket" << std::endl; 46 #ifdef _WIN32 47 closesocket(serverSocket); 48 #else 49 close(serverSocket); 50 #endif 51 return -1; 52 } 53 54 //监听连接请求 55 if (listen(serverSocket, 5) == -1) 56 { 57 std::cerr << "Error listening on socket" << std::endl; 58 #ifdef _WIN32 59 closesocket(serverSocket); 60 #else 61 close(serverSocket); 62 #endif 63 return -1; 64 } 65 66 std::cout << "Waitinig for client to connect..." << std::endl; 67 68 //接受连接请求 69 sockaddr_in clientAddress; 70 socklen_t clientAddressLength = sizeof(clientAddress); 71 int clientScoket = accept(serverSocket, (struct sockaddr*)&clientAddress, &clientAddressLength); 72 if (clientScoket == -1) 73 { 74 std::cerr << "Error accepting connection" << std::endl; 75 closesocket(serverSocket); 76 return -1; 77 } 78 79 std::cout << "Connection established with client" << std::endl; 80 81 //发送和接受数据 82 char buffer[1024]; 83 memset(buffer, 0, sizeof(buffer)); 84 recv(clientScoket, buffer, sizeof(buffer), 0); 85 std::cout << "Received from client: " << buffer << std::endl; 86 87 const char* message = "Hello, client!"; 88 send(clientScoket, message, strlen(message), 0); 89 90 //关闭套接字 91 closesocket(clientScoket); 92 closesocket(serverSocket); 93 94 return a.exec(); 95 }
客户端:
1 // main.cpp 2 #include <QCoreApplication> 3 #include <iostream> 4 #include <QDebug> 5 #include <cstring> 6 #include <WinSock2.h> 7 #include <ws2tcpip.h> 8 #include <QtNetwork> 9 #pragma comment(lib, "ws2_32.lib") 10 #ifdef _WIN32 11 typedef int socklen_t; 12 #endif 13 14 15 int main(int argc, char *argv[]) 16 { 17 QCoreApplication a(argc, argv); 18 19 #ifdef _WIN32 20 WSADATA wsaData; 21 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { 22 std::cerr << "Failed to initialize WinSock" << std::endl; 23 return -1; 24 } 25 #endif 26 27 int clientSocket = socket(AF_INET, SOCK_STREAM, 0); 28 if (clientSocket == -1) 29 { 30 std::cerr << "Error creating socket" << strerror(errno) << std::endl; 31 return -1; 32 } 33 34 //准备地址结构 35 sockaddr_in serverAddress; 36 serverAddress.sin_family = AF_INET; 37 serverAddress.sin_port = htons(8888); 38 inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr); 39 40 //连接到服务端 41 if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) 42 { 43 std::cerr << "Error connecting to server" << std::endl; 44 closesocket(clientSocket); 45 return -1; 46 } 47 48 std::cout << "Connected to server" << std::endl; 49 50 //发送和接收数据 51 const char* message = "Hello server!"; 52 send(clientSocket, message, strlen(message), 0); 53 54 char buffer[1024]; 55 memset(buffer, 0, sizeof(buffer)); 56 recv(clientSocket, buffer, sizeof(buffer), 0); 57 std::cout << "Received from server: " << buffer << std::endl; 58 59 //关闭套接字 60 closesocket(clientSocket); 61 62 return a.exec(); 63 }
注意:先运行服务器端,再运行客户端,可正常通信
运行效果:
标签:main,Socket,lib,int,C++,WIN32,TCP,ifdef,include From: https://www.cnblogs.com/l-yy/p/17859347.html