C++ TCP winsock 多线程编程

C++ TCP winsock 多线程编程,第1张

概述C++ TCP winsock 多线程编程

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

// Thread.cpp : 定义控制台应用程序的入口点。//  #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];    //初始化 recvBuffer    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;    //init windows socket    if (WSAStartup(MAKEWORD(2,2),&WSA)!= 0 )    {        cout<<"init Failed"<<endl;        return -1;    }    //creat socket    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,8);    //bind socket    RET = bind(severScoket,(struct sockaddr*)&LocalAddr,sizeof(LocalAddr));    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);       clIEntScoket = accept(severScoket,(struct sockaddr*)&clIEntAddr,&AddrLen);       if (clIEntScoket ==INVALID_SOCKET)       {           cout<<"accept Failed";           break;       }       cout<<"客户端连接"<<inet_ntoa(clIEntAddr.sin_addr)<<"."<<clIEntAddr.sin_port<<endl;       hThread  = CreateThread(NulL,ClIEntThread,(LPVOID)clIEntScoket,NulL);       if(hThread == NulL)       {           cout<<"creat thread Failed"<<endl;           break;       }       CloseHandle(hThread);    }    closesocket(severScoket);    closesocket(clIEntScoket);    WSACleanup();    return 0;}  

// ClIEnt.cpp : 定义控制台应用程序的入口点。
#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];    //init windows socket    if (WSAStartup(MAKEWORD(2,&WSA)!= 0 )    {        cout<<"init Failed"<<endl;        return -1;    }    //creat socket    clIEntScoket = socket(AF_INET,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,8);      RET = connect(clIEntScoket,(struct sockaddr*)&serveAddr,sizeof(serveAddr));    if (RET != 0)    {        cout<<"connect Failed";        return -1;    }    else{ cout<<"链接成攻"<<endl;}    while(true)    {        cin.getline(SendBuffer,sizeof(SendBuffer));        RET = send(clIEntScoket,SendBuffer,(int)strlen(SendBuffer),0);        if (RET == SOCKET_ERROR)        {            cout<<"send to error"<<endl;            break;        }    }    closesocket(clIEntScoket);    WSACleanup();    return 0;}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C++ TCP winsock 多线程编程全部内容,希望文章能够帮你解决C++ TCP winsock 多线程编程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/langs/1232277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存