Socket File Transfer

Source: Internet
Author: User

Private string Path = "f :\\ smartmovie. EXE"; // file to be sent

Private socket S;

Private void listen ()
{
String IP = "127.0.0.1"; // The remote IP address is defined as your machine.
IPaddress [] ih = DNS. gethostaddresses (IP); // obtain the IP address list
IPaddress newip = IH [0]; // obtain the IP address
Int Port = 6789; // define the port
Ipendpoint conncet = new ipendpoint (newip, Port); // construct a node
S = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP); // initialize the socket

try
{< br> S. connect (conncet); // connect to the remote server
If (S. connected) // If the connection is successful. connected is true, otherwise false
{

Console. writeline ("connection successful ");
Thread t = new thread (New threadstart (SET); // create a process
T. Start (); // start the process
Console. writeline ("sent ")

}

}
Catch (nullreferenceexception E)
{

Console. writeline ("{0}", e );

}

Private void set () // create the Set Function
{
Console. writeline ("start to send data ");
Byte [] B = new byte [10000000]; // creates a File Buffer, which can be considered as the maximum value of a file.
Filestream file = file. Open (path, filemode. Open, fileaccess. Read); // create a file stream
Int start = 0;
Int end = (INT) file. length; // get the file length. If the file transfer needs to exceed the int range, it is estimated that the filestream class will be rewritten.

Try
{
While (end! = 0)
{
Int COUNT = file. Read (B, start, end); // write data into the stream
Start + = count;
End-= count;
}
While (start! = 0)
{
Int n = S. Send (B, end, start, socketflags. None); // use the send method of socket to send a stream
End + = N;
Start-= N;
}
File. Close (); // close the file stream
S. Close (); // close the socket
}
Catch (nullreferenceexception E)
{
Console. writeline ("{0}", e );
}
}

In this way, the file transmission model is implemented.

To receive a file, first determine the length of the file to be sent by the other party. In fact, the above section also includes the function of sending the file length. The implementation is very simple, that is, to send the int variable end, and then request to receive the file.CodeReturn a Boolean to determine whether to send the message. The principle is not implemented here to make it clearer.

Private void get ()
{
String Path = "G: \ da.exe"; // received File
Filestream file = new filestream (path, filemode. openorcreate, fileaccess. Write); // write the file stream
Tcplistener Listen = new tcplistener (6789); // listener Port
Socket S1 = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP); // define the socket and initialize
Try
{
Listen. Start (); // start listening
S1 = listen. acceptsocket (); // obtain the socket connection
Byte [] DATA = new byte [10000000]; // defines the buffer
Int longer = data. length;
Int start = 0;
Int mid = 0;
If (s1.connected) // confirm the connection
{
Console. writeline ("connection successful ");
Int COUNT = s1.receive (data, start, longer, socketflags. None); // Save the received byte to the buffer zone
Mid + = count;
Longer-= mid;
While (count! = 0)
{
Count = s1.receive (data, mid, longer, socketflags. None );
Mid + = count;
Longer-= mid;
}
File. Write (data, 0, 1214134); // write the file. The value 1214134 indicates the file size, which can be sent using Socket. The code is described earlier.
S1.close ();
File. Close ();
}
}
Catch (nullreferenceexception E)
{
Console. writeline ("{0}", e );
}
}

Http://www.cnblogs.com/wsy6634/archive/2008/10/13/1310294.html

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.