C # Network programming (concluding protocols and sending files)

Source: Internet
Author: User
Tags ftp regular expression ftp protocol port number

File transfer

The examples used in the previous two articles are transmission strings, and sometimes we might want to pass files between the server and the client. For example, consider a situation where the client displays a menu that sends files client01.jpg, client02.jpg, client03.jpg to the server when we enter S1, S2, or S3 (s for send); When we enter R1, When R2 or R3 (R is the receive abbreviation), the file server01.jpg, Server02.jpg, Server03.jpg are received from the server side, respectively. So how do we do it? There are two possible ways to do this:

Similar to the FTP protocol, the server opens two ports and continues to listen to the two ports: a control port for receiving strings, similar to FTP, which receives various commands (receiving or sending files), and one for transmitting data, that is, sending and receiving files.

The service side only opens a port for receiving strings, which we call the control port. When a request is received, a port is dedicated to the file transfer based on the requested content and the port is closed after the transfer is completed.

Now we're only focusing on the data ports above, recall that in the second article we concluded that, when we use the method above, the server's data port can be used for multiple clients to request the service; When we use Method two o'clock, the service end is only one request service for a client, But because each request will reopen the port, it is actually equivalent to being able to service multiple requests for more than one client. Also, because it serves only once, we do not need to use asynchronous transmission when transferring files on the data port. But in the control port we still need to use asynchronous mode.

As you can see from the above, the first approach is much better, but we'll take the second approach. As for the reason, you can review the Part.1 (Basic concepts and operations) about the Chat program mode, because the next article we will create a chat program, and this chat program uses a third mode, so this article practice is a foreshadowing of the next article.

1. Conclusion of agreements

1.1 Sending files

Let's take a look at the sending file, and if we want to send the file client01.jpg to the client by the client, what is the process:

The client opens the data port for listening, and gets the port number, assuming 8005.

Assuming that the client has entered a S1, the following control string is sent to the server: [File=client01.jpg, Mode=send, port=8005].

When the server receives it, it establishes a connection to the client based on the client IP and port number.

The client detects the connection to the server and begins sending the file.

The client and the server end the connection separately after the transfer is completed.

At this time, we concluded the sending file agreement as follows: [File=client01.jpg, Mode=send, port=8005]. However, since it is an ordinary string, in the previous article we used a regular expression to get the valid values, but this is obviously not a good idea. So in this article and in the next article, we use a new way to write the protocol: XML. For the above statement, we can write this XML:

<protocol><file name="client01.jpg" mode="send" port="8005" /></protocol>

So we can do a lot better on the service side, and then we'll look at the process of receiving files and their protocols.

Note: Here said that send, receive file is standing in the position of the client said, when the client sent the file, for the server to collect, is to receive files.

1.2 Receiving files

The receiving file is actually exactly the same as sending the file, except that the client writes data to the network stream or the server writes the data to the network stream.

The client opens the data port for listening, assuming 8006.

If the client enters R1, the control string is sent: <protocol><file name= "server01.jpg" mode= "Receive" port= "8006"/></protocol> To the service side.

When the server receives it, it establishes a connection to the client based on the client IP and port number.

The client establishes a connection to the server and the server begins to write data in the network stream.

After delivery, the server and client shut down the connection respectively.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.