C # code for downloading files using TCP/IP

Source: Internet
Author: User

Use TCP/IP to send files. The simple design idea is to start TcpListener on the server side to listen to the Socket connection of the client.
When the Client sends a connection request, TcpListener captures the Socket of the current request and obtains the received data (a string called a command ).
The command processing program then analyzes the string. If the string header contains GET_FILE, It is the file download request.

For example, the client sends a request to the server program: "GET_FILE | D: PUBLISHTEST. DLL ". First, TcpListener capture
The Socket of the current request. The string is received. If it is a file Download request, the request is sent through socket. SendFile (file = "D: PUBLISH
TEST. DLL ") to transfer the file to the current Socket. The NetworkStream. Read () method on the client receives files sent from the server.

Similarly, if you use Socket. Send (command)-> analysis-> execution-> mobile chess to play games, isn't it?

Socket applications often have more than this. Learning Socket components is the foundation of network programming.
 
The running diagram of the server interface.


Client Interface:

Enter the name of the file to be downloaded and the storage directory.
 

Thoughts:
1. if you provide a list of downloaded files, can you download files in batches?
2. If the files on the server are compared with the last modification date of the files on the local machine, can they be converted to version upgrade programs?
3. If the client uploads files to the server, can it be converted into a mini ftp program?
4. If the received byte [] is the serialized data of an object and then the byte [] is restored to an object, does it implement TCP/IP Transmission of the object?
5. If the object is a command sent from one computer to another, can it implement online Combat games?
6. How far do you think socket can go.
 

Some original code:
UpgraderClient. cs
 

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net. Sockets;
Using System. Net;
Using System. IO;

Namespace VJSDN. Tech. TcpIpDownloader
{
/// <Summary>
/// TCP/IP download server client
/// </Summary>
Public class UpgraderClient
{
Private OutputInvoke _ writeOutput = null;

Public UpgraderClient (OutputInvoke writeOutput)
{
_ WriteOutput = writeOutput;
}

/// <Summary>
/// Download an object
/// </Summary>
Public bool DownloadFile (string file, string savePath)
{
String hostIp = "8.8.8.2"; // host ip Address
Int hostPort = 12346; // host port

Try
{
TcpClient client = new TcpClient ();
Client. ReceiveTimeout = 1000*60;

If (client = null | file. Trim () = "") return false;

TryConnect (client, hostIp, hostPort); // connect to the server
If (! Client. Connected) return false; // connection line failure, exit

Byte [] bs = Encoding. Unicode. GetBytes ("GET_FILE |" + file );
Client. Client. Send (bs); // Send the request

// Start to accept data ....
NetworkStream ns = client. GetStream ();
MemoryStream MS = new System. IO. MemoryStream ();

Byte [] resBytes = new byte [1, 256];
Int resSize;
Do
{
ResSize = ns. Read (resBytes, 0, resBytes. Length );
String msg = Byte2Str (resBytes );
If (msg. Trim (). ToUpper () = "FILE_NOT_FOUND ")
{
If (_ writeOutput! = Null) _ writeOutput ("file not found:" + file );
& N

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.