Linux----网络编程(TCP网络通信服务器客户端编程实现多线程)

本文档详细介绍了如何在Linux环境下使用TCP进行网络通信,通过ser.c实现服务器端,利用多线程处理客户端请求,同时提供了客户端cli.c的代码,实现了与服务器的有效交互。
摘要由CSDN通过智能技术生成

1、服务器端ser.c

1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <assert.h>
  5 #include <string.h>
  6 #include <sys/socket.h>
  7 #include <netinet/in.h>
  8 #include <arpa/inet.h>
  9 #include <pthread.h>
 10 
 11 //客户端服务器实现多线程
 12 void* fun(void * arg)
 13 {
 14     int c = (int)arg;
 15     while(1)
 16     {
 17         char buff[128] = {0};
 18         int n = recv(c, buff, 127, 0);
 19         if(n <= 0)
 20         {
 21             break;
 22             }
 23         printf("buff(%d)=%s\n",c,buff);
 24         send(c, "ok", 2, 0);
 25         }
 26     printf("one client over\n");
 27     close(c);
 28     }
 29 
 30 int main()
 31 {
 32     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
 33     assert(sockfd != -1);
 34 
 35     struct sockaddr_in saddr, caddr;
 36     me
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值