Sending code:
Public void startsendfilethread (){
Try
{
Thread sendfilethread = new thread (New threadstart (sendfile ));
Sendfilethread. Start ();
}
Catch {
MessageBox. Show ("an error occurred when the thread is enabled or the thread has been sent ...... ");
This. sendflag = false;
}
Finally
{
}
}
Public void sendfile ()
{
Public socket socketclient = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Ipendpoint ipep = new ipendpoint (IPaddress. parse (userip), userport );
Try
{
// Because the client is only used to send information to a specific server, you do not need to bind the local IP address and port. You do not need to listen.
Socketclient. sendbuffersize = 1024*1024*10;
Socketclient. receivebuffersize = 1024*1024*10;
Socketclient. Connect (ipep );
// MessageBox. Show ("connected !! ");
}
Catch (socketexception E)
{
Console. writeline ("unable to connect to server ");
Console. writeline (E. tostring ());
}
Filestream FS = new filestream (filepath, filemode. Open, fileaccess. Read );
Int partsize = 1024*100;
Long filelength = FS. length;
Long offset = 0;
While (true)
{
If (Offset + partsize> = filelength)
Partsize = convert. toint32 (filelength-offset );
Byte [] temp = new byte [partsize];
FS. Position = offset;
FS. Read (temp, 0, partsize );
Socketclient. Send (temp, temp. length, socketflags. None); // send data to the specified endpoint
// Mycontrol. trafransferssize + = partsize;
If (Offset + partsize> = filelength)
Break;
Offset + = partsize;
}
// Mychat. rich_out.appendtext ("sent:" + filename + "\ n ");
Socketclient. Shutdown (socketshutdown. Both );
Socketclient. Close ();
// Mychat. removefileinfofromcontrol (filename );
}
Some code of the acceptor:
Public void listener () // listens to local port 5421
{
If (classtcpsocket = NULL)
Classtcpsocket = new classstcpsocket (5421 );
Else
{
Try
{
Thdudp = new thread (New threadstart (getudpdata); // create a thread
Thdudp. Start (); // execute the current thread
}
Catch (exception E)
{
MessageBox. Show (E. tostring (); // display thread error information
}
}
}
Public void getudpdata () // gets the received message
{
Serversocket = classtcpsocket. gettcpsocker (). Accept (); // wait for a tcpsocker
Byte [] DATA = NULL;
Int number = 0;
Bool flag = true;
While (FLAG)
{
Data = new byte [1024*10];
If (number = serversocket. Receive (data ))! = 0)
{
Filestream FS = NULL;
Try
{
FS = new filestream (filepath, filemode. Open, fileaccess. readwrite, fileshare. readwrite); // file. openwrite (filepath );
FS. Seek (0, seekorigin. End); // set the current bit value of the stream to 0
FS. Write (data, 0, number); // write the received information to the file
// Mycontrol. trafransferssize + = number;
}
Finally
{
FS. Close ();
}
}
Else flag = false;
}
// Mychat. rich_out.appendtext ("received successfully:" + filename + "\ n file size:" + gettext (mycontrol. trafransferssize) + "\ n ");
Docleanoperation ();
}
Public void docleanoperation (){
If (serversocket. Connected)
{
Serversocket. Shutdown (socketshutdown. Both );
Serversocket. Close ();
}
Classtcpsocket. Active = false;
// Mychat. removefileinfofromcontrol (filename); // panelsendfile. Controls. Remove (mycontrol );
Thdudp. Abort ();
}
// Classtcpsocket
Class
Using system;
Using system. componentmodel;
Using system. Collections. Generic;
Using system. diagnostics;
Using system. text;
Using system. net. Sockets;
Using system. net;
Using system. Windows. forms;
Using system. Threading;
Namespace qqclient
{
Public class classtcpsocket
{
Socket tcpsocket = NULL;
Private int localport = 5421;
Public int localport
{
Get {return localport ;}
Set {localport = value ;}
}
Public classtcpsocket (INT localport ){
This. localport = localport;
Init ();
}
Private string localhost = "127.0.0.1 ";
Public String localhost
{
Get {return localhost ;}
Set {localhost = value ;}
}
Private bool active = false;
Public bool active
{
Get {return active ;}
Set // read the value of this attribute
{
Active = value;
If (tcpsocket = NULL)
Init ();
If (active)
{
Openlisten ();
}
Else
Stoplisten ();
}
}
Public void Init (){
Byte [] DATA = new byte [1024];
Ipendpoint ipep = new ipendpoint (IPaddress. parse (classgetlocalinfo. getlocalhostip (), localport); // define a network endpoint and store the local IP address and port number as the network endpoint
Tcpsocket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP); // define a socket
Tcpsocket. BIND (ipep); // The socket is associated with a local endpoint.
}
Public void openlisten (){
If (this. tcpsocket! = NULL)
{
Tcpsocket. Listen (10 );
// MessageBox. Show (tcpsocket. addressfamily + ":" + tcpsocket. localendpoint );
}
Else
{
Init ();
Tcpsocket. Listen (10 );
}
}
Public void stopsocket (){
If (this. tcpsocket! = NULL)
{
Tcpsocket. Shutdown (socketshutdown. Both );
Tcpsocket. Close ();
}
}
Public void stoplisten (){
If (this. tcpsocket! = NULL & this. tcpsocket. Connected)
{
Tcpsocket. Shutdown (socketshutdown. Both );
}
}
Public socket gettcpsocker (){
Return tcpsocket;
}
}
}