240702任伟超8.20作业

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

struct sockaddr_in serve_addr;
struct sockaddr_in client_addr;
pthread_t tid1;
pthread_t tid2;
int sock_fd = 0;
int con_fd =-1;

void * sendfun(void *arg)
{
	char *s_buf = malloc(100);
	while(1)
	{
		memset(s_buf,0,100);
		fgets(s_buf,100,stdin);

		send(sock_fd,s_buf,100,0);
		if(strcmp(s_buf,"bye\n")==0)
		{
			break;
		}

	}

	close(sock_fd);
	pthread_cancel(tid1);

}

void * recvfun(void *arg)
{
	char *r_buf = malloc(100);
	while(1)
	{
		memset(r_buf,0,100);
		int n = recv(sock_fd,r_buf,100,0);
		if(0 == n)
		{
			printf("已退出\n");
			break;
		}
		if(strcmp(r_buf, "bye\n")==0)
		{
			break;
		}

		printf("recv:%s\n", r_buf);

	}

	close(sock_fd);
	pthread_cancel(tid2);

}


int main(int argc, const char *argv[])
{
	//1.创建套接字
	sock_fd = socket(AF_INET,SOCK_STREAM,0);


	//结构体初始化
	serve_addr.sin_family = AF_INET;
	serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
	serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序

	socklen_t serve_len = sizeof(serve_addr);
	socklen_t cilent_len = sizeof(client_addr);
	//2.绑定  
	bind(sock_fd,(struct sockaddr *)&serve_addr,sizeof(serve_addr));

	//3.监听
	listen(sock_fd,4);
	//4.连接
	printf("等待连接...\n");
	con_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &cilent_len);//阻塞运行
	//有对端地址,怎么访问到?
	client_addr.sin_port;

	printf("端口号:[%d],%s连接成功...\n", ntohs(client_addr.sin_port), inet_ntoa(client_addr.sin_addr) );
	accept(sock_fd,NULL,NULL);

	pthread_create(&tid1,NULL,sendfun,NULL);
	pthread_create(&tid2,NULL,recvfun,NULL);

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

	return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

struct sockaddr_in client_addr;
struct sockaddr_in serve_addr;
pthread_t tid1;
pthread_t tid2;
int con_fd = 0;


void * sendfun(void *arg)
{
	char *s_buf = malloc(100);
	while(1)
	{
		memset(s_buf,0,100);
		fgets(s_buf,100,stdin);

		send(con_fd,s_buf,100,0);
		if(strcmp(s_buf,"bye\n")==0)
		{
			break;
		}

	}

	close(con_fd);
	pthread_cancel(tid1);

}

void * recvfun(void *arg)
{
	char *r_buf = malloc(100);
	while(1)
	{
		memset(r_buf,0,100);
		int n = recv(con_fd,r_buf,100,0);
		if(0 == n)
		{
			printf("已退出\n");
			break;
		}
		if(strcmp(r_buf, "bye\n")==0)
		{
			break;
		}

		printf("recv:%s\n", r_buf);

	}

	close(con_fd);
	pthread_cancel(tid2);

}


int main(int argc, const char *argv[])
{
	//1.创建套接字
	con_fd = socket(AF_INET,SOCK_STREAM,0);


	//结构体初始化
	client_addr.sin_family = AF_INET;
	client_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
	client_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序

	socklen_t client_len = sizeof(client_addr);
	//2.绑定  
	bind(con_fd,(struct sockaddr *)&serve_addr,sizeof(serve_addr));

	connect(con_fd,(struct sockaddr *)&serve_addr,sizeof(serve_addr));

	pthread_create(&tid1,NULL,sendfun,NULL);
	pthread_create(&tid2,NULL,recvfun,NULL);

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

	return 0;
}

ls版:

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

int con_fd = -1;

void *recv_to_cilent(void *arg)
{
	
	char *buf = malloc(100);
	while(1)
	{
		memset(buf,0,100);
		recv(con_fd,buf,100,0);
		printf("from cilent:%s\n",buf);
		
	}
}

//一对一双向
int main(int argc,char **argv)
{
	int sock_fd = socket(AF_INET,SOCK_STREAM,0);
	
	struct sockaddr_in serve, cilent;
	
	serve.sin_family = AF_INET;
	serve.sin_port = htons(atoi(argv[2]));
	serve.sin_addr.s_addr = inet_addr(argv[1]);
	
	socklen_t serve_len = sizeof(serve);  
	socklen_t cilent_len =sizeof(cilent); 
	
	bind(sock_fd,&serve,serve_len);
	
	listen(sock_fd,4);
	
	printf("等待连接.....\n");
	con_fd = accept(sock_fd,&cilent,&cilent_len);
	printf("连接成功!!!!\n");
	
	pthread_t tid;
	pthread_create(&tid,NULL, recv_to_cilent,NULL);
	
	
	char *buf = malloc(100);
	while(1)
	{
		memset(buf,0,100);
		fgets(buf,100,stdin);
		send(con_fd,buf,100,0);
	}
}
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

int sock_fd = -1;

void *cilent_to_serve(void *arg)
{
	char *buf = malloc(100);
	while(1)
	{
		memset(buf,0,100);
		fgets(buf,100,stdin);
		send(sock_fd,buf,100,0);
	}
}



int main(int argc,char **argv)
{
	sock_fd = socket(AF_INET,SOCK_STREAM,0);
	
	struct sockaddr_in serve, cilent;
	
	serve.sin_family = AF_INET;
	serve.sin_port = htons(atoi(argv[2]));
	serve.sin_addr.s_addr = inet_addr(argv[1]);
	
	socklen_t serve_len = sizeof(serve);  
	socklen_t cilent_len =sizeof(cilent); 
	
	bind(sock_fd,&serve,serve_len);
	
	connect(sock_fd,&serve,serve_len);
	
	pthread_t tid;
	pthread_create(&tid,NULL, cilent_to_serve,NULL);
	
	char *buf = malloc(100);
	while(1)
	{
		memset(buf,0,100);
		recv(sock_fd,buf,100,0);
		printf("from serve:%s\n",buf);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值