topic

来源:互联网 发布:网络炒作事件 编辑:程序博客网 时间:2024/05/18 02:14

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 6666int 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--;}}



原创粉丝点击