资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
多线程:#include #include #include #include #include int task1(int *cnt)while(*cnt 5)sleep(1);(*cnt)+;printf(task1 cnt = %d.n, *cnt);return (*cnt);int task2(int *cnt)while(*cnt 5)sleep(2);(*cnt)+;printf(task2 cnt = %d.n, *cnt);return (*cnt);int main(int argc, char *argv)int result;int t1 = 0;int t2 = 0;int rt1, rt2;pthread_t thread1, thread2;/* create the first thread. */result = pthread_create(&thread1, PTHREAD_CREATE_JOINABLE, (void *)task1, (void *)&t1);if(result)perror(pthread_create: task1.n);exit(EXIT_FAILURE);/* create the second thread. */result = pthread_create(&thread2, PTHREAD_CREATE_JOINABLE, (void *)task2, (void *)&t2);if(result)perror(pthread_create: task2.n);exit(EXIT_FAILURE);pthread_join(thread1, (void *)&rt1);pthread_join(thread2, (void *)&rt2);printf(total %d times.n, t1+t2);printf(return value of task1: %d.n, rt1);printf(return value of task2: %d.n, rt2);exit(EXIT_SUCCESS);TCP(sever):#include #include #include #include #include #include #include #include #define PORT 5000 / The port which is communicate with server#define BACKLOG 10#define LENGTH 512 / Buffer length int main () int sockfd; / Socket file descriptor int nsockfd; / New Socket file descriptor int num; int sin_size; / to store struct size char sdbufLENGTH; / Send buffer struct sockaddr_in addr_local; struct sockaddr_in addr_remote; char sendstr16= 123456789 abcde; /* Get the Socket file descriptor */ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0) = -1 ) printf (ERROR: Failed to obtain Socket Despcritor.n); return (0); else printf (OK: Obtain Socket Despcritor sucessfully.n); /* Fill the local socket address struct */ addr_local.sin_family = AF_INET; / Protocol Family addr_local.sin_port = htons(PORT); / Port number addr_local.sin_addr.s_addr = INADDR_ANY; / AutoFill local address bzero(&(addr_local.sin_zero), 8); / Flush the rest of struct /* Blind a special Port */ if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr) = -1 ) printf (ERROR: Failed to bind Port %d.n,PORT); return (0); else printf(OK: Bind the Port %d sucessfully.n,PORT); /* Listen remote connect/calling */ if(listen(sockfd,BACKLOG) = -1) printf (ERROR: Failed to listen Port %d.n, PORT); return (0); else printf (OK: Listening the Port %d sucessfully.n, PORT); while(1) sin_size = sizeof(struct sockaddr_in); /* Wait a connection, and obtain a new socket file despriptor for single connection */ if (nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size) = -1) printf (ERROR: Obtain new Socket Despcritor error.n); continue; else printf (OK: Server has got connect from %s.n, inet_ntoa(addr_remote.sin_addr); /* Child process */ if(!fork() printf(You can enter string, and press exit to end the connect.n); while(strcmp(sdbuf,exit) != 0) scanf(%s, sdbuf); if(num = send(nsockfd, sdbuf, strlen(sdbuf), 0) = -1) printf(ERROR: Failed to sent string.n); close(nsockfd); exit(1); printf(OK: Sent %d bytes sucessful, please enter again.n, num); close(nsockfd); while(waitpid(-1, NULL, WNOHANG) 0); TCP(client):#include #include #include #include #include #include #include #include #define PORT 5000 / The port which is communicate with server#define LENGTH 256 / Buffer lengthint main(int argc, char *argv) int sockfd; / Socket file descri
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号