Point-to-Point communication and file transfer using C # (send receive part)

Source: Internet
Author: User
Tags sendfile socket thread
It describes the base class for communication, and the following is the part that uses that class for sending and receiving:

Second, send part:

We use multiple threads, we can do multiple tasks at the same time, such as sending files, sending text, and so on, do not affect each other:

Send Text method:

private void Startsendtext (string strhost,int iport,string strinfo)

{

SendText sttext = new SendText (strhost,iport,strinfo,new communclass.onsend (onsenddrawprogress));

Startthread (new ThreadStart (sttext.send));

}

Here are some of the methods he calls:

Start a thread

private void Startthread (ThreadStart target)

{

Thread dostep = new Thread (target);

Dostep.isbackground = true;

Dostep.start ();

}

Send part (set in this article is 1024 bytes) After the successful callback method

public void onsenddrawprogress (int itotal,int isending)

{

if (itotal!= pbmain.maximum)

Pbmain.maximum = Itotal;

Pbmain.value = isending;

}

Because the thread is used, the sending text uses a method of sending a text class, which is as follows:

public class SendText

{

private string Host;

private int Port;

private string Info;

Private Communclass.onsend OnSend;

Public SendText (String strhost,int iport,string strinfo,

Communclass.onsend onsend)

{

Host = Strhost;

Port = Iport;

Info = Strinfo;

OnSend = OnSend;

}

public void Send ()

{

Socket s = null;

Try

{

s = communclass.connecttoserver (Host,port);



Communclass.writecommandtosocket (S, "SendText");

Communclass.writecommanddesctosocket (S, "");

Communclass.writedynamictexttosocket (S,info,onsend);

}

catch (Exception e)

{

MessageBox.Show (E.message);

}

Finally

{

if (s!= null)

S.close ();

}

}



}//end class



This allows you to send text using a single thread.

The methods for sending files are similar:

private void Startsendfile (string strhost,int iport,string strfile)

{

SendFile sffile = new SendFile (strhost,iport,strfile,this.pbmain);

Pbmain.value = 0;

Startthread (new ThreadStart (sffile.send));

}

Classes to send files:

public class SendFile

{

private string Host;

private int Port;

private string Filetosend;

Private ProgressBar Pbar;



Public SendFile (String strhost,int iport,string strfile,progressbar pbmain)

{

Host = Strhost;

Port = Iport;

Filetosend = strfile;

Pbar = Pbmain;

}

public void Send ()

{

Socket s = null;

Try

{

s = communclass.connecttoserver (Host,port);



Communclass.writecommandtosocket (S, "SENDFILE");

Communclass.writecommanddesctosocket (S, "");



Communclass.writefiletosocket (s,filetosend,new communclass.onsend (onsenddrawprogress));

}

catch (Exception e)

{

MessageBox.Show (E.message);

}

Finally

{

if (s!= null)

S.close ();

}

}





public void onsenddrawprogress (int itotal,int isending)

{

if (Itotal!= pbar. Maximum)

Pbar. Maximum = Itotal;



Pbar. Value = isending;

}



}//end class

Of course, you send a command to the server to start a program (rely on, this is not a Trojan?) ) can also:

I only give a part of the code here, the rest of you can play the following:

public class Executefile

{

private string Host;

private int Port;

private string FileName;

private string Cmdparam;



Public Executefile (String strhost,int iport,string strfilename,string strcmdparam)

{

Host = Strhost;

Port = Iport;

FileName = strFileName;

Cmdparam = Strcmdparam;

}



public void Send ()

{

Socket s = null;

Try

{

s = communclass.connecttoserver (Host,port);



Communclass.writecommandtosocket (S, "executefile");

Communclass.writecommanddesctosocket (S,filename);

Communclass.writedynamictexttosocket (S, "", null);

MessageBox.Show (Communclass.readdynamictextfromsocket (s));

}

catch (Exception e)

{

MessageBox.Show (E.message);

}

Finally

{

if (s!= null)

S.close ();

}



}

}

The following is the server-side code that accepts information:

To create a listener:

<summary>

Create a listener on a given host and port

</summary>

<param name= "Straddress" ></param>

<param name= "Iport" ></param>

private void Buildingserver (String straddress,int iport)

{

IPAddress ipaddress = Dns.resolve (straddress). ADDRESSLIST[0];



Try

{

Listener = new TcpListener (ipaddress, Iport);

}

catch (Exception e)

{

Addinfo (E.message);

}

}


Start listening:


<summary>

Start listening.

</summary>

private void Startlisten ()

{

bool done = false;



Listener. Start ();

while (!done)

{

Socket s = listener. AcceptSocket ();

if (s!= null)

{

Dealwithsocket DWS = new Dealwithsocket (s,this.tblog);

Startthread (new ThreadStart (DWS). Dealwith));

}

}

}



private void Startthread (ThreadStart target)

{

Thread dostep = new Thread (target);

Dostep.isbackground = true;

Dostep.start ();

}



After beginning the listening, the connection for each monitored client is handled with a separate thread, and the processing is done through the class Dealwithsocket, the following is the class code:

public class Dealwithsocket

{

Private Socket s = null;

Private TextBox tblog = null;

Public Dealwithsocket (Socket newsocket,textbox tbinfo)

{

s = newsocket;

Tblog = Tbinfo;

}



public void Dealwith ()

{

String strcmd = Communclass.readcommandfromsocket (s);

String Strdesc = Communclass.readcommanddescfromsocket (s);

Addinfo (Strcmd);

Switch (strcmd)

{

Case "SENDFILE":

Communclass.readdynamicfilefromsocket (S, "e:\\rrr.txt");

break;

Case "Executefile":

String strparam = Communclass.readdynamictextfromsocket (s);

String strresult = Executefile (Strdesc,strparam);

Communclass.writedynamictexttosocket (S,strresult,null);

break;

Default

String strdetail = Communclass.readdynamictextfromsocket (s);

Addinfo (Strdetail);

break;

}

Try

{

S.close ();

}

catch (Exception e)

{

Addinfo (E.message);

}

}

private void Addinfo (string strinfo)

{

String Info = DateTime.Now.ToLongTimeString () + "" + Strinfo + "\ r \ n";

Tblog.text + = Info;

Tblog.refresh ();

}

private string Executefile (String strfilename,string strcmdparam)

{

System.Diagnostics.Process proc = new System.Diagnostics.Process ();

Proc. Startinfo.filename = strFileName;

Proc. Startinfo.arguments = Strcmdparam;

Try

{

Proc. Start ();

return "OK";

}

catch (Exception err)

{

return err. message;

}

}

}//end class

The above is the code used, I hope you can critically correct.


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.