锘??xml version="1.0" encoding="utf-8" standalone="yes"?>精品久久久久久无码专区,无码精品久久久天天影视,亚洲国产成人精品91久久久 http://www.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://www.shnenglu.com/cokecoffe/archive/2011/11/25/160987.htmlWangkekeWangkekeFri, 25 Nov 2011 14:38:00 GMThttp://www.shnenglu.com/cokecoffe/archive/2011/11/25/160987.htmlhttp://www.shnenglu.com/cokecoffe/comments/160987.htmlhttp://www.shnenglu.com/cokecoffe/archive/2011/11/25/160987.html#Feedback0http://www.shnenglu.com/cokecoffe/comments/commentRss/160987.htmlhttp://www.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 鍙戣〃璇勮
]]>
欧美日韩精品久久久久| 欧美与黑人午夜性猛交久久久| 久久久精品波多野结衣| 国产69精品久久久久99| 欧美国产成人久久精品| 午夜欧美精品久久久久久久| 无码AV波多野结衣久久| 91精品国产高清久久久久久io | 99久久人妻无码精品系列蜜桃| 色综合合久久天天综合绕视看| 日本五月天婷久久网站| 国产午夜精品理论片久久影视| 久久久久久曰本AV免费免费| 精品欧美一区二区三区久久久| 久久精品无码专区免费青青| 少妇被又大又粗又爽毛片久久黑人 | 久久久久久国产精品美女| 国产精品99久久久久久宅男小说| 久久99精品久久久久久hb无码| 亚洲国产视频久久| 国产亚洲精午夜久久久久久| 色婷婷综合久久久中文字幕| 一本一本久久A久久综合精品| 一本色道久久88综合日韩精品| 日韩美女18网站久久精品| 狠狠色丁香久久综合五月| 亚洲国产另类久久久精品小说| 日日狠狠久久偷偷色综合免费| 香港aa三级久久三级| 久久精品国产黑森林| 久久精品国产69国产精品亚洲| 亚洲国产精品无码久久98| 久久久久久久女国产乱让韩| 久久99热这里只频精品6| 伊人精品久久久久7777| 久久国产欧美日韩精品免费| 日韩久久无码免费毛片软件| 亚洲精品NV久久久久久久久久| 欧美久久综合九色综合| 欧美亚洲国产精品久久高清| 亚洲午夜久久久久久久久电影网 |