// Server.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
#define PORT 8080
int main() {
WSADATA wsaData;
int iResult;
// 初始化 Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cerr << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
// 绑定套接字
if (bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cerr << "Bind failed: " << WSAGetLastError() << std::endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 监听
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cerr << "Listen failed: " << WSAGetLastError() << std::endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
std::cout << "Waiting for a connection..." << std::endl;
// 接受客户端连接
SOCKET clientSocket = accept(serverSocket, nullptr, nullptr);
if (clientSocket == INVALID_SOCKET) {
std::cerr << "Accept failed: " << WSAGetLastError() << std::endl;
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 接收客户端消息
char buffer[1024] = { 0 };
int bytesReceived = recv(clientSocket, buffer, 1024, 0);
if (bytesReceived > 0) {
std::cout << "Message from client: " << buffer << std::endl;
}
// 发送消息给客户端
const char* hello = "Hello from server";
send(clientSocket, hello, strlen(hello), 0);
// 关闭套接字
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
// 等待用户按下回车
std::cout << "Press Enter to exit...";
std::cin.get(); // 等待用户输入
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Socket test project
共54个文件
tlog:14个
pdb:4个
ipch:3个
需积分: 0 0 下载量 8 浏览量
2024-09-26
00:17:51
上传
评论
收藏 41.95MB RAR 举报
温馨提示
Socket test project
资源推荐
资源详情
资源评论
收起资源包目录
Socket.rar (54个子文件)
Socket
Client
Client
Client.vcxproj.filters 979B
Client.vcxproj.user 168B
x64
Debug
vc143.pdb 468KB
Client.tlog
CL.write.1.tlog 348B
Cl.items.tlog 81B
Client.lastbuildstate 146B
CL.command.1.tlog 662B
link.command.1.tlog 1KB
link.read.1.tlog 3KB
link.write.1.tlog 344B
CL.read.1.tlog 35KB
Client.ilk 762KB
Client.log 204B
vc143.idb 363KB
Client.exe.recipe 280B
Client.obj 116KB
Client.cpp 2KB
Client.vcxproj 6KB
.vs
Client
v17
.suo 30KB
LTSC 17.6
Browse.VC.db 9.09MB
ipch
AutoPCH
f2ce6095122d75f3
CLIENT.ipch 37.13MB
FileContentIndex
read.lock 0B
9656c9e3-ba7c-4515-b92c-e73f5bb32fb5.vsidx 10KB
merges
Client.sln 1KB
Server
Server.sln 2KB
.vs
Server
v17
.suo 24KB
LTSC 17.6
Browse.VC.db 36.59MB
ipch
AutoPCH
251519b29500149b
SERVER.ipch 82.13MB
f2ce6095122d75f3
CLIENT.ipch 82.13MB
FileContentIndex
read.lock 0B
5278e58c-f2a9-4ac1-ba7c-9527d7b4e757.vsidx 14KB
dc87d8c3-0d61-4e76-a027-4e75a49ff2c4.vsidx 15KB
merges
x64
Debug
Server.exe 70KB
Client.pdb 1.5MB
Server.pdb 1.5MB
Client.exe 70KB
Server
x64
Debug
vc143.pdb 468KB
Server.log 204B
Server.obj 117KB
Server.ilk 766KB
vc143.idb 363KB
Server.exe.recipe 280B
Server.tlog
CL.write.1.tlog 348B
Cl.items.tlog 81B
CL.command.1.tlog 662B
link.command.1.tlog 1KB
link.read.1.tlog 3KB
link.write.1.tlog 344B
Server.lastbuildstate 146B
CL.read.1.tlog 35KB
Server.vcxproj.filters 979B
Server.cpp 2KB
Server.vcxproj.user 168B
Server.vcxproj 6KB
共 54 条
- 1
资源评论
pxp1981
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功