C # network programming (Protocol establishment and File Sending) 4

Source: Internet
Author: User
Tags ftp protocol

File Transfer

The examples used in the previous two articles are transmission strings. Sometimes we may want to transfer files between the server and the client. . How can we accomplish this? There may be two methods:

  • Similar to the FTP protocol, the server opens two ports and continuously listens to these two ports: one for receiving strings, similar to the FTP control port, which receives various commands (receiving or sending files ); one is used to transmit data, that is, to send and receive files.
  • The server opens up only one port for receiving strings, which is called the control port. After receiving the request, open a port on the client for file transmission based on the request content, and close the port after the transmission is complete.

Now we only focus on the above data port, and recall what we have summarized in the second article. We can conclude that when we use the above method, the data port of the server can be used for multiple requests from multiple clients. When method 2 is used, the server only requests the service once for one client, but the port is re-opened for each request, therefore, it is equivalent to multiple client requests. At the same time, because it is only one request service, we do not need to use asynchronous transmission when transferring files on the data port. However, we still need to use the Asynchronous Method to Control the port.

We can see from the above that the first method is much better, but we will adopt the second method. As for the reason, you can review the description of the chat program mode in C # network programming (basic concepts and operations), as we will create a chat program in the next article, this chat program adopts the third mode, so the exercises in this article are actually a foreshadowing for the next article.

1. conclude agreement 1.1 to send documents

First, let's look at the status of the sent file. If we want to send the client01.jpg file to the client from the client, what is the process:

  1. The client opens a data port for listening and obtains the port number, which is assumed to be 8005.
  2. If S1 is input on the client, the following control string is sent to the server: [file=Client01.jpg, mode = send, port = 8005].
  3. After receiving the message, the server establishes a connection with the client based on the Client ip address and port number.
  4. The client listens to the connection to the server and starts to send files.
  5. After the transfer, the client and server close the connection respectively.

In this case, the protocol for sending files is as follows: sending file1_client01.jpg, mode = send, port = 8005]. However, because it is a common string, we used a regular expression in the previous article to obtain the valid value, but this is obviously not a good method. Therefore, in this article and the next article, we adopt a new method to write the Protocol: XML. For the preceding statement, we can write the following XML:

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

In this way, we will be able to process much better on the server. Next let's take a look at the process of receiving files and their protocols.

NOTE:Here, sending and receiving files are based on the client. When the client sends a file, it receives the file from the server.

1.2 receive files

The difference between receiving and sending files is that the client writes data to the network stream or the server writes data to the network stream.

  1. The data port opened on the client is used for listening, which is assumed to be 8006.
  2. If the client inputs R1, the sending control string is <protocol> <file name = "Server01.jpg" mode = "receive" port = "8006"/> </protocol>.
  3. After receiving the message, the server establishes a connection with the client based on the Client ip address and port number.
  4. The client establishes a connection with the server, and the server starts to write data in the network flow.
  5. After the transfer, the server and client close the connection respectively.
2. Implementation of protocol processing class

As in the previous chapter, before writing the actual Server client code, we must first write the class for processing the Protocol. It must provide the following two functions: 1. It is convenient for us to obtain complete protocol information, because as we have said earlier, the server may split or merge multiple independent requests from the client. For example, if the client sends two control messages consecutively to the server, and the server merges them, you need to split them and process them separately. 2. You can easily obtain the desired attribute information. Because the Protocol is in XML format, you also need a class to process XML to obtain the string attribute value.

2.1 ProtocalHandler helper class

Let's take a look at ProtocalHandler. It works the same as RequestHandler in the previous article. Note that it must be declared as an instance rather than static, because each TcpClient must correspond to a ProtocalHandler, because the patialProtocal maintained internally cannot be shared, when the Protocol sending is incomplete, this variable is used to temporarily Save the truncated string.

Public class ProtocolHandler {

Private string partialProtocal; // Save the incomplete Protocol

Public ProtocolHandler (){
PartialProtocal = "";
}

Public string [] GetProtocol (string input ){
Return GetProtocol (input, null );
}

// Obtain the Protocol
Private string [] GetProtocol (string input, List <string> outputList ){
If (outputList = null)
OutputList = new List <string> ();

If (String. IsNullOrEmpty (input ))
Return outputList. ToArray ();

If (! String. IsNullOrEmpty (partialProtocal ))
Input = partialProtocal + input;

String pattern = "(^ <protocol> .*? </Protocol> )";

// If a match exists, it indicates that the complete protocol has been found.
If (Regex. IsMatch (input, pattern )){

// Obtain the matched Value
String match = Regex. Match (input, pattern). Groups [0]. Value;
OutputList. Add (match );
PartialProtocal = "";

// Shorten the input length
Input = input. Substring (match. Length );

// Recursive call
GetProtocol (input, outputList );

&

Related Article

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.