UDP program

A, transmitting data to UdpClient UDPServer
(1), the program UDPServer establishment procedure
1, Socket () function creates a communication endpoint ----
parameters: 1, a communication domain, to establish a communication protocol Group 2, 3 datagram socket type, particularly protocol type, typically the first two parameters of the agreement has been finalized, like 0;
2, the bind () ---- service port binding
parameters: 1, the socket descriptor 2, 3 server address, server structure function byte size
3, recvfrom () ---- receiving client information
parameters: 1, the socket descriptor 2, 3 array, the array size 4, the flag is 05, 6 client address, the client structure body size in bytes

Two, UDPclient to UDPserver transfer files
Note: UDPclient and UDPserver program must not be placed in the same directory as an error occurs during transmission; think about the windows directory under the same file again transferred from the catalog to the Catalog under it? ? The answer is no! ! ! There are going to be transferred must file must exist in the catalog.

The basic process:
client server ---------------
1, send the file name ---------- 1, accept the file name, and create file
2, open the file ----------- 2, open file
3. Read data, transmission data ------ 3 accept data, write data
4, read is completed, transmits "bye" ----- 4 , received a "bye", close the file

(1) The client Client
1, the sendto () to send the file ----
parameters: 1, the socket descriptor 2, 3 array, the array size 4, the flag is 05, the address server 6, the server word structure section size
2, open () ------ open the file
parameters: 1, the filename 2, a read-only manner open
3, read () ---- read the file
parameters: 1, the file descriptor fd 2, array buf 3, the array size
to fd read the sizeof (buf) from the bytes in buf

(2) Server Server
1, Socket () function creates a communication endpoint ----
parameters: 1, a communication domain, to establish a communication protocol Group 2, 3 datagram socket type, protocol type particular, typically the first two parameters the protocol has been finalized, like 0;
2, recvfrom () ---- accept client file name
parameters: 1, the socket descriptor 2, 3 array, the array size 4, the flag is 05, the client address 6, the client structure byte size
3, open () ------ open the file (there is open, no create)
parameters: 1, 2 file name, just write the way open, 3, authority is 00700,
. 4, write () ---- write data
parameters: 1, 2 file descriptor fd, buf array. 3, the size of the array
to write buf the sizeof (buf) bytes from the fd

Guess you like

Origin blog.csdn.net/qq_41915323/article/details/86555586