topic

6 篇文章 0 订阅

1.create server and client;

2.client create two thread,one create random number between 0-50,the other create random number between 51-100,(1s  interval, 10 times).

3.server recive number by one thread at first;

4.if number less than 50,write in file"small.txt",otherwise ,write in file"big.txt".

头文件:

#ifndef _MYHEAD_H_
#define _MYHEAD_H_

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

#define MYIP "192.168.1.1"
#define MYPORT 6666

int ret = 0;


#endif


//server
/*************************************************************************
    > File Name: server.c
    > Author: zz
    > Mail: zzzkkk@outlook.com 
    > Created Time: 2017年09月04日 星期一 22时49分44秒
 ************************************************************************/

#include "myhead.h"
int queue[20] = {0};
void* insert_queue(void* cfd);

int main()
{
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd == -1)
	{
		perror("socket");
		exit(1);
	}
	printf("socket...\n");
	
	struct sockaddr_in my_addr = {0};
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(MYPORT);
	my_addr.sin_addr.s_addr = inet_addr(MYIP);
	
	ret = bind(sfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("bind");
		exit(1);
	}
	printf("bind...\n");
	
	ret = listen(sfd, 10);
	if(-1 == ret)
	{
		perror("listen");
		exit(1);
	}
	printf("listen...\n");
	
	struct sockaddr_in cn_addr = {0};
	int len = sizeof(struct sockaddr);
	int cfd = accept(sfd, (struct sockaddr*)&cn_addr, &len);
	if(-1 == cfd)
	{
		perror("accept");
		exit(1);
	}
	printf("client is %d\n", cfd);
	printf("accept success, clientIP %s, port %d...\n", inet_ntoa(cn_addr.sin_addr), ntohs(cn_addr.sin_port));
	
	pthread_t th;
	pthread_create(&th, NULL, insert_queue, &cfd);
	pthread_join(th, NULL);
	
	int i = 0;
	FILE* f1;
	FILE* f2;
	f1 = fopen("./small.txt", "w");			
	f2 = fopen("./big.txt", "w");
	while(i != 20)
	{
		if(queue[i] < 50)
		{
			//fseek(f1, 0, SEEK_END);	
			char str[1024] = {0};
			fprintf(f1, "%2d, ", queue[i]);
			//fwrite(str, sizeof(char), 4, f1);
		}
		else
		{
			//fseek(f2, 0, SEEK_END);
			char str[1024] = {0};
			fprintf(f2, "%2d, ", queue[i]);
			//fwrite(str, sizeof(char), 4, f2);
		}
		i++;
	}
	
	fclose(f1);
	fclose(f2);
	return 0;
}

void* insert_queue(void* cfd)
{
	int fd = *((int*)cfd);
	int num = 0;
	int i = 0;
	while(i != 20)
	{
		recv(fd, (void*)&num, sizeof(int), 0);
		num = ntohl(num);
		queue[i] = num;
		i++;
	}
}

//client

/*************************************************************************
    > File Name: client.c
    > Author: zz
    > Mail: zzzkkk@outlook.com 
    > Created Time: 2017年09月04日 星期一 22时49分55秒
 ************************************************************************/

#include "myhead.h"

void* ran0_50(void* argc);
void* ran51_100(void* argc);

int main()
{
	int sfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sfd == -1)
	{
		perror("socket");
		exit(1);
	}
	printf("socket...\n");
	
	struct sockaddr_in my_addr = {0};
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(MYPORT);
	my_addr.sin_addr.s_addr = inet_addr(MYIP);
	
	ret = connect(sfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("connect");
		exit(1);
	}
	printf("connect...\n");
	
	pthread_t th1;
	pthread_t th2;
	
	ret = pthread_create(&th1, NULL, (void*)ran0_50, &sfd);
	if(ret)
	{
		perror("thread1 create");
		exit(1);
	}
	
	ret = pthread_create(&th2, NULL, (void*)ran51_100, &sfd);
	if(ret)
	{
		perror("thread2 create");
		exit(1);
	}
	
	while(1)
	{}
		
	return 0;
}

void* ran0_50(void* argc)
{
	int fd = *((int*)argc);
	int num = 0;
	srand((unsigned)time(NULL));
	int i = 10;
	while(i)
	{
		num = rand()%50 + 0;	
		printf("th1 %d\n", num);	
		sleep(1);
		num = htonl(num);
		send(fd, (void*)&num, sizeof(int), 0);
		i--;
	}
}

void* ran51_100(void* argc)
{
	int fd = *((int*)argc);
	int num = 0;
	srand((unsigned)time(NULL));
	int i = 10;
	while(i)
	{
		num = rand()%50 + 51;	
		printf("th2 %d\n", num);
		sleep(1);
		num = htonl(num);
		send(fd, (void*)&num, sizeof(int), 0);
		i--;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值