锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲七七久久综合桃花剧情介绍,蜜臀久久99精品久久久久久9,亚洲在线成人精品http://m.shnenglu.com/cokecoffe/category/17380.htmlzh-cnSun, 27 Nov 2011 08:42:59 GMTSun, 27 Nov 2011 08:42:59 GMT60Simple UDP code with Chttp://m.shnenglu.com/cokecoffe/archive/2011/11/25/160987.htmlWangkekeWangkekeFri, 25 Nov 2011 14:38:00 GMThttp://m.shnenglu.com/cokecoffe/archive/2011/11/25/160987.htmlhttp://m.shnenglu.com/cokecoffe/comments/160987.htmlhttp://m.shnenglu.com/cokecoffe/archive/2011/11/25/160987.html#Feedback0http://m.shnenglu.com/cokecoffe/comments/commentRss/160987.htmlhttp://m.shnenglu.com/cokecoffe/services/trackbacks/160987.htmlhttp://www.abc.se/~m6695/udp.htmlThe 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.


Wangkeke 2011-11-25 22:38 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲精品一区在线| 欧美一区视频在线| 亚洲电影有码| 国产揄拍国内精品对白| 国产伦理一区| 国产情人综合久久777777| 国产情人节一区| 韩日精品视频一区| 亚洲第一精品影视| 亚洲国产婷婷香蕉久久久久久99| 伊伊综合在线| 亚洲裸体视频| 亚洲欧美日韩综合一区| 久久精品国产96久久久香蕉| 六十路精品视频| 亚洲区第一页| 一本色道久久综合亚洲精品高清 | 亚洲国产精品福利| 亚洲精选在线| 亚洲欧美在线播放| 久久在线91| 亚洲美女在线看| 欧美一区二区三区在线免费观看| 噜噜噜在线观看免费视频日韩| 欧美日韩三级一区二区| 国产亚洲综合性久久久影院| 亚洲国产精品高清久久久| 亚洲欧美综合一区| 欧美福利在线| 午夜精品久久久久| 欧美剧在线免费观看网站| 国产精品亚洲а∨天堂免在线| 亚洲福利视频在线| 午夜精品久久久久久久久久久| 老色批av在线精品| 亚洲一区三区视频在线观看 | 欧美成人午夜| 日韩视频一区二区| 久久精品人人做人人爽| 亚洲欧洲一区二区三区在线观看 | 欧美激情国产高清| 国产一区二区三区的电影 | 欧美不卡高清| 午夜一区不卡| 欧美三级网址| 亚洲精品久久久蜜桃| 欧美一区二区在线| 亚洲人成网站色ww在线| 久久不射2019中文字幕| 国产精品美女www爽爽爽视频| 在线日本欧美| 久久久爽爽爽美女图片| 亚洲尤物精选| 国产精品夫妻自拍| 一区二区免费在线播放| 欧美成人在线免费视频| 欧美伊人影院| 国产偷自视频区视频一区二区| 亚洲天堂av电影| 亚洲人体大胆视频| 免费观看亚洲视频大全| 黄色一区二区三区| 久久久久久亚洲精品中文字幕| 一区二区日本视频| 欧美性大战久久久久久久蜜臀| 亚洲欧洲午夜| 欧美激情亚洲国产| 欧美成人高清| 日韩视频免费看| 夜夜嗨av一区二区三区四季av| 欧美日本亚洲| 亚洲一级在线观看| 一区二区不卡在线视频 午夜欧美不卡'| 免费高清在线一区| 亚洲人成网站色ww在线| 亚洲电影有码| 欧美日韩国产123| 亚洲自拍偷拍一区| 香蕉久久夜色精品国产使用方法| 国产区亚洲区欧美区| 久久久久一区| 免费在线观看日韩欧美| 亚洲伦理在线免费看| 这里是久久伊人| 狠狠色丁香婷婷综合久久片| 欧美国产日韩a欧美在线观看| 欧美激情亚洲精品| 亚洲观看高清完整版在线观看| 国产欧美日韩在线观看| 久久国产免费| 美日韩精品免费| 一区二区欧美日韩视频| 欧美一级精品大片| 91久久中文| 亚洲影院色无极综合| 在线精品一区二区| 日韩视频一区二区三区在线播放免费观看 | 欧美三区在线| 久久免费观看视频| 欧美激情一区二区三区四区| 欧美一区二粉嫩精品国产一线天| 久久精品国产一区二区三| 亚洲免费av电影| 亚洲欧洲av一区二区三区久久| 尤物九九久久国产精品的特点| 亚洲黄色一区| 国产在线不卡精品| av成人免费在线| 1024欧美极品| 午夜精品福利电影| 亚洲美女视频| 裸体歌舞表演一区二区| 先锋影音久久久| 欧美精品日韩综合在线| 久久久国产91| 国产精品日韩一区| 亚洲免费大片| 亚洲黄色成人久久久| 小嫩嫩精品导航| 亚洲免费视频成人| 免费成人黄色av| 久久人人精品| 国产精品免费看| 日韩特黄影片| 亚洲激情在线播放| 久久成人免费网| 午夜激情久久久| 欧美日韩另类字幕中文| 亚洲黄色影院| 日韩视频一区二区三区| 久久视频在线看| 久热这里只精品99re8久| 国产精品一区一区| 一区二区三区三区在线| 在线一区二区三区四区| 欧美韩国日本一区| 亚洲高清一二三区| 亚洲第一精品影视| 老司机一区二区三区| 美日韩丰满少妇在线观看| 国产一区二区三区视频在线观看 | 国产精品久久久久久久电影| 日韩西西人体444www| 亚洲毛片在线观看.| 看欧美日韩国产| 欧美高清在线一区二区| 亚洲人成亚洲人成在线观看| 国产精品三级久久久久久电影| 日韩视频中午一区| 一区二区三区高清在线观看| 欧美国产精品v| 亚洲黄色性网站| 亚洲视频导航| 国产精品女同互慰在线看| 亚洲欧洲av一区二区| 久久亚洲综合网| 亚洲国产精品久久久久久女王| 麻豆乱码国产一区二区三区| 亚洲国产精品一区二区三区| 日韩午夜在线| 国产精品白丝黑袜喷水久久久| 亚洲一区视频| 美女精品自拍一二三四| 99国产精品久久久久久久久久| 欧美日韩午夜| 午夜精品99久久免费| 欧美韩国在线| 亚洲一区二区在线看| 国产视频亚洲精品| 欧美成人有码| 亚洲尤物在线| 亚洲成人中文| 亚洲男同1069视频| 一区二区三区中文在线观看 | 黄网动漫久久久| 欧美激情1区2区3区| 在线视频免费在线观看一区二区| 欧美怡红院视频一区二区三区| 一区二区三区无毛| 欧美性猛交一区二区三区精品| 欧美在线精品免播放器视频| 亚洲激情在线激情| 久久国产一区| 日韩一二三在线视频播| 国产日韩欧美麻豆| 欧美国产日韩亚洲一区| 香蕉成人久久| 亚洲精品中文字| 老司机67194精品线观看| 亚洲一区视频在线| 亚洲黄网站在线观看| 国产一区二区欧美| 国产精品久久一级| 欧美激情中文不卡| 久久久无码精品亚洲日韩按摩| 亚洲丝袜av一区| 亚洲激情电影中文字幕| 理论片一区二区在线| 久久精品国产清自在天天线| 在线一区观看|