Linux下基于TCP的线程通信

Linux下基于TCP的线程通信

/*
*server.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <pthread.h>


#define BACKLOG 10
#define MYPORT 8888
#define SIZE 1024

int sfd, new_fd;

void receive_message()
{
	int nbytes;
	char buf[SIZE];
	while(1)
	{
		bzero(buf, SIZE);
		nbytes = recv(new_fd, buf, SIZE, 0);
		if(nbytes<=0)
		{
			perror("recv");
			exit(1);
		}

		buf[nbytes] = '\0';		
		if(strcmp(buf, "quit") == 0)
		{
			printf("Client is quit\n");
			close(new_fd);
			close(sfd);
			exit(1);
		}
		printf("Receive message from cLient: %s\n", buf);
	}
}


void accept_connect()
{
	int sin_size;
	int ret = 0;
	pthread_t tid;
	struct sockaddr_in client_addr;

	sin_size = sizeof(struct sockaddr_in);
	new_fd = accept(sfd, (struct sockaddr*)&client_addr, &sin_size);
	if(-1 == new_fd)
	{
		perror("accept");
		exit(1);
	}
	
	printf("Server get connected from %s\n", inet_ntoa(client_addr.sin_addr));

	ret = pthread_create(&tid, NULL, (void *)receive_message, NULL);
	if(ret != 0)
	{
		printf("pthread create failed\n");
		exit(1);
	}
}

int main()
{
	int ret=0;
	pthread_t  tid;
	struct sockaddr_in myaddr;

	sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1== sfd)
	{
		perror("socket");
		close(sfd);
		exit(1);
	}

	bzero(&myaddr, sizeof(myaddr));
	myaddr.sin_family = AF_INET;
	myaddr.sin_port = htons(MYPORT);
	myaddr.sin_addr.s_addr = INADDR_ANY;
	
	if(bind(sfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr)) == -1)
	{
		perror("bind()");
		close(sfd);
		exit(1);
	}

	if(listen(sfd, BACKLOG) == -1)
	{
		perror("listen()");
		close(sfd);
		exit(1);
	}
	
	ret = pthread_create(&tid, NULL, (void *)accept_connect, NULL);
	if(0 != ret)
	{
		printf("pthread create failed\n");
		exit(1);
	}

	char msg[SIZE];
	while(1)
	{
		bzero(msg, SIZE);
		scanf("%s", msg);
		
		if(send(new_fd, msg, strlen(msg), 0) == -1)
		{
			perror("send()");
			close(new_fd);
			exit(1);
		}
		
		if(strcmp(msg, "quit") == 0)
		{
			printf("Byebye!\n");
			close(new_fd);
			close(sfd);
			exit(1);
		}
	}
	return 0;
}

/*
*client.c
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>


#define BACKLOG 10
#define MYPORT 8888
#define SIZE 1024

int sfd;

void receive_message()
{
	int nbytes;
	char buf[SIZE];
	while(1)
	{
		bzero(buf, SIZE);
		nbytes = recv(sfd, buf, SIZE, 0);
		if(nbytes<=0)
		{
			perror("recv()");
			exit(1);
		}

		buf[nbytes] = '\0';		
		if(strcmp(buf, "quit") == 0)
		{
			printf("Server is quit\n");
			close(sfd);
			exit(1);
		}
		printf("Receive message from server: %s\n", buf);
	}
}



int main(int argc, char *argv[])
{
	int ret=0;
	char msg[SIZE];
	pthread_t tid;
	struct sockaddr_in server_addr;
	struct hostent *hent;

	if(argc != 2)
	{
		fprintf(stderr, "usage: <client hostname>\n");
		exit(1);
	}


	hent = gethostbyname(argv[1]);
	if(hent == NULL)
	{
		perror("gethostbyname()");
		exit(1);
	}


	sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1== sfd)
	{
		herror("socket");
		close(sfd);
		exit(1);
	}

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(MYPORT);
	server_addr.sin_addr.s_addr = ((struct in_addr *)hent->h_addr)->s_addr;
	//server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	bzero(&server_addr.sin_zero, 8);
	
	if(connect(sfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in))==-1)
	{
		perror("connect()");
		close(sfd);
		exit(1);
	}
	
	ret = pthread_create(&tid, NULL, (void *)receive_message, NULL);
	if(0 != ret)
	{
		printf("pthread create failed\n");
		exit(1);
	}

	while(1)
	{
		bzero(msg, SIZE);
		scanf("%s", msg);
		
		if(send(sfd, msg, strlen(msg), 0) == -1)
		{
			perror("send()");
			close(sfd);
			exit(1);
		}
		
		if(strcmp(msg, "quit") == 0)
		{
			printf("Byebye!\n");
			close(sfd);
			exit(1);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值