C # implement LAN file transmission

Source: Internet
Author: User

Network Communication is generally conducted through Socket, known as the process communication mechanism, also known as "Socket", used to describe the IP address and port, is a communication chain handle.

First, let's take a look at the basic principle of socket:

Socket principle:
There are many such hosts on the Internet. These Hosts generally run multiple service software and provide several services at the same time. Each service opens a Socket and binds it to a port. Different ports correspond to different services. Socket is like a porous Socket, just as it was originally intended. A host is like a room with various sockets. Each socket has a serial number. Some sockets provide 220 v ac, some provide 110 v ac, and some provide cable TV programs. The customer software inserts the plug into a socket with different numbers to obtain different services.

Socket image comprehension:

Socket is very similar to a telephone outlet. Take a national-level telephone network as an example.

The two sides of a telephone call are equivalent to two processes that communicate with each other. The area code is the network address of the phone call. A vswitch in the area is equivalent to a host, the local number assigned to each user by the host is equivalent to the socket number.

Before a call, a user must possess a telephone, which is equivalent to applying for a socket. At the same time, the user must know the number of the other party, which is equivalent to having a fixed socket.

Then a dial-up call to the other Party is equivalent to a connection request (if the other party is not in the same region, you need to call the other party's zone number, which is equivalent to a network address ).

If the other party is present and idle (equivalent to the communication of another host starting and accepting connection requests), pick up the phone microphone and both parties can formally call, equivalent to a successful connection.

The communication process between the two parties is the process of sending a signal to the phone and receiving a signal from the phone. It is equivalent to sending data to and receiving data from the socket.

After the call is over, one party suspends the phone to close the socket and cancel the connection.

 

Socket Communication Application diagram

 

 

 

TCP/IP Client/Server Communication Flowchart

 

 

Through understanding the Socket communication principle, we can start to write a simple communication program for verification.

Here, we developed a file transfer program in the LAN to simulate the feature of Flying Pigeon transmission.

QQ is disabled in the company, and QQ cannot be used. Sometimes it is difficult to transmit files, and it is often necessary to use a USB flash disk to copy the files.

To this end, I want to write a simple LAN file transfer tool to temporarily solve the problem of not being able to access QQ.

Code snippet of the sent file:


/// <Summary>
/// Send a file
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
PRivate void btnSentFile_Click (object sender, EventArgs e)
{
// Open the file
OpenFileDialog dlg = new OpenFileDialog ();
If (dlg. ShowDialog () = DialogResult. OK)
{
For (int I = 0; I <lvwDisplayUser. SelectedItems. Count; I ++)
{
String ip = this. lvwDisplayUser. SelectedItems [I]. SubItems [2]. Text;

// Initialize accept socket: addressing scheme, which communicates with Tcp in the form of accept stream
SocketSent = new Socket (AddressFamily. InterNetwork,
SocketType. Stream,
ProtocolType. Tcp );

// Set the Server IP address and port
IpSent = new IPEndPoint (IPAddress. Parse (ip), 8001 );

// Connect to the server
LanSocket socketConnet = new LanSocket (socketSent, ipSent );
Thread tConnection = new Thread (new ThreadStart (socketConnet. SocketConnect ));
TConnection. Start ();

Thread. Sleep (100 );
// Add the "DAT" identifier to the file to be sent

SentLanFile sentFile = new SentLanFile (dlg, socketSent );
Thread tSentFile = new Thread (new ThreadStart (sentFile. SentFile ));
TSentFile. Start ();
}
}
}

/// <Summary>
/// Send a file
/// </Summary>
Public void SentFile ()
{
String msg = "0DAT" + dlg. FileName;

// Convert "msg" into a byte stream for Transmission
SocketSent. Send (Encoding. Default. GetBytes (msg ));

// Define a read file stream
FileStream read = new FileStream (dlg. FileName, FileMode. Open, Fileaccess. Read );

// Set the buffer to 1024 bytes.
Byte [] buff = new byte [1024];
Int len = 0;
While (len = read. Read (buff, 0, 1024 ))! = 0)
{
// Send information based on the actual total number of bytes
SocketSent. Send (buff, 0, len, SocketFlags. None );
}

// Add the "END" identifier at the END of the message to be sent
Msg = "END ";

// Send "msg"
SocketSent. Send (Encoding. Default. GetBytes (msg ));
SocketSent. Close ();
Read. Close ();
}

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.