下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 浙江2025年民生银行温州分行社会招聘备考题库及答案详解1套
- 2025年杭州市公安局上城区分局警务辅助人员招聘60人备考题库及答案详解1套
- 2025年国家矿山安全监察局安徽局安全技术中心招聘劳务派遣财务人员备考题库完整参考答案详解
- 2025年根河市人民政府面向社会公开招聘(补招)乡镇及政府专职消防队员26人备考题库附答案详解
- 2025年资阳市公安局公开招聘警务辅助人员的备考题库及答案详解1套
- 航空工业成飞2026届校园招聘备考题库及完整答案详解一套
- 申论考试科目及答案
- 2025年兰溪市卫健系统第一批面向高校公开招聘医学类应届毕业生17人的备考题库及答案详解一套
- 2025四川德阳市住房公积金管理中心罗江管理部编外聘用人员招聘1人备考考试题库及答案解析
- 2025重庆高新技术产业开发区招聘专职消防队员114人备考考试试题及答案解析
- 2025黑龙江大兴安岭地区韩家园林业局工勤岗位人员招聘40人备考考点试题及答案解析
- 2025年陕煤澄合矿业有限公司招聘(570人)笔试备考题库附答案解析
- 2025年保密观知识竞赛题库(含参考答案)
- 2025山西朔州市两级法院司法辅助人员招聘16人笔试考试备考试题及答案解析
- 危险化学品应急救援员岗位招聘考试试卷及答案
- 物业餐饮安全协议书
- 梁截面加高加固施工方案
- 骨干教师绩效考核制度实施细则
- 2025年低空经济「无人机农业」应用场景与解决方案报告
- 施工项目质量管理提升方案
- 养殖蛋鸡的技术知识培训课件
评论
0/150
提交评论