【LINUX多线程编程】C语言实现多线程

本文主要讲解了如何使用多线程,多个客户端同时给服务端发消息,显示在服务端上面。(TCP协议单个服务端对多个客户端)

服务端代码

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>

#define SERV_IP "127.0.0.1"
#define SERV_PORT 8088
#define APP_TRACE printf

void data_mutual(int fd)
{
	while (1)
	{
	
		int ret;
		char client_buf[1024] = "";
		char server_buf[1024] = "";
	
		ret = recv(fd, client_buf, 1024, 0);

		if(ret < 0)
		{
			APP_TRACE("recv failed!\n");
		}else if(ret == 0)
		{
			APP_TRACE("client[%d] exit!\n", fd);
			close(fd);
			fd = -1;
			pthread_exit(0);
		}
		else
		{
			APP_TRACE("client[%d] send:[%s]\n", fd, client_buf);
		}
	}
}

//TCP server
int main()
{
	APP_TRACE("this is tcp server\n");
	int fd = -1;

	if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		APP_TRACE("socket create failed!!!\n");
	}

	struct sockaddr_in server;
	int ret;
	bzero(&server, sizeof(server));

	server.sin_family = AF_INET;
	server.sin_port = htons(SERV_PORT);
	//server.sin_addr.s_addr = inet_addr(SERVER_IP);
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	
	if((bind(fd, (struct sockaddr *)&server, sizeof(server))) < 0)
	{
		APP_TRACE("bind is failed\n");
		goto tcp_error;

	}else
		APP_TRACE("bind success\n");

	
	if((listen(fd, 10)) < 0)
	{
		APP_TRACE("listen is failed\n");	
		goto tcp_error;
	}else
		APP_TRACE("listen success\n");

	int newfd;
	int client_len;
	struct sockaddr_in client;

	
	while(1)
	{

		pthread_t id;
		newfd = accept(fd, (struct sockaddr *)&client, &client_len);
		APP_TRACE("connect success %d\n", newfd);

		pthread_create(&id, NULL, (void *)data_mutual, newfd);


		
	}

tcp_error:
	close(fd);
	fd = -1;
}

客户端代码

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>

#define CLIENT_PORT 8088
#define CLIENT_IP "127.0.0.1"
#define APP_TRACE printf
//TCP client
int main()
{
	
	int fd = -1;

	fd = socket(AF_INET, SOCK_STREAM, 0);
	if(fd < 0)
	{
		APP_TRACE("fd create failed!\n");
	}

	struct sockaddr_in server;
	bzero(&server, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(CLIENT_PORT);
	server.sin_addr.s_addr = inet_addr(CLIENT_IP);
	int ret;
	ret = connect(fd, (struct sockaddr *)&server, sizeof(server));

	if(ret < 0)
	{
		APP_TRACE("connect failed\n");
		goto client_error;
	}else
		APP_TRACE("connect success!\n");

	while (1)
	{
		char content[1024] = "";
		char ser_buf[1024] = "";
		APP_TRACE("please enter content:");
		scanf("%s", content);

		send(fd, content, strlen(content), 0);

//		recv(fd, ser_buf, 1024, 0);
//		APP_TRACE("SERVER send content[%s]\n", ser_buf);
	}


client_error:
	fd = -1;
	close(fd);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

April.19th

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值