一、头文件
#ifndef __HEAD_H__ //防止头文件被重复定义
#define __HEAD_H__ 防止头文件被重复定义#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <poll.h>
#include <semaphore.h>
#include <pthread.h>#endif 防止头文件被重复定义
二、客户端
思路: socket------>connect------>pthread(建立两个线程,一个负责send发送,一个负责recv接收)------>close
使用自己的IP号
#include "head.h"
int init_tcp_cli(char *ip,unsigned short port)
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockfd)
{
TCP客户端与服务端实现:线程通信示例,

该文章展示了如何使用C语言实现一个简单的TCP客户端和服务端程序。客户端创建两个线程,一个用于发送数据,另一个用于接收。服务端同样使用两个线程,分别处理发送和接收。通过socket接口进行网络通信,使用pthread库创建线程,实现数据的双向传输。
最低0.47元/天 解锁文章
880

被折叠的 条评论
为什么被折叠?



