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

void *pid_write(void *sockfd);
void *pid_read(void *sockfd);

int main()
{
	//1,socket
	int sockfd = -1;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0){
		printf("create socket error!\n");
		exit(1);
	}
	printf("sockfd = %d\n",sockfd);
	//2,set struct
	struct sockaddr_in ser_addr;
	memset(&ser_addr,0,sizeof(struct sockaddr));
	ser_addr.sin_family = AF_INET;
	ser_addr.sin_port = htons(8089);
	//int ip;
	inet_pton(AF_INET,"127.0.0.1",&ser_addr.sin_addr.s_addr);
	//ser_addr.sin_addr.s_addr =  INADDR_ANY;//htonl(ip);	

	int ret = -1;
	//3,connect
	ret = connect(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));
	if(ret < 0){
		printf("connect error!\n");
		exit(2);
	}
	else
		printf("connect success!\n");
	//4,write
	pthread_t pid_w;
	ret = pthread_create(&pid_w,NULL,pid_write,(void *)sockfd);
	pthread_t pid_r;
	ret = pthread_create(&pid_r,NULL,pid_read,(void *)sockfd);
	pthread_join(pid_w,NULL);
	pthread_join(pid_r,NULL);





	return 0;
}
void *pid_write(void *sockfd)
{
	int ret = -1;
	char buf[128] = {0};
	while(1){
		memset(buf,0,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';
		ret = write((int)sockfd,buf,strlen(buf));
		if(strcmp(buf,"exit") == 0){
			printf("write exit success!\n");
			break;
		}
	}
}

void *pid_read(void *sockfd)
{
	char buf[128] = {0};
	ssize_t cnt = -1;
	while(1){
		memset(buf,0,sizeof(buf));
		cnt = read((int)sockfd,buf,sizeof(buf)); 
		if(cnt > 0){
			printf("\033[31m%s\033[0m\n",buf);
		}
		else if(cnt == 0){
			printf("read null!\n");
			break;
		}
		if(strcmp(buf,"exit") == 0){
			printf("read exit success!\n");
			break;
		}
	}
}
















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

void *pid_write(void *sockfd);
void *pid_read(void *sockfd);

int main()
{
	//1,socket
	int sockfd = -1;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0){
		printf("create socket error!\n");
		exit(1);
	}
	printf("sockfd = %d\n",sockfd);
	//2,set struct
	struct sockaddr_in ser_addr;
	memset(&ser_addr,0,sizeof(struct sockaddr));
	ser_addr.sin_family = AF_INET;
	ser_addr.sin_port = htons(8089);
	//int ip;
	inet_pton(AF_INET,"127.0.0.1",&ser_addr.sin_addr.s_addr);
	//ser_addr.sin_addr.s_addr =  INADDR_ANY;//htonl(ip);	

	int ret = -1;
	//3,connect
	ret = connect(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));
	if(ret < 0){
		printf("connect error!\n");
		exit(2);
	}
	else
		printf("connect success!\n");
	//4,write
	pthread_t pid_w;
	ret = pthread_create(&pid_w,NULL,pid_write,(void *)sockfd);
	pthread_t pid_r;
	ret = pthread_create(&pid_r,NULL,pid_read,(void *)sockfd);
	pthread_join(pid_w,NULL);
	pthread_join(pid_r,NULL);





	return 0;
}
void *pid_write(void *sockfd)
{
	int ret = -1;
	char buf[128] = {0};
	while(1){
		memset(buf,0,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';
		ret = write((int)sockfd,buf,strlen(buf));
		if(strcmp(buf,"exit") == 0){
			printf("write exit success!\n");
			break;
		}
	}
}

void *pid_read(void *sockfd)
{
	char buf[128] = {0};
	ssize_t cnt = -1;
	while(1){
		memset(buf,0,sizeof(buf));
		cnt = read((int)sockfd,buf,sizeof(buf)); 
		if(cnt > 0){
			printf("\033[31m%s\033[0m\n",buf);
		}
		else if(cnt == 0){
			printf("read null!\n");
			break;
		}
		if(strcmp(buf,"exit") == 0){
			printf("read exit success!\n");
			break;
		}
	}
}

















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

void *pid_write(void *sockfd);
void *pid_read(void *sockfd);

int main()
{
	//1,socket
	int sockfd = -1;
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd < 0){
		printf("create socket error!\n");
		exit(1);
	}
	printf("sockfd = %d\n",sockfd);
	//2,set struct
	struct sockaddr_in ser_addr;
	memset(&ser_addr,0,sizeof(struct sockaddr));
	ser_addr.sin_family = AF_INET;
	ser_addr.sin_port = htons(8089);
	//int ip;
	inet_pton(AF_INET,"127.0.0.1",&ser_addr.sin_addr.s_addr);
	//ser_addr.sin_addr.s_addr =  INADDR_ANY;//htonl(ip);	

	int ret = -1;
	//3,connect
	ret = connect(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));
	if(ret < 0){
		printf("connect error!\n");
		exit(2);
	}
	else
		printf("connect success!\n");
	//4,write
	pthread_t pid_w;
	ret = pthread_create(&pid_w,NULL,pid_write,(void *)sockfd);
	pthread_t pid_r;
	ret = pthread_create(&pid_r,NULL,pid_read,(void *)sockfd);
	pthread_join(pid_w,NULL);
	pthread_join(pid_r,NULL);





	return 0;
}
void *pid_write(void *sockfd)
{
	int ret = -1;
	char buf[128] = {0};
	while(1){
		memset(buf,0,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';
		ret = write((int)sockfd,buf,strlen(buf));
		if(strcmp(buf,"exit") == 0){
			printf("write exit success!\n");
			break;
		}
	}
}

void *pid_read(void *sockfd)
{
	char buf[128] = {0};
	ssize_t cnt = -1;
	while(1){
		memset(buf,0,sizeof(buf));
		cnt = read((int)sockfd,buf,sizeof(buf)); 
		if(cnt > 0){
			printf("\033[31m%s\033[0m\n",buf);
		}
		else if(cnt == 0){
			printf("read null!\n");
			break;
		}
		if(strcmp(buf,"exit") == 0){
			printf("read exit success!\n");
			break;
		}
	}
}