c++|Socket 多线程
服务端
#include
#include
#pragma comment (lib, "ws2_32")
#include void sendFuction(SOCKET &sockConn)
{
while (true)
{
char buffer[100];
char mymsg[100];
gets(mymsg);
sprintf_s(buffer,100,"server say:\n%s",mymsg);
send(sockConn, buffer, strlen(buffer)+sizeof(char), NULL);
}
}
void recvFunction(SOCKET& sockClient)
{
while (true)
{
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
}}void main()
{
//1:加载套接字库,创建套接字(WSAStartup()/socket());
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
} if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
//2:绑定套接字到一个IP地址和一个端口上(bind());
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//3:将套接字设置为监听模式等待连接请求(listen());
listen(sockSrv,5);
//4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[50];
sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
//5:用返回的套接字和客户端进行通信(send()/recv())
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[50];
recv(sockConn,recvBuf,50,0);
printf("%s\n",recvBuf);
//多线程
std::thread sendThread(sendFuction,sockConn);
sendThread.detach();
std::thread recvThread(recvFunction,sockConn);
recvThread.detach();
//6:返回,等待另一连接请求;
//7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。
//closesocket(sockConn);
}}
客户端 【c++|Socket 多线程】
#include
#include
#pragma comment (lib, "ws2_32")
#include
void sendFuction(SOCKET &sockConn)
{
while (true)
{
char buffer[100];
char mymsg[100];
gets(mymsg);
sprintf_s(buffer,100,"clien say:\n%s",mymsg);
send(sockConn, buffer, strlen(buffer)+sizeof(char), NULL);
}}
void recvFunction(SOCKET& sockClient)
{
while (true)
{
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
}
}
void main()
{
//1:加载套接字库,创建套接字(WSAStartup()/socket());
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
} if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
//2:向服务器发出连接请求(connect());
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//3:和服务器端进行通信(send()/recv());
send(sockClient,"hello",strlen("hello")+1,0);
char recvBuf[50];
recv(sockClient,recvBuf,50,0);
printf("%s\n",recvBuf);
std::thread sendThread(sendFuction,sockClient);
sendThread.detach();
std::thread recvThread(recvFunction,sockClient);
recvThread.detach();
//4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。
//closesocket(sockClient);
//WSACleanup();
while (true)
{ }
}
推荐阅读
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 爱就是希望你好好活着
- 昨夜小楼听风
- 知识
- 死结。
- 我从来不做坏事
- 烦恼和幸福
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- opencv|opencv C++模板匹配的简单实现
- Linux下面如何查看tomcat已经使用多少线程