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)
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值