题目
由于本学期上了网络编程课程,老师要求写使用Socke实现网络编程。于是参考 C++多线程实现即时通信软件 写出了简单版本的没有界面的即时通信软件。
软件:VS
直接上代码,需要讲解原理的,可以参考 C++多线程实现即时通信软件
服务器端
#include<stdio.h>
#include <Winsock2.h>
#include<WS2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#include<iostream>
using namespace std;
#include<windows.h>
SOCKADDR_IN cAddr = { 0 };
int len = sizeof cAddr;
SOCKET clientSocket[1001];
void chat(int n) {
char buff[1024];
int r;
while (1) {
r = recv(clientSocket[n], buff, 1023, NULL);
if (r > 0) {
buff[r] = 0;
printf("%s发来的信息:%s\n", inet_ntop(AF_INET, (void*)&cAddr.sin_addr, buff, 1023), buff);
}
}
}
int main() {
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2) {
cout << "确认版本失败!\n" << endl;
return -1;
}
cout << "确认版本成功!" << endl;
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == SOCKET_ERROR) {
cout << "创建socket失败!" << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "socket创建成功!" << endl;
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
inet_pton(AF_INET, "192.168.31.100", (void*)&addr.sin_addr.S_un.S_addr);
addr.sin_port = htons(9999);
int r = bind(serverSocket, (struct sockaddr*)&addr, sizeof addr);
if (r == -1) {
cout << "绑定失败!" << GetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
cout << "绑定成功!" << endl;
r = listen(serverSocket, 10);
if (r == -1) {
cout << "监听失败!" << GetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
cout << "监听成功!" << endl;
for (int i = 0; i < 1001; i++) {
clientSocket[i] = accept(serverSocket, (sockaddr*)&cAddr, &len);
if (clientSocket[i] == SOCKET_ERROR) {
cout << "客户端连接失败:" << GetLastError() << endl;
closesocket(serverSocket);
WSACleanup();
return -1;
}
cout << "客户端连接成功!" << endl;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)chat, (LPVOID)i, NULL, NULL);
}
closesocket(serverSocket);
WSACleanup();
while (1);
return 0;
}
客户端
多次点击TcpClient.exe可以产生多个窗口
#include<stdio.h>
#include <Winsock2.h>
#include<WS2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#include<iostream>
using namespace std;
#include<windows.h>
int main() {
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2) {
cout << "确认版本失败!\n" << endl;
return -1;
}
cout << "确认版本成功!" << endl;
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == SOCKET_ERROR) {
cout << "创建socket失败!" << GetLastError() << endl;
WSACleanup();
return -1;
}
cout << "socket创建成功!" << endl;
SOCKADDR_IN addr = { 0 };
addr.sin_family = AF_INET;
inet_pton(AF_INET, "192.168.31.100", (void*)&addr.sin_addr.S_un.S_addr);
addr.sin_port = htons(9999);
int r = connect(clientSocket, (sockaddr*)&addr,sizeof addr);
if (r == -1) {
cout << "连接服务器失败:" << GetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
cout << "服务器连接成功!" << endl;
char buff[1024];
while (1) {
cin >> buff;
send(clientSocket, buff, strlen(buff), NULL);
}
closesocket(clientSocket);
WSACleanup();
while (1);
return 0;
}
运行效果:
|