windows网络编程 使用流式套接字实现网络通信
【实验要求】
(1)使用基本的服务器编程模式,创建流式套接字,根据指定端口号绑定服务,建立监听队列,并接受来自客户端的连接请求,收发数据;使用基本的客户端编程模式,能够创建流式套接字,通过用户的指定的服务器地址向指定服务器发起连接请求,与服务器之间实现收发数据,然后关闭连接。
(2)通过改进服务器端,实现服务器循环为多个客户端提供服务;服务器端能够实现显示对应客户端的IP地址;客户端实现用户输入信息方式与服务器对话。
(3)采用Windows环境下多线程开发方法改进服务器端,对每个客户连接请求独立创建通信线程,实现并发服务器。
客户端:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// 连接到WinSock 2对应的lib文件:Ws2_32.lib, Mswsock.lib, Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
// 定义默认的缓冲区长度和端口号
#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT "27015"
#pragma warning(disable:4996)
int __cdecl main(int argc, char** argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo* result = NULL, * ptr = NULL, hints;
char sendbuf[DEFAULT_BUFLEN];
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
char servIP[20] = "127.0.0.1"; // 保存输入的服务器IP
// 初始化套接字
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
while (true)
{
printf("请输入服务器域名/IP地址:\n>>>");
fflush(stdout);
scanf("%s", servIP); // 输入服务器IP
// 解析服务器地址和端口号
iResult = getaddrinfo(servIP, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
fflush(stdout);
continue;
//WSACleanup();
//return 1;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// 创建套接字
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 向服务器请求连接
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
printf(">>>连接到服务器(%s)...\n>>>连接成功...[ok]\n", servIP);
struct sockaddr_in sa; // 客户端ip
int len = sizeof(sa);
if (!getsockname(ConnectSocket, (struct sockaddr*) & sa, &len))
{
printf("Client Ip:%s ", inet_ntoa(sa.sin_addr));
printf("Client Port:%d \n\n", ntohs(sa.sin_port));
}
printf("====================================================\n");
char buff[40] = "";
sprintf(buff, "title client:[%s: %d]", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
system(buff); // 设置客户端标题
// 发送数据
bool flag = true;
while (flag)
{
printf("send: ");
fflush(stdout);
scanf("%s", sendbuf); // 键盘输入数据
if (0 == strncmp(sendbuf, "end", 3)) // 输入end 结束发送
{
// 数据发送结束,调用shutdown()函数声明不再发送数据,此时客户端仍可以接收数据
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
flag = false;
}
else
{
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("@%s: Bytes Sent: %ld\n-----------------------\n", servIP, iResult);
}
// 持续接收数据,直到服务器关闭连接
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
}
// 关闭套接字
closesocket(ConnectSocket);
// 释放资源
WSACleanup();
return 0;
}
服务器:
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <thread>
using std::thread; // 声明线程库
#pragma warning(disable:4996)
// 连接到WinSock 2对应的lib文件:Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// 定义默认的缓冲区长度和端口号
#define DEFAULT_BUFLEN 1024
#define DEFAULT_PORT "27015"
void task_connect(SOCKET, struct sockaddr_in); // 线程处理函数,处理已连接的套接字通讯
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo* result = NULL;
struct addrinfo hints;
// 初始化WinSock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
// 声明IPv4地址族,流式套接字,TCP协议
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// 解析服务器地址和端口号
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// 为面向连接的服务器创建套接字
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// 为套接字绑定地址和端口号
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
// 监听连接请求
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("服务器已启动,正在监听...\n");
// 接受客户端的连接请求,返回连接套接字ClientSocket
// 循环接收
int id = 0; // 表示第几个客户端连接
while (true)
{
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("\n>>>连接成功...[ok]\n");
struct sockaddr_in sa; // 客户端ip
int len = sizeof(sa);
if (!getpeername(ClientSocket, (struct sockaddr*) & sa, &len))
{
printf("Client Ip:%s ", inet_ntoa(sa.sin_addr));//显示客户端IP
printf("Client Port:%d \n\n", ntohs(sa.sin_port));//显示端口号
}
thread task(task_connect, ClientSocket, sa);
task.detach();
}
// 在必须要监听套接字的情况下释放该套接字
closesocket(ListenSocket);
WSACleanup();
return 0;
}
void task_connect(SOCKET ClientSocket, struct sockaddr_in ip)
{
int iResult; // 保存接收到数据的长度
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// 持续接收数据,直到对方关闭连接
do
{
memset(recvbuf, 0, recvbuflen);
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
// 情况1:成功接收到数据 [ip:port]:(len)data
printf("[%s:%d]:(%d)\t\b>> %s\n", inet_ntoa(ip.sin_addr), ntohs(ip.sin_port), iResult, recvbuf); // 打印数据
strcpy(recvbuf, "ok!"); // 回复ok
// 将缓冲区的内容回送给客户端
int iSendResult = send(ClientSocket, recvbuf, (int)strlen(recvbuf), 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
}
printf("reply : (%d) ok! \n-----------------------\n", iSendResult);
}
else if (iResult == 0) {
// 情况2:连接关闭
printf("Client [%s]: Connection closing...\n", inet_ntoa(ip.sin_addr));
}
else {
// 情况3:接收发生错误
printf("Client [%s]: recv failed with error: %d\n", inet_ntoa(ip.sin_addr), WSAGetLastError());
closesocket(ClientSocket);
printf("Client [%s]: Connection closing...\n", inet_ntoa(ip.sin_addr));
return;
}
} while (iResult > 0);
// 关闭连接
iResult = shutdown(ClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("Client [%s]: shutdown failed with error: %d\n", inet_ntoa(ip.sin_addr), WSAGetLastError());
closesocket(ClientSocket);
printf("Client [%s]: Connection closing...\n", inet_ntoa(ip.sin_addr));
return;
}
// 关闭套接字,释放资源
closesocket(ClientSocket);
}
获得本机IP:win+r,输入ipconfig即可。

浙公网安备 33010602011771号