版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、多线程:#in elude <stdio.h>#in elude <stdlib.h>#in elude vuni std.h>#in elude <sys/ioetl.h>#i nclude <pthread.h>int task1(i nt *cnt)while(*ent v 5)sleep(1);(*e nt)+;pri ntf("task1 ent = %d.n", *e nt);return (*e nt);int task2(i nt *cnt)while(*ent v 5)sleep (2);(*e nt)
2、+;pri ntf("task2 ent = %d.n", *e nt);return (*e nt);int main (i nt arge, ehar *argv)int result;int t1 = 0;int t2 = 0;int rt1, rt2;pthread_t threadl, thread2;/* ereate the first thread. */result = pthread_ereate(&thread1, PTHREAD_CREATE_JOINABl(Eoid *)task1, (void *)&t1);if(result)p
3、error("pthread_create: task1.n"); exit(EXIT_FAILURE);/* create the sec ond thread. */result = pthread_create(&thread2, PTHREAD_CREATE_JOINABLEoid *)task2, (void *)&t2);if(result)perror("pthread_create: task2.n");exit(EXIT_FAILURE);pthreadoi n(thread1, (void *)&rt1); p
4、threadoi n(thread2, (void *)&rt2);prin tf("total %d times.'n", t1+t2);prin tf("return value of task1: %d.n", rt1); prin tf("return value of task2: %d.n", rt2);exit(EXIT_SUCCESS);TCP (sever):#in clude <stdlib.h>#in clude <stdio.h>#in clude verrno .h&g
5、t;#in clude vstri ng.h>#in clude <sys/types.h>#in clude <n eti net/in .h>#in clude <sys/wait.h>#in clude <sys/socket.h>#defi ne PORT 5000#defi ne BACKLOG 10#defi ne LENGTH 512int mai n () int sockfd;int n sockfd;int num;int sin _size;char sdbufLENGTH; struct sockaddr_ in a
6、ddr_local;/ The port which is com muni cate with server/ Buffer len gth/ Socket file descriptor/ New Socket file descriptor/ to store struct size/ Send bufferstruct sockaddr_ in addr_remote;char sen dstr16= "123456789 abcde"/* Get the Socket file descriptor */if( (sockfd = socket(AF_INET,
7、SOCK_STREAM, 0) = -1 )printf ("ERROR: Failed to obta in Socket Despcritor.'n");return (0);elseprintf ("OK: Obtai n Socket Despcritor sucessfully.'n");/* Fill the local socket address struct */addr_local.sin_family = AF_INET;/ Protocol Familyaddr_local.sin_port = hto ns(PO
8、RT);/ Port nu mberaddr_local.sin_addr.s_addr = INADDR_ANY; / AutoFill local address bzero(&(addr_local.sin_zero), 8);/ Flush the rest of struct/*Bli nd a special Port */if( bin d(sockfd, (struct sockaddr* )&addr_local, sizeof(struct sockaddr) = -1 ) printf ("ERROR: Failed to bind Port %
9、d.n",PORT);return (0);elseprin tf("OK: Bi nd the Port %d sucessfully.n",PORT);/*Liste n remote conn ect/calli ng */if(liste n(sockfd,BACKLOG) = -1)printf ("ERROR: Failed to liste n Port %d.n", PORT);return (0);elseprintf ("OK: Liste ning the Port %d sucessfully.n",
10、 PORT);while(1)sin _size = sizeof(struct sockaddr_ in);/* Wait a connection, and obtain a new socket file despriptor for single connection */if (n sockfd = accept(sockfd, (struct sockaddr *)&addr_remote, & sin_size) = -1)printf ("ERROR: Obta in new Socket Despcritor error.'n");
11、con ti nue;elseprintf ("OK:Server has gotconn ectfrom %s. n",inet_n toa(addr_remote.sin_addr);/* Child process */if(!fork()prin tf("You can en ter str ing, and press 'exit' to end the conn ect. n"); while(strcmp(sdbuf,"exit") != 0)scan f("%s", sdbuf);i
12、f(num = send(n sockfd, sdbuf, strle n( sdbuf), 0) = -1)prin tf("ERROR: Failed to sent stri ng.n");close (n sockfd);exit(1);prin tf("OK: Sent %d bytes sucessful, please en ter aga in.n", nu m);close (n sockfd);while(waitpid(-1, NULL, WNOHANG) > 0);TCP( client):#in clude <std
13、io.h>#in clude <stdlib.h>#in clude verrno .h>#in clude vstri ng.h>#in clude <n etdb.h>#in elude <sys/types.h>#in elude <n eti net/in .h>#in elude <sys/soeket.h>/ The port which is com muni cate with server/ Buffer len gth/ Socket file descriptor/ Coun ter of re
14、ceived bytes/ Receive buffer/ Host address in formati on#defi ne PORT 5000#defi ne LENGTH 256int main (i nt argc, char *argv)int sockfd;int num;char revbufLENGTH;struct sockaddr_ in remote_addr;/* Check parameters nu mber */if (argc != 2)printf ("Usage: clie nt HOST IP (ex: ./clie nt 192.168.0.
15、94).n"); return (0);/* Get the Socket file descriptor */if (sockfd = socket(AF_INET, SOCK_STREAM, 0) = -1)prin tf("ERROR: Failed to obta in Socket Descriptor!n"); return (0);/* Fill the socket address struct */remote_addr.sin_family = AF_INET;/ Protocol Familyremote_addr.sin_port = ht
16、o ns(PORT);/ Port nu mberin et_pto n(AF_INET, argv1, & remote_addr.sin_addr); / Net Address bzero(&(remote_addr.sin_zero), 8);/ Flush the rest of struct/* Try to connect the remote */if (conn ect(sockfd, (struct sockaddr *)&remote_addr,sizeof(struct sockaddr) = -1)printf ("ERROR: Fa
17、iled to connect to the host!n");return (0);elseprintf ("OK: Have conn ected to the %sn",argv1);/* Try to connect the server */while (strcmp(revbuf,"exit") != 0)/ Check remoter comma ndbzero(revbuf,LENGTH);num = recv(sockfd, revbuf, LENGTH, 0);switch( num)case -1:prin tf(&quo
18、t;ERROR: Receive stri ng error!n");close(sockfd); return (0);case 0:close(sockfd);return(0);default:printf ("OK: Receviced nu mbytes = %dn", nu m); break;revbuf num = '0'printf ("OK: Receviced str ing is: %sn", revbuf);close (sockfd);return (0);UDP(liste ner ):#in cl
19、ude <stdlib.h>#in clude <stdio.h>#in clude verrno .h>#in clude vstri ng.h>#in clude <sys/types.h>#in clude <n eti net/in .h>#in clude <sys/wait.h>#in elude <sys/socket.h>#defi ne PORT 5000#defi ne BACKLOG 10#define LENGTH 512/ The port which is com muni cate
20、 with server/ Buffer len gthint mai n () int sockfd;int n sockfd;int num;int sin _size;char revbufLENGTH;struct sockaddr_ in addr_local; struct sockaddr_ in addr_remote;/ Socket file descriptor/ New Socket file descriptor/ to store struct size/ Se nd buffer/* Get the Socket file descriptor */if( (so
21、ckfd = socket(AF_INET, SOCK_DGRAM, 0) = -1 )printf ("ERROR: Failed to obta in Socket Despcritor.'n"); return (0);elseprintf ("OK: Obta in Socket Despcritor sucessfully.'n");/* Fill the local socket address struct */addr_local.sin_family = AF_INET; addr_local.sin_port = ht
22、o ns(PORT);/ Protocol Family / Port nu mberaddr_local.sin_addr.s_addr = INADDR_ANY; / AutoFill local address bzero(&(addr_local.sin_zero), 8);/ Flush the rest of struct/*Bli nd a special Port */if( bin d(sockfd, (struct sockaddr* )&addr_local, sizeof(struct sockaddr) = -1 ) printf ("ERR
23、OR: Failed to bind Port %d.n",PORT);return (0);elseprin tf("OK: Bi nd the Port %d sucessfully.n",PORT);sin _size = sizeof(struct sockaddr);if(num = recvfrom(sockfd, revbuf,LENGTH, 0, (struct sockaddr *)&addr_remote,&sin _size) = -1)prin tf("ERROR!n");elseprin tf(&quo
24、t;OK: %s.n",revbuf); close(sockfd); return (0);UDP(talker ):#in clude <stdio.h>#in clude <stdlib.h>#in clude verrno .h>#in clude vstri ng.h>#in clude <n etdb.h>#in clude <sys/types.h>#in clude <n eti net/in .h>#in clude <sys/socket.h>#defi ne PORT 5000/
25、 The port which is com muni cate with server#defi ne LENGTH 512/ Buffer len gthint main (i nt argc, char *argv)/ Socket file descriptor/ Coun ter of received bytes/ Receive buffer/ Host address in formati on int sockfd;int num;char sdbufLENGTH;struct sockaddr_ in addr_remote;char sdstr= "MagicARM2410 UDP Experime nt."/* Check parameters nu mber */if (argc != 2)printf ("U
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《电动汽车再看回首》课件
- 小学六年级科学课件教科版第2课 怎样放得更大
- 《颈部包块临床诊断》课件
- 《法国教育中心》课件
- 《科学决策》课件
- 《法制知识竞赛》课件
- 2024年湖南省常德市公开招聘警务辅助人员(辅警)笔试高频必刷题试卷含答案
- 2021年陕西省安康市公开招聘警务辅助人员(辅警)笔试经典自测卷(2)含答案
- 2023年山东省莱芜市公开招聘警务辅助人员(辅警)笔试自考练习卷一含答案
- 2023年安徽省池州市公开招聘警务辅助人员(辅警)笔试必刷测试卷(2)含答案
- 2024年度工矿企业设备维修与保养合同3篇
- 食品生产小作坊培训
- 2024年度电商平台商家入驻协议
- 河北省石家庄市2023-2024学年六年级上学期期末科学试卷(含答案)
- 幕墙施工重点难点及解决方案
- 年度成本管控的实施方案
- 2024年中国板钉式空气预热器市场调查研究报告
- 人教版八年级上册数学期末考试试卷附答案
- DB1331T 041-2023 雄安新区绿色街区规划设计标准
- 北京市海淀区2022届高三上学期期末考试政治试题 含答案
- 初中七年级主题班会:如何正确对待自己的错误(课件)
评论
0/150
提交评论