TCP多线程服务端-客户端模板(Linux下)

简介: TCP多线程服务端-客户端模板(Linux下)

服务器模板

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <malloc.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define PORT 9999   // 端口
void *client_thread(void *arg);
//多线程 并发服务器
int main(int argc, char const *argv[])
{
    int ret;
    int sockfd;  //套接字描述符
    int connfd;
    char buf[1024];
    int fp;
    sockfd = socket(AF_INET, SOCK_STREAM, 0); //创建通信套接字ipv4协议 tcp通信
    if(sockfd==-1){
        printf("socket failed\n");
        exit(-1);
    }
    //定义addr存入本机地址信息
    struct sockaddr_in addr;
    addr.sin_family =  AF_INET   ;  //协议
    addr.sin_port =  htons(PORT) ;  //端口
    addr.sin_addr.s_addr = inet_addr("0") ; //ip  0代表本机
    //绑定地址信息(sockfd + addr)
    ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)); 
    if(ret==-1){
        printf("bind failed\n");
        exit(-1);
    }
    listen(sockfd,255);  //建立监听队列,并监听状态
    while(1){
        printf("wait...\n");
        connfd =  accept(sockfd, NULL, NULL); //建立连接
        printf("connect a client\n");
        // 创建子线程
        pthread_t pid;
        pthread_create(&pid, NULL, client_thread, (void *)&connfd);
        pthread_detach(pid);
    }
    close(sockfd);
    return 0;
}
// 线程函数
void *client_thread(void *arg)
{
    int connfd = *(int *)arg;
    char buf[256];  // 接收缓冲区,大小根据你的需求定
    while(1) {
      // 你的程序逻辑
        // 接收函数: ret = read(connfd, buf, sizeof(buf));   // 返回值为实际接收的字节数,ret <= 0 表示客户端断开连接
        // 发送函数: ret = write(connfd, buf, sizeof(buf));  // 返回值为实际发送的字节数
    }
}

客户端模板

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define HOST "192.168.1.108"  // 服务器地址
#define PORT 9999  // 服务器端口
//搭建tcp客服端
int main(int argc, char const *argv[])
{
  int ret,ret1,ret2;
  int sockfd; //套接字描述符
  srand(time(NULL));
  sockfd = socket(AF_INET ,SOCK_STREAM, 0); //创建通信套接字(TCP)
  if(sockfd == -1)
  {
    printf("socket failed\n");
    exit(-1);
  }
  //定义addr存入本机地址信息
  struct sockaddr_in addr;
    addr.sin_family = AF_INET; //协议
    addr.sin_port = htons(PORT);    //端口
    addr.sin_addr.s_addr= inet_addr(HOST); //服务器地址
    ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));  //连接tcp客服端
    if(ret == -1)
    {
      printf("connect failed1\n");
      return 0;
    }
    printf("connect success!\n");
    while(1)
    {
      // 你的程序逻辑
        // 接收函数: ret = read(connfd, buf, sizeof(buf));   // 返回值为实际接收的字节数
        // 发送函数: ret = write(connfd, buf, sizeof(buf));  // 返回值为实际发送的字节数
    }
    close(sockfd); //关闭通信套接字(TCP)
}
目录
相关文章
|
6月前
|
NoSQL IDE MongoDB
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
403 3
|
2月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
519 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
3月前
|
NoSQL IDE MongoDB
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
310 1
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
6月前
|
NoSQL IDE MongoDB
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
523 21
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
7月前
|
Linux 虚拟化 iOS开发
Rocky Linux 10 x86_64 OVF (sysin) - VMware 虚拟机模板
Rocky Linux 10 x86_64 OVF (sysin) - VMware 虚拟机模板
438 35
Rocky Linux 10 x86_64 OVF (sysin) - VMware 虚拟机模板
|
7月前
|
Linux 虚拟化 iOS开发
Rocky Linux 10 aarch64 OVF (sysin) - Apple silicon VMware 虚拟机模板
Rocky Linux 10 aarch64 OVF (sysin) - Apple silicon VMware 虚拟机模板
281 34
Rocky Linux 10 aarch64 OVF (sysin) - Apple silicon VMware 虚拟机模板
|
5月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
1221 0
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
6月前
|
Java Linux 开发工具
Linux下版本控制器(SVN) -命令行客户端
Linux下版本控制器(SVN) -命令行客户端
196 4
|
9月前
|
NoSQL IDE MongoDB
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
365 2
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
8月前
|
SQL Linux 数据库
YashanDB Linux客户端安装
本文详细介绍了YashanDB客户端在Linux系统中的安装、使用与卸载步骤。安装方法包括适用于所有Linux平台的脚本安装和专用于CentOS的rpm安装。脚本安装需解压软件包并配置环境变量,而rpm安装则需以root用户执行相关命令。此外,文章还说明了如何通过yasql连接YashanDB并进行数据库操作,以及两种安装方式对应的卸载方法,帮助用户顺利完成客户端的管理与维护。