求C++,socket聊天程序的代码,最好是实现了两个客户端之间经由服务器的信息互发,聊天功能要完整些

求C++,socket聊天程序的代码,最好是实现了两个客户端之间经由服务器的信息互发,聊天功能要完整些,第1张

#include <stdioh>
#include <winsock2h>
#define MAX_SIZE 200
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrvsin_addrS_unS_addr = htonl(INADDR_ANY);
addrSrvsin_family = AF_INET;
addrSrvsin_port = htons(6000);
bind(sockSrv, (SOCKADDR )&addrSrv, sizeof(SOCKADDR));
SOCKADDR addrClient;
char recvBuffer[MAX_SIZE];
memset(recvBuffer, 0, sizeof(recvBuffer));
int len = sizeof(SOCKADDR);
recvfrom(sockSrv, recvBuffer, sizeof(recvBuffer), 0, (SOCKADDR )&addrClient, &len);
printf("%s\n", recvBuffer);
closesocket(sockSrv);
}
#include <stdioh>
#include <winsock2h>
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
/ The WinSock DLL is acceptable Proceed /
SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrClient;
addrClientsin_addrS_unS_addr = inet_addr("127001");
addrClientsin_family = AF_INET;
addrClientsin_port = htons(6000);
sendto(sockClient, "你若此生若只如一瞬", strlen("你若此生若只如一瞬"), 0, (SOCKADDR )&addrClient, sizeof(SOCKADDR));
closesocket(sockClient);
WSACleanup();
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10854219.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存