嵌入式程序解析_第1頁
嵌入式程序解析_第2頁
嵌入式程序解析_第3頁
嵌入式程序解析_第4頁
嵌入式程序解析_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論