Linux的tcp客户端服务器通信

/*
客户端发送hello给服务器
实现单向通信
实现双向通信
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>     
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
//#define send_msg 1	//发送hello
//#define single	2	//单向通信
#define double 3	//双向通信


void *fun_send(void *arg)
{
	char buf[20];
	int newfd = *(int *)arg;
	while(1){
		//bzero(buf,sizeof(buf));
		scanf("%s",buf);
		send(newfd,buf,strlen(buf),0);		
	}
	pthread_exit(NULL);
}
int main(int argc,char *argv[])//服务器ip,服务器端口号
{
#ifdef send_msg
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}	
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
	struct sockaddr_in server_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	int ret_listen = listen(soc_fd,5);
	if(ret_listen<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in client_addr;
	socklen_t addr_len = sizeof(client_addr);
	int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
	if(ret_accept<0){
		perror("accept error");
		exit(1);
	}
	char buf[20];
	bzero(buf,sizeof(buf));
	recv(ret_accept,buf,sizeof(buf),0);	
	printf("%s\n",buf);
	close(ret_accept);
	close(soc_fd);
#endif

#ifdef single
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}	
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
	struct sockaddr_in server_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	int ret_listen = listen(soc_fd,5);
	if(ret_listen<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in client_addr;
	socklen_t addr_len = sizeof(client_addr);
	int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
	if(ret_accept<0){
		perror("accept error");
		exit(1);
	}
	char buf[20];
	int ret_recv;
	while(1){
		bzero(buf,sizeof(buf));
		ret_recv = recv(ret_accept,buf,sizeof(buf),0);	
		if(ret_recv==0){
			close(ret_accept);
			close(soc_fd);
			break;
		}
		printf("%s\n",buf);		
	}
#endif

#ifdef double
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}	
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
	struct sockaddr_in server_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	int ret_listen = listen(soc_fd,5);
	if(ret_listen<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in client_addr;
	socklen_t addr_len = sizeof(client_addr);
	int ret_accept = accept(soc_fd,(struct sockaddr *)&client_addr,&addr_len);
	if(ret_accept<0){
		perror("accept error");
		exit(1);
	}
	int ret_pthread_create;
	pthread_t pthread_id;
	ret_pthread_create = pthread_create(&pthread_id,NULL,fun_send,(void *)&ret_accept);
	if(ret_pthread_create!=0){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);		
	}	
	char buf[20];
	int ret_recv;
	while(1){
		bzero(buf,sizeof(buf));
		ret_recv = recv(ret_accept,buf,sizeof(buf),0);	
		if(ret_recv==0){
			close(ret_accept);
			close(soc_fd);
			break;
		}
		printf("%s\n",buf);		
	}
#endif
	return 0;
}
/*
客户端发送hello给服务器
实现单向通信
实现双向通信
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>     
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
//#define send_msg 1	//发送hello
//#define single	2	//单向通信
#define double 3	//双向通信

void *fun_recv(void *arg)
{
	char buf[20];
	int soc_fd = *(int *)arg;
	int ret_recv;
	while(1){
		bzero(buf,sizeof(buf));
		ret_recv = recv(soc_fd,buf,sizeof(buf),0);	
		if(ret_recv==0){
			close(soc_fd);
			exit(0);
		}
		printf("%s\n",buf);			
	}
	pthread_exit(NULL);
}

int main(int argc,char *argv[])//服务器ip,服务器端口号
{
#ifdef send_msg
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));	
	struct sockaddr_in client_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	client_addr.sin_family = AF_INET;
	client_addr.sin_port = htons(6666);
	client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in server_addr;//绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);		
	int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_connect<0){
		perror("connect error");
		exit(1);
	}	
	char buf[20]="hello world";
	send(soc_fd,buf,strlen(buf),0);	
	close(soc_fd);
#endif

#ifdef single
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));	
	struct sockaddr_in client_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	client_addr.sin_family = AF_INET;
	client_addr.sin_port = htons(6666);
	client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in server_addr;//绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);		
	int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_connect<0){
		perror("connect error");
		exit(1);
	}	
	char buf[20];
	while(1){
		bzero(buf,sizeof(buf));
		gets(buf);
		send(soc_fd,buf,strlen(buf),0);
	}
	close(soc_fd);	
#endif

#ifdef double
	if(argc != 3){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);
	}
	int soc_fd = socket(AF_INET,SOCK_STREAM,0);
	if(soc_fd < 0){
		perror("socket error");
		exit(1);
	}
	int on =1;
	setsockopt(soc_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));	
	struct sockaddr_in client_addr;//客户端绑定客户端信息,服务器绑定服务器信息
	client_addr.sin_family = AF_INET;
	client_addr.sin_port = htons(6666);
	client_addr.sin_addr.s_addr = inet_addr("192.168.90.74");
	
	int ret_bind = bind(soc_fd,(struct sockaddr *)&client_addr,sizeof(client_addr));
	if(ret_bind<0){
		perror("bind error");
		exit(1);
	}
	struct sockaddr_in server_addr;//绑定服务器信息
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);		
	int ret_connect = connect(soc_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));
	if(ret_connect<0){
		perror("connect error");
		exit(1);
	}	
	int ret_pthread_create;
	pthread_t pthread_id;
	ret_pthread_create = pthread_create(&pthread_id,NULL,fun_recv,(void *)&soc_fd);
	if(ret_pthread_create!=0){
		printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
		exit(1);		
	}
	char buf[20];
	while(1){
		bzero(buf,sizeof(buf));
		gets(buf);
		send(soc_fd,buf,strlen(buf),0);
	}
	close(soc_fd);	
	pthread_join(pthread_id,NULL);
#endif
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值