青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

厚積薄發,滴水穿石

搬家到主站了:http://www.cnblogs.com/cokecoffe/
隨筆 - 45, 文章 - 8, 評論 - 12, 引用 - 0
數據加載中……

Simple UDP code with C

The server

1 #include <arpa/inet.h>
2 #include <netinet/in.h>
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <unistd.h>
7
8 #define BUFLEN 512
9 #define NPACK 10
10 #define PORT 9930
11
12 void diep(char *s)
13 {
14 perror(s);
15 exit(1);
16 }
17
18 int main(void)
19 {
20    struct sockaddr_in si_me, si_other;
21     int s, i, slen=sizeof(si_other);
22     char buf[BUFLEN];
23
24     if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
25        diep("socket");
26
27    memset((char *) &si_me, 0, sizeof(si_me));
28     si_me.sin_family = AF_INET;
29    si_me.sin_port = htons(PORT);
30     si_me.sin_addr.s_addr = htonl(INADDR_ANY);
31   
        if (bind(s, &si_me, sizeof(si_me))==-1)
32         diep("bind");
33
34     for (i=0; i<NPACK; i++) {
35        if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
36              diep("recvfrom()");
37        printf("Received packet from %s:%d\nData: %s\n\n",
38        inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
39     }
40
41     close(s);
42     return 0;
43 }
Comments

Lines 8-10 define the buffer size (quite arbitrary), the number of packets to receive and the UDP port number to listen at. You could use any port number above 1023, although bind() will fail if someone else is using the same port simultaneously.
The function diep() is used for error handling.
21: Declare receive buffer.
22: sockaddr_in is a structure containing an Internet socket address. Basically, it contains:
an address family (always AF_INET for our purposes)
a port number
an IP address
si_me defines the socket where the server will listen. si_other defines the socket at the other end of the link (that is, the client).
24: Create a socket. AF_INET says that it will be an Internet socket. SOCK_DGRAM says that it will use datagram delivery instead of virtual circuits. IPPROTO_UDP says that it will use the UDP protocol (the standard transport layer protocol for datagrams in IP networks). Generally you can use zero for the last parameter; the kernel will figure out what protocol to use (in this case, it would choose IPPROTO_UDP anyway).
27: We need to initialize the si_me structure. The first step is to fill it with binary zeroes, which is done on this line. (I doubt this step is actually necessary in modern Unix implementations, but better safe than sorry.)
28: We will use Internet addresses.
29: Here, the port number is defined. htons() ensures that the byte order is correct (Host TO Network order/Short integer).
30: This line is used to tell what IP address we want to bind to. Most machines have more than one network interface (for example, 127.0.0.1 for the loopback interface and some other address for the network card; there may be more than one network card). In the general case, you want to accept packets from any interface, so you use INADDR_ANY instead of a specific address.
31: Now we are ready to bind the socket to the address we created above. This line tells the system that the socket s should be bound to the address in si_me.
35: This call says that we want to receive a packet from s, that the data should be put info buf, and that buf can store at most BUFLEN characters. The zero parameter says that no special flags should be used. Data about the sender should be stored in si_other, which has room for slen byte. Note that recvfrom() will set slen to the number of bytes actually stored. If you want to play safe, set slen to sizeof(si_other) after each call to recvfrom().
37: The information about the sender we got from recvfrom() is displayed (IP:port), along with the data in the packet. inet_ntoa() takes a struct in_addr and converts it to a string in dot notation, which is rather useful if you want to display the address in a legible form.

The client

1 #define SRV_IP "999.999.999.999"
2 /* diep(), #includes and #defines like in the server */
3
4 int main(void)
5 {
6     struct sockaddr_in si_other;
7     int s, i, slen=sizeof(si_other);
8     char buf[BUFLEN];
9
10     if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
11        diep("socket");
12
13     memset((char *) &si_other, 0, sizeof(si_other));
14     si_other.sin_family = AF_INET;
15     si_other.sin_port = htons(PORT);

16     if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
17           fprintf(stderr, "inet_aton() failed\n");
18          exit(1);
19    }
20
21     for (i=0; i<NPACK; i++) {
22        printf("Sending packet %d\n", i);
23        sprintf(buf, "This is packet %d\n", i);
24        if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
25               diep("sendto()");
26 }
27
28       close(s);
29       return 0;
30 }
Note: The client is quite similar to the server. Only the differences will be discussed.

1: You need to know the IP address of the machine where the server runs. If you run the client and the server on the same machine, try 127.0.0.1. "999.999.999.999" is not a legal IP address; you need to substitute your own server's address.
12: You may call bind() after the call to socket(), if you wish to specify which port and interface that should be used for the client socket. However, this is almost never necessary. The system will decide what port and interface to use.
13-19: Here, we set up a sockaddr_in corresponding to the socket address where the server is listening. inet_aton() is used to convert a string in dotted-decimal ASCII notation to a binary address.
24: Send BUFLEN bytes from buf to s, with no flags (0). The receiver is specified in si_other, which contains slen byte.
General tips

Remember to always check return values from system calls! By doing so, you will save time in the long run, I promise. Many people do not test return values in small quick-and-dirty test programs. However, in such cases it is especially important to check return values, because if you don't really know what you are doing you are much more likely to make a mistake. The checks help you understand what went wrong and why.
There is a tool called netcat (the actual command is nc) which is very useful for testing and debugging socket code. Check the man page if you are curious (of course, it might not be installed on your system).
If you want to cut and paste the code above, use cut -c9- to get rid of the line numbers. (The exact syntax of cut may be different on your system, and you may have to remove more or less than 9 characters.)
The command netstat can be useful to check which sockets are active. Try netstat -a.
For an overview over some of the structures used in socket programming, check out the code examples from lecture 13 on my course in Unix system programming. You will also find some material on TCP programming there. Disregard the initial material on select() and friends. There are some comments in Swedish, but most of the page is written in C.

posted on 2011-11-25 22:38 Wangkeke 閱讀(600) 評論(0)  編輯 收藏 引用 所屬分類: Net


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品不卡在线| 国产精品乱人伦中文| 韩日午夜在线资源一区二区| 亚洲欧美中文另类| 一区二区日韩伦理片| 国产精品久久看| 性久久久久久久久| 久久精品一区二区三区不卡牛牛 | 久久亚洲精品网站| 亚洲国产精品日韩| 亚洲精品日韩久久| 国产伦精品一区| 久久久欧美一区二区| 久久综合色婷婷| 在线视频日本亚洲性| 亚洲一区二区成人| 在线观看成人小视频| 亚洲三级免费| 国产精品女主播在线观看 | 亚洲免费av观看| 亚洲专区一区| 亚洲国内在线| 亚洲性人人天天夜夜摸| 一区二区三区我不卡| 日韩午夜在线| 亚洲欧美精品中文字幕在线| 激情成人在线视频| 亚洲视频在线观看三级| 欧美伊人久久久久久午夜久久久久| 精品成人在线观看| 亚洲视频一区二区免费在线观看| 国产在线不卡精品| 亚洲麻豆视频| 亚洲电影第三页| 亚洲一级片在线看| 亚洲精品免费网站| 欧美一级久久| 一区二区高清| 久久亚洲视频| 久久精品一区二区三区不卡牛牛| 欧美韩日亚洲| 久久中文在线| 国产精品黄视频| 亚洲人成在线观看| 在线免费观看视频一区| 亚洲一区二区视频在线| 99亚洲精品| 男人插女人欧美| 玖玖精品视频| 狠狠爱综合网| 欧美一级在线亚洲天堂| 香蕉成人久久| 国产精品久久久91| 一本久久a久久免费精品不卡| 亚洲精品男同| 欧美成人精品影院| 免费国产一区二区| 国产一区亚洲| 久久精品国产视频| 久久精品国产亚洲一区二区三区| 国产精品久久久久久久久借妻| 亚洲全部视频| 亚洲精品欧美激情| 欧美不卡福利| 亚洲国产精品久久久久秋霞影院 | 国产日韩精品在线播放| 亚洲视频免费看| 午夜精品久久久久久久99水蜜桃| 欧美日韩一区在线播放| av成人免费观看| 亚洲一区成人| 国产精品一区二区三区久久久| 一区二区三区 在线观看视频| 亚洲视频在线一区| 国产精品都在这里| 亚洲欧美一区二区激情| 欧美在线不卡视频| 国产一区二区三区黄视频| 欧美资源在线观看| 欧美成人免费在线观看| 亚洲欧洲精品一区二区三区不卡| 欧美大片在线观看| 99re热精品| 久久精品国产亚洲a| 一区视频在线| 欧美国产日韩一区二区在线观看| 日韩午夜激情| 久久精品日产第一区二区三区| 狠狠色综合日日| 欧美福利电影网| 亚洲一区二区三区涩| 久久久精品国产99久久精品芒果| 在线日韩欧美| 亚洲激情精品| 性欧美暴力猛交另类hd| 影音先锋日韩精品| 欧美日韩精品不卡| 久久国产精品99国产精| 激情欧美日韩| 欧美视频中文在线看 | 免费观看在线综合色| 日韩天堂在线观看| 国产日韩精品电影| 欧美96在线丨欧| 欧美一区二区福利在线| 亚洲国产精品va在线观看黑人| 亚洲永久精品国产| 亚洲电影免费观看高清完整版在线 | 欧美一级日韩一级| 久久不射2019中文字幕| 亚洲人成毛片在线播放女女| 欧美一区二区三区视频在线| 91久久黄色| 国内精品嫩模av私拍在线观看| 欧美精品国产一区二区| 欧美在线在线| 亚洲欧美国产制服动漫| 亚洲片国产一区一级在线观看| 久久成人久久爱| 亚洲永久免费| 欧美暴力喷水在线| 久久久精品性| 欧美亚洲日本国产| 一本色道久久综合亚洲精品高清| 男人插女人欧美| 久久婷婷久久一区二区三区| 亚洲欧美成人一区二区在线电影| 91久久精品国产91久久性色| 国产揄拍国内精品对白| 国产日韩精品入口| 亚洲久久成人| 亚洲二区在线视频| 欧美成人免费视频| 蜜桃av综合| 免费成人你懂的| 模特精品裸拍一区| 欧美成人精品一区二区| 久久男人资源视频| 久久久久久欧美| 久久国产精品久久国产精品| 亚洲欧美日韩一区二区在线| 欧美a一区二区| 美乳少妇欧美精品| 你懂的网址国产 欧美| 久久精品中文字幕一区二区三区 | 亚洲福利av| 欧美顶级大胆免费视频| 男女精品视频| 亚洲电影免费| 亚洲高清电影| 亚洲精品永久免费精品| 亚洲国产日日夜夜| 亚洲精品五月天| 夜夜爽www精品| 一区二区三区久久久| 亚洲一区在线直播| 欧美伊人影院| 久久综合网hezyo| 亚洲福利专区| 一区二区精品| 久久aⅴ国产紧身牛仔裤| 久久精品夜色噜噜亚洲aⅴ| 麻豆成人综合网| 亚洲国产91精品在线观看| 亚洲片区在线| 午夜精品久久久久久99热| 久久久久久久尹人综合网亚洲| 久久午夜电影| 欧美日韩亚洲综合一区| 国产伦精品一区二区三| 曰本成人黄色| 亚洲天堂偷拍| 精品动漫3d一区二区三区| 亚洲激情婷婷| 午夜精品国产更新| 免费亚洲电影| 夜夜嗨av色一区二区不卡| 欧美亚洲一区在线| 牛牛影视久久网| 国产精品青草综合久久久久99| 黄网站免费久久| 亚洲一区二区三区在线播放| 久久先锋影音| 日韩午夜在线播放| 久久久久久久综合| 国产精品高清免费在线观看| 好看不卡的中文字幕| 在线综合亚洲| 欧美大片国产精品| 亚洲欧美日韩视频一区| 欧美激情第二页| 激情欧美日韩一区| 午夜精品在线| 亚洲三级免费观看| 久久精品国产2020观看福利| 国产精品成人免费精品自在线观看 | 一区二区动漫| 美国十次成人| 激情五月***国产精品| 性色一区二区|