資源簡介
多個服務器的端口,多個客戶端可任意連接端口,多個客戶端可相互之間聊天。

代碼片段和文件信息
#include?
#include?
#include?
#include
#include
using?namespace?std;
#pragma?comment(lib“ws2_32.lib“)
//字符串處理的函數
int?myStrLen(char*?str)
{
int?i?=?0;
while?(*str?!=?‘\0‘)
{
i++;
str++;
}
return?i;
}
char*?myStrcat(char?*str1?char*?str2)
{
char?*temp?=?str1;
while?(*temp?!=?‘\0‘)temp++;
while?((*temp++=*str2++)?!=?‘\0‘);
return?str1;
}
char*?myStrcp(char?*str1?char?*str2)
{
char?*temp?=?str1;
int?i?=?0;
while?((*temp++?=?*str2++)?!=?‘\0‘);
return?str1;
}
char?*?myStrSplit(char?*str1?char?*str2?char?*str3?char?sp)
//參數:姓名需要分解信息得到信息分隔符
{
char?*temp?=?str1;
int?i?=?0;
while?((*str3++?=?*str2++)?!=?sp)i++;
while?((*temp++?=?*str2++)?!=?‘\0‘);
*(--str3)?=?‘\0‘;
return?str3;
}
bool?myStrSame(char?*str1?char?*str2)
{
if?(myStrLen(str1)?!=?myStrLen(str2))return?false;
for?(int?i?=?0;?i if?(*str1++?!=?*str2++)return?false;
return?true;
}
//start?set?global?varibal
WSADATA?wsaData?=?{?0?};//存放套接字信息
SOCKET?ClientSocket?=?INVALID_SOCKET;//客戶端套接字
SOCKADDR_IN?ServerAddr?=?{?0?};//服務端地址
USHORT?uPort?=?10002;//服務端端口
char?IP[32]?=?“127.0.0.1“;
char?msg_type?=?‘#‘;
char?buffer[4096]?=?{?0?};
char?name[30]?=?{0};
int?iRecvLen?=?0;
int?iSnedLen?=?0;
//end?set?global?varibal
//msg?type
//#?name?
//@?show?online?user
//info&xx?send?info?to?xx
//&?is?exit
bool?startConnectServer()
//連接服務器
{
//初始化套接字
if?(WSAStartup(MAKEWORD(2?2)?&wsaData))
{
printf(“WSAStartup?failed?with?error?code:?%d\n“?WSAGetLastError());
return?false;
}
//判斷套接字版本
if?(LOBYTE(wsaData.wVersion)?!=?2?||?HIBYTE(wsaData.wVersion)?!=?2)
{
printf(“wVersion?was?not?2.2\n“);
return?false;
}
//創建套接字
ClientSocket?=?socket(AF_INET?SOCK_STREAM?IPPROTO_TCP);
if?(ClientSocket?==?INVALID_SOCKET)
{
printf(“socket?failed?with?error?code:?%d\n“?WSAGetLastError());
return?false;
}
//設置服務器地址
ServerAddr.sin_family?=?AF_INET;
ServerAddr.sin_port?=?htons(uPort);//服務器端口
ServerAddr.sin_addr.S_un.S_addr?=?inet_addr(IP);//服務器地址
//連接服務器
if?(SOCKET_ERROR?==?connect(ClientSocket?(SOCKADDR*)&ServerAddr?sizeof(ServerAddr)))
{
printf(“connect?failed?with?error?code:?%d\n“?WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return?false;
}
printf(“連接服務器成功?IP:%s?Port:%d\n\n\n\n“
inet_ntoa(ServerAddr.sin_addr)?htons(ServerAddr.sin_port));
return?true;
}
void?sendMessage(char?*buf)
{
iSnedLen?=?send(ClientSocket?buf?myStrLen(buf)?0);
if?(SOCKET_ERROR?==?iSnedLen)
{
printf(“send?failed?with?error?code:?%d\n“?WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
}
else
{
//?< }
}
void?recvMessage()
{
char?buf[1024]?=?{?0?};
int?ret?=?0;
while?(1)
{
this_thread::sleep_for(chrono::seconds(3));
this_thread::yield();
memset(buf?0?sizeof(buf));
ret?=?recv(ClientSocket?bu
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-07-03?11:32??多端口服務器多個客戶端相互聊天\
?????文件???????90112??2019-07-03?11:18??多端口服務器多個客戶端相互聊天\Client.exe
?????文件????????4612??2019-07-03?11:18??多端口服務器多個客戶端相互聊天\MyClient.cpp
?????文件????????9470??2019-07-03?11:07??多端口服務器多個客戶端相互聊天\MyServer.cpp
?????文件??????120320??2019-07-03?11:07??多端口服務器多個客戶端相互聊天\Server.exe
?????目錄???????????0??2019-07-01?11:10??多端口服務器多個客戶端相互聊天\純c++聊天服務器及客戶端\
?????文件???????88576??2019-06-30?18:35??多端口服務器多個客戶端相互聊天\純c++聊天服務器及客戶端\Client.exe
?????文件????????3991??2019-06-30?18:35??多端口服務器多個客戶端相互聊天\純c++聊天服務器及客戶端\MyClient.cpp
?????文件????????7539??2019-06-30?19:05??多端口服務器多個客戶端相互聊天\純c++聊天服務器及客戶端\MyServer.cpp
?????文件??????112640??2019-07-01?10:33??多端口服務器多個客戶端相互聊天\純c++聊天服務器及客戶端\Server.exe
- 上一篇:Qt 仿QQ圖片查看器
- 下一篇:短信轟炸機電腦版源碼.e
評論
共有 條評論