原码
客户端:
#include<Winsock2.h>
#include <WS2tcpip.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<graphics.h>
#pragma comment(lib,"Ws2_32.lib")
using namespace std;
SOCKET clientSocket;
int num = 0;
//接收服务器的内容
void recvServerAndShow();
void recvServerAndShow()
{
char buff[1024]={0};
int r;
while (1)
{
r = recv(clientSocket, buff, 1023, NULL);
if (r > 0)
{
outtextxy(0, num * 20, buff);
num++;
}
}
}
int main()
{
initgraph(200, 400, SHOWCONSOLE);
//1.确定协议
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
cout << "确定版本失败:" << GetLastError();
return -1;
}
cout << "确定版本成功!" << endl;
// 2.创建socket
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SOCKET_ERROR == clientSocket)
{
cout << "创建失败" << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "创建成功!" << endl;
// 3.获取服务器协议地址簇
SOCKADDR_IN sAddr = { 0 };
sAddr.sin_family = AF_INET;
sAddr.sin_addr.S_un.S_addr = inet_addr("192.168.116.1");
sAddr.sin_port = htons(9527);
//4.连接服务器
int r = connect(clientSocket, (sockaddr*)&sAddr, sizeof sAddr);
if (-1 == r)
{
cout << "连接服务器失败:" << GetLastError();
closesocket(clientSocket);
WSACleanup();
return -1;
}
cout << "连接服务器成功" << endl;
CreateThread(NULL, NULL,
(LPTHREAD_START_ROUTINE)recvServerAndShow,
NULL, NULL, NULL);
//5.通信
char buff[1024];
//string buff;
//string buff;
while (1)
{
memset(buff, 0, 1024);
cout << "请输入要发送给服务器的信息:";
//cin >> buff;
scanf("%s", buff);
send(clientSocket, buff, strlen(buff), NULL);
}
//
//
//
//
// 8.断开连接
closesocket(clientSocket);
// 9.清理协议信息
WSACleanup();
//return -1;
}
服务器端:
#include<Winsock2.h>
#include <WS2tcpip.h>
#include<stdio.h>
#include<iostream>
#include<Windows.h>
#pragma comment(lib,"Ws2_32.lib")
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
using namespace std;
int c=0;
SOCKET cSocket[1024];//最多允许1024个客户端
void communicat(int idx);
int main()
{
//1.确定协议
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
cout << "确定版本失败:" << GetLastError();
return -1;
}
cout << "确定版本成功!" << endl;
// 2.创建socket
SOCKET sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (SOCKET_ERROR == sSocket)
{
cout << "创建失败" << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "创建成功!" << endl;
// 3.服务器协议地址簇
SOCKADDR_IN sAddr = { 0 };
sAddr.sin_family = AF_INET;
sAddr.sin_addr.S_un.S_addr = inet_addr("192.168.116.1");
sAddr.sin_port = htons(9527);
// 4.绑定
int r = bind(sSocket, (sockaddr*)&sAddr, sizeof sAddr);
if (-1 == r)
{
cout << "创建socket失败" << GetLastError() << endl;
closesocket(sSocket);
WSACleanup();
return -1;
}
cout << "绑定成功" << endl;
// 5.监听
r = listen(sSocket, 10);
if (-1 == r)
{
cout << "监听失败:" << GetLastError << endl;
closesocket(sSocket);
WSACleanup();
return -1;
}
cout << "监听成功!" << endl;
// 6.接受客户端连接
SOCKADDR_IN cAddr;// = { 0 };
int cAddrLen = sizeof cAddr;
for (int i = 0; i < 1024; i++)
{
cSocket[i] = accept(sSocket, (sockaddr*)&cAddr, &cAddrLen);
if (INVALID_SOCKET == cSocket[i])
{
cout << "网络崩溃:" << GetLastError();
closesocket(sSocket);
WSACleanup();
return -1;
}
cout << "有客户端连接上了服务器" << inet_ntoa(cAddr.sin_addr) << endl;
c++;
CreateThread(NULL, NULL,
(LPTHREAD_START_ROUTINE)communicat, (LPVOID)i,
NULL, NULL);
}
// 8.断开连接
closesocket(sSocket);
// 9.清理协议信息
WSACleanup();
//return -1;
}
void communicat(int idx)
{
// 7.通信
char buff[1024];
int r;
while (1)
{
r = recv(cSocket[idx], buff, 1023, NULL);
if (r > 0)
{
buff[r] = 0;
printf("%d >> %s\n", idx, buff);
//广播 发送给所有客户
for (int i = 0; i < c; i++)
{
send(cSocket[i], buff, r, NULL);
}
}
}
}
详解
理论依据:TCP、Socket、网络编程;
实现语言:C语言;
实现环境:VS2022
将两个代码放在两个项目里,先运行服务器端再运行客户端,如果有报错的话可能是因为一些标准现在已经废弃不使用了,百度一下解决方法调整一些属性值就可以了。
服务器端
确定协议;
创建socket;
服务器协议地址簇;
绑定;
监听(要监听链路,方便后期连接);
接受客户端连接(并通信);
断开连接;
清理协议信息;
客户端
确定协议;
创建socket;
获取服务器协议地址簇;
连接服务器(连上之后接受服务器内容);
通信;
断开连接;
清理协议信息;