windows使用winsock库实现一个TCP/UDP的客户机服务器模式的聊天程序案例
时间: 2024-05-06 12:18:35 浏览: 124
以下是使用winsock库实现TCP/UDP客户端服务器聊天程序的示例代码:
服务器端代码:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c;
char* message;
std::cout << "\nInitialising Winsock...";
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::cout << "Failed. Error Code : " << WSAGetLastError();
return 1;
}
std::cout << "Initialised.\n";
//Create a socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
std::cout << "Could not create socket : " << WSAGetLastError();
}
std::cout << "Socket created.\n";
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
//Bind
if (bind(s, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
std::cout << "Bind failed with error code : " << WSAGetLastError();
exit(EXIT_FAILURE);
}
std::cout << "Bind done.\n";
//Listen to incoming connections
listen(s, 3);
//Accept and incoming connection
std::cout << "Waiting for incoming connections...\n";
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(s, (struct sockaddr*)&client, &c)) != INVALID_SOCKET)
{
std::cout << "Connection accepted.\n";
//Reply to the client
message = "Hello Client, I have received your connection. And now I will assign a name for you.\n";
send(new_socket, message, strlen(message), 0);
//Receive a reply from the client
int recv_size;
char client_reply[2000];
while ((recv_size = recv(new_socket, client_reply, 2000, 0)) != SOCKET_ERROR)
{
//Add a NULL terminating character to make it a proper string before printing
client_reply[recv_size] = '\0';
std::cout << "Client: " << client_reply << std::endl;
//Reply to the client
message = "Hello Client, I have received your message.\n";
send(new_socket, message, strlen(message), 0);
}
if (recv_size == SOCKET_ERROR)
{
std::cout << "recv failed with error code : " << WSAGetLastError();
}
}
if (new_socket == INVALID_SOCKET)
{
std::cout << "accept failed with error code : " << WSAGetLastError();
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
```
客户端代码:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char* message, server_reply[2000];
std::cout << "\nInitialising Winsock...";
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::cout << "Failed. Error Code : " << WSAGetLastError();
return 1;
}
std::cout << "Initialised.\n";
//Create a socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
std::cout << "Could not create socket : " << WSAGetLastError();
}
std::cout << "Socket created.\n";
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(8888);
//Connect to remote server
if (connect(s, (struct sockaddr*)&server, sizeof(server)) < 0)
{
std::cout << "connect error\n";
return 1;
}
std::cout << "Connected.\n";
//Receive a reply from the server
int recv_size;
while ((recv_size = recv(s, server_reply, 2000, 0)) != SOCKET_ERROR)
{
//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
std::cout << "Server: " << server_reply << std::endl;
//Send some data
message = "Hello Server, I am a client.\n";
send(s, message, strlen(message), 0);
}
if (recv_size == SOCKET_ERROR)
{
std::cout << "recv failed with error code : " << WSAGetLastError();
}
closesocket(s);
WSACleanup();
return 0;
}
```
这个程序实现了一个简单的TCP聊天程序,当客户端连接到服务器时,服务器会向客户端发送一条欢迎信息,然后等待接收客户端发送的消息并回复给客户端。客户端连接到服务器后,会收到欢迎信息,然后不断发送消息给服务器并等待回复。
阅读全文