自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

UDPClient代碼分享

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理
文章中,我們來(lái)對(duì)UDPClient的相關(guān)源碼編寫(xiě)進(jìn)行了分析,那么具體的內(nèi)容我們也做了解釋。希望對(duì)大家有用。

前文,我們對(duì)UDP Server的編寫(xiě)過(guò)程進(jìn)行了分析和介紹,并且也分享了源碼,不知道大家掌握的如何。這里我們繼續(xù)來(lái)對(duì)UDPClient程序進(jìn)行一下分析,希望對(duì)大家有所幫助。

UDPClient程序

編寫(xiě)UDPClient程序的步驟

(1)初始化sockaddr_in結(jié)構(gòu)的變量,并賦值。這里使用“8888”作為連接的服務(wù)程序的端口,從命令行參數(shù)讀取IP地址,并且判斷IP地址是否符合要求。

(2)使用socket()來(lái)建立一個(gè)UDP socket,第二個(gè)參數(shù)為SOCK_DGRAM。

(3)使用connect()來(lái)建立與服務(wù)程序的連接。與TCP協(xié)議不同,UDP的connect()并沒(méi)有與服務(wù)程序三次握手。上面說(shuō)了UDP是非連接的,實(shí)際上也可以是連接的。使用連接的UDP,kernel可以直接返回錯(cuò)誤信息給用戶程序,從而避免由于沒(méi)有接收到數(shù)據(jù)而導(dǎo)致調(diào)用recvfrom()一直等待下去,看上去好像客戶程序沒(méi)有反應(yīng)一樣。

(4)向服務(wù)程序發(fā)送數(shù)據(jù),因?yàn)槭褂眠B接的UDP,所以使用write()來(lái)替代sendto()。這里的數(shù)據(jù)直接從標(biāo)準(zhǔn)輸入讀取用戶輸入。

(5)接收服務(wù)程序發(fā)回的數(shù)據(jù),同樣使用read()來(lái)替代recvfrom()。

(6)處理接收到的數(shù)據(jù),這里是直接輸出到標(biāo)準(zhǔn)輸出上。

udpclient.c程序內(nèi)容:

  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. #define MAXLINE 80  
  10. #define SERV_PORT 8888  
  11.  
  12. void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)  
  13. {  
  14. int n;  
  15. char sendline[MAXLINE], recvline[MAXLINE + 1];  
  16. /* connect to server */ 
  17. if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)  
  18. {  
  19. perror("connect error");  
  20. exit(1);  
  21. }  
  22. while(fgets(sendline, MAXLINE, fp) != NULL)  
  23. {  
  24. /* read a line and send to server */ 
  25. write(sockfd, sendline, strlen(sendline));  
  26. /* receive data from server */ 
  27. n = read(sockfd, recvline, MAXLINE);  
  28. if(n == -1)  
  29. {  
  30. perror("read error");  
  31. exit(1);  
  32. }  
  33. recvline[n] = 0; /* terminate string */ 
  34. fputs(recvline, stdout);  
  35. }  
  36. }  
  37. int main(int argc, char **argv)  
  38. {  
  39. int sockfd;  
  40. struct sockaddr_in srvaddr;  
  41. /* check args */ 
  42. if(argc != 2)  
  43. {  
  44. printf("usage: udpclient \n");  
  45. exit(1);  
  46. }  
  47. /* init servaddr */ 
  48. bzero(&servaddr, sizeof(servaddr));  
  49. servaddr.sin_family = AF_INET;  
  50. servaddr.sin_port = htons(SERV_PORT);  
  51. if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)  
  52. {  
  53. printf("[%s] is not a valid IPaddress\n", argv[1]);  
  54. exit(1);  
  55. }  
  56. sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  57. do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));  
  58. return 0;  

 

責(zé)任編輯:佟健 來(lái)源: uml.org.cn
相關(guān)推薦

2010-07-07 11:09:36

UDPClient

2012-06-27 10:03:39

PHP

2009-12-18 17:01:37

Ruby基礎(chǔ)代碼

2009-12-17 09:49:18

Ruby代碼管理

2012-01-10 12:48:52

Java

2010-04-22 17:53:36

Apache負(fù)載均衡

2010-04-26 16:36:31

DNS負(fù)載均衡設(shè)置

2018-05-10 15:35:03

前端代碼圖像

2012-01-12 10:09:30

Java

2012-01-17 10:43:26

Java

2012-01-11 09:39:50

Java

2015-07-21 15:35:47

代碼總結(jié)源碼

2011-07-11 10:16:07

JavaScript

2011-07-18 16:48:02

Cocoa Objective-

2014-04-04 09:53:18

2048C++

2011-07-07 10:35:53

htaccess

2010-05-28 12:55:23

2015-08-25 08:55:14

優(yōu)秀代碼基因

2020-08-04 07:47:59

代碼模板模式

2009-11-24 13:26:17

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)