博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ TCP winsock 多线程编程的代码
阅读量:5831 次
发布时间:2019-06-18

本文共 2608 字,大约阅读时间需要 8 分钟。

将写代码过程中经常用到的一些代码段珍藏起来,下边资料是关于C++ TCP winsock 多线程编程的代码,希望能对各位也有好处。

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

#define PORT 4000

#define IP_ADDRESS "127.0.0.1"

DWORD WINAPI ClientThread (LPVOID ipParameter)

{
SOCKET ClientScoket = (SOCKET)ipParameter;
int RET = 0;
char RecvBuffer[MAX_PATH];
while(true){
memset(RecvBuffer,0x00,sizeof(RecvBuffer));
RET = recv(ClientScoket,RecvBuffer,MAX_PATH,0);
if(RET == 0||RET == SOCKET_ERROR)
{
cout<<"failed,exit"<<endl;
break;
}
cout<<"接受的消息为"<<RecvBuffer<<endl;
}

return 0;

}

int main(void)
{
WSADATA WSA;
SOCKET severScoket,clientScoket;
struct sockaddr_in LocalAddr,clientAddr;
int AddrLen = 0;
HANDLE hThread = NULL;
int RET = 0;
if (WSAStartup(MAKEWORD(2,2),&WSA)!= 0 )
{
cout<<"init failed"<<endl;
return -1;
}
severScoket = socket(AF_INET,SOCK_STREAM,0);
if(severScoket == INVALID_SOCKET)
{
cout<<"creat failed"<<GetLastError()<<endl;
return -1;
}
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
LocalAddr.sin_port = htons(PORT);
memset(LocalAddr.sin_zero,0x00,8);
if (RET != 0)
{
cout<<"bind failed";
return -1;
}
RET = listen(severScoket,5);
if (RET!= 0)
{
cout<<"listen failed";
return -1;
}
cout<<"服务器已启动"<<endl;
while(true)
{
AddrLen = sizeof(clientAddr);
if (clientScoket ==INVALID_SOCKET)
{
cout<<"accept failed";
break;
}
cout<<"客户端连接"<<inet_ntoa(clientAddr.sin_addr)<<"."<<clientAddr.sin_port<<endl;
hThread = CreateThread(NULL,0,ClientThread,(LPVOID)clientScoket,0,NULL);
if(hThread == NULL)
{
cout<<"creat thread failed"<<endl;
break;
}
CloseHandle(hThread);
}
closesocket(severScoket);
closesocket(clientScoket);
WSACleanup();
return 0;
}

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

#define PORT 4000

#define IP_ADDRESS "127.0.0.1"
int main(void)
{
WSADATA WSA;
SOCKET clientScoket;
struct sockaddr_in serveAddr;
int AddrLen = 0;
HANDLE hThread = NULL;
int RET = 0;
char SendBuffer[MAX_PATH];
if (WSAStartup(MAKEWORD(2,2),&WSA)!= 0 )
{
cout<<"init failed"<<endl;
return -1;
}
clientScoket = socket(AF_INET,SOCK_STREAM,0);
if(clientScoket == INVALID_SOCKET)
{
cout<<"creat failed"<<GetLastError()<<endl;
return -1;
}
serveAddr.sin_family = AF_INET;
serveAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
serveAddr.sin_port = htons(PORT);
memset(serveAddr.sin_zero,0x00,8);

if (RET != 0){    cout<<"connect failed";    return -1;}else{ cout<<"链接成攻"<

}

转载于:https://blog.51cto.com/14122754/2382428

你可能感兴趣的文章
openNebula Image上传
查看>>
zabbbix3.2部署方案
查看>>
WAS ND 启动节点(startNode)失败
查看>>
struts2将数据通过Json格式显示于EasyUI-datagrid数据表格
查看>>
js/Jquery 对像不存在或为空的原因
查看>>
datagrid不能显示数据,原因在于JSON字符串存在特殊字符
查看>>
squid代理配置与应用
查看>>
sublime text 批量删除空白行
查看>>
品牌机与组装机
查看>>
三维全景虚拟现实
查看>>
网传Linux运维面试题解答(一)
查看>>
【育儿日记3】兔子会关心大人了
查看>>
Android:实现TabWidget选项卡按钮在屏幕下方
查看>>
T-sql 操作数据列的操作
查看>>
localhost:8080 自动变成了 IP 地址
查看>>
一个监视文件夹文件数量的脚本(可发送警告邮件)
查看>>
mariadb常用的客服端及其服务端命令
查看>>
Linux用户基础
查看>>
ARM与X86架构终端特性对比
查看>>
linux创建磁盘分区的脚本文件(创建两个512M和2G的主分区)
查看>>