客户端:
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")
SOCKET serverSocket;
void send_message();
int main()
{
//确定网络协议版本
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wHighVersion) != 2)
{
printf("确定网络版本协议失败:%d\n", GetLastError());
system("pause");
return -1;
}
else { printf("确定版本成功\n"); }
//创建socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//在这里我们就确定了使用tcp进行通信
if (serverSocket == SOCKET_ERROR)
{
printf("创建socket失败:%d\n", GetLastError());
WSACleanup();
system("pause");
return -1;
}
else { printf("创建socket成功\n"); }
//确定服务器协议地址簇
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
addr.sin_port = htons(9527);//非常重要的一步!确定端口号
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//非常重要的一步!确定ip地址,由于是本机通信故是"127.0.0.1"
//连接服务器
int r = connect(serverSocket, (sockaddr*) & addr, sizeof(addr));
if (r == -1)
{
printf("连接服务器失败:%d\n:", GetLastError());
WSACleanup();
system("pause");
return -1;
}
else { printf("连接服务器成功\n"); }
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)send_message, NULL, NULL,NULL);
char temp[256];
while (1)
{
r = recv(serverSocket, temp, 255, NULL);
if (r > 0)
{
temp[r] = '\0';
printf("%s\n", temp);
}
}
//关闭socket
closesocket(serverSocket);
WSACleanup();
}
void send_message()
{
//通信
char buff[128];
while (1)
{
scanf("%s", buff);//循环接受输入并发送给服务器
int r = send(serverSocket, buff, strlen(buff), NULL);//接受消息,并写入buff
printf("发送成功\n");
}
}
服务端:
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#include <time.h>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")
SOCKET clientSocket[512];
int count = 0;
void receive_send_message(int idx);
int main()
{
//确定网络协议版本
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wHighVersion) != 2)
{
printf("确定网络版本协议失败:%d\n", GetLastError());
system("pause");
return -1;
}
else { printf("确定版本成功\n"); }
//创建socket
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//在这里我们就确定了使用tcp进行通信
if (serverSocket == SOCKET_ERROR)
{
printf("创建socket失败:%d\n", GetLastError());
WSACleanup();
system("pause");
return -1;
}
else { printf("创建socket成功\n"); }
//确定服务器协议地址簇
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
addr.sin_port = htons(9527);//非常重要的一步!确定端口号
addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//非常重要的一步!确定ip地址,由于是本机通信故是"127.0.0.1"
//绑定
int r = bind(serverSocket, (sockaddr*)&addr, sizeof addr);
if (r == -1)
{
printf("绑定失败:%d:", GetLastError());
WSACleanup();
system("pause");
return -1;
}
else { printf("绑定成功\n"); }
//监听
r = listen(serverSocket, 10);
if (r == -1)
{
printf("监听失败:%d:", GetLastError());
WSACleanup();
system("pause");
return -1;
}
else { printf("监听成功\n"); }
//接受客户端连接
for (int i = 0; i < 512; i++)
{
clientSocket[i] = accept(serverSocket, (sockaddr*)NULL, NULL);
count++;
printf("%d号机客户端连接\n",i);
//创建一个线程,接收到当前客户端发送的消息就给其它所有客户端发送消息
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)receive_send_message, (LPVOID)i, NULL, NULL);
}
//关闭socket
closesocket(serverSocket);
WSACleanup();
while (1);
}
void receive_send_message(int idx)
{
//通信
char buff[128];
char temp[256];
while (1)
{
int r = recv(clientSocket[idx], buff, 128, NULL);//接受消息,并写入buff
if (r > 0)//如果接受到消息
{
time_t now = time(NULL);
char time_str[20];//定义时间类型
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M", localtime(&now));
buff[r] = '\0';
printf("%d号机:%s\n%s\n\n", idx, buff, time_str);
memset(temp, 0, 256);//清空temp
sprintf(temp, "%d号机:%s\n%s\n\0", idx, buff, time_str);
for (int i = 0; i < count; i++)
{
send(clientSocket[i], temp, strlen(temp), NULL);
}
}
}
}
运行结果: