C# 使用多线程实现TCP并发服务器

本文详细介绍了如何使用C#编程语言,结合多线程技术,构建一个能够处理并发连接的TCP服务器。通过实例代码,阐述了在服务器端创建线程来服务每个客户端连接,以提高服务器的并发性能,确保高效稳定地处理网络通信。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#define N 128
#define ERRLOG(msg) do{\
        printf("%s %s %d:", __FILE__, __func__, __LINE__);\
        perror(msg);\
        exit(-1);\
}while(0)

void *data_recv_send(void *arg){
    msg_t msg = *(msg_t *)arg;
    int nbytes = 0;
    char buff[N] ={0};
    printf("客户端[%s:%d]连接到服务器\n",inet_ntoa(msg.clientaddr.sin_addr),ntohs(msg.clientaddr.sin_port));
    while(1){
        memset(buff,0,N);
        //接收客户端发来的数据
        if(-1 == (nbytes = recv(msg.acceptfd,buff,N,0))){
            ERRLOG("recv error");
        }else if(0 == nbytes){
            printf("客户端[%s:%d]与服务器断开连接...\n",inet_ntoa(msg.clientaddr.sin_a
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值