C++ TCP winsock 多线程编程详解编程语言

// 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,0x00,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,0,ClientThread,(LPVOID)clientScoket,0,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,2),&WSA)!= 0 ) 
    { 
        cout<<"init failed"<<endl; 
        return -1; 
    } 
    //creat socket 
    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); 
   
    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; 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11110.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论