windows多客户端与liunx-ubuntu服务端进行通信

本文展示了如何使用C++在Linux服务器端和Windows客户端之间建立TCP通信。服务器端采用多线程处理客户端连接,每个客户端的连接由单独的线程处理。客户端通过VS2017编译,能与服务器进行双向数据传输。代码中包含了Windows客户端的WS2_32库的使用,以及Linux服务器端的socket编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows多客户端与liunx-ubuntu服务端进行通信

liunx服务端:

#include <stdio.h>
#include <netinet/in.h>
#include <pthread.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <iostream>


#define MAXLINE 80
#define SERV_PORT 8000

//编译的时候 因为thread不是内置的  要加上gcc main.c -o test -lpthread 
struct s_info {
        struct sockaddr_in cliaddr;
        int connfd;
};
void *do_work(void *arg)
{
        int n,i;
        struct s_info *ts = (struct s_info*)arg;
        char buf[MAXLINE];//在线程自己的用户空间栈开辟的,该线程运行结束的时候,主
控线程就不能操作这块内存了
        char str[INET_ADDRSTRLEN];//INET_ADDRSTRLEN 是宏16个字节

//在创建线程前设置线程创建属性,设为分离态,效率高
        pthread_detach(pthread_self());

        while (1) {
        n = read(ts->connfd, buf, MAXLINE);
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值