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

xiaoxiao2021-03-06  118

The base class of communication is introduced above, and below is the part of the sector to send and receive:

Second, the send part:

Send us to use multithreading, you can do multiple tasks at the same time, such as sending files, send text, etc., 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 is 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 a part (setting 1024 bytes in this article) successful callback method

Public void OnsenddrawProgress (int tentotal, int tenting)

{

IF (itotal! = pbmain.maximum)

Pbmain.maximum = ipotal;

PBMain.Value = ityding;

} Since the use is a thread, the send text is 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 oversend;

Public SendText (String strHost, int int, string strinfo,

Communclass.onsend Onsend)

{

Host = strhost;

Port = iPort;

INFO = STRINFO;

Onsend = Onsen;

}

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 will use a thread to send text. Method of sending a file is also 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 (SFFFILE.SEND));} Send a file: public class sendfile

{

Private string host;

PRIVATE INT Port;

PRIVATE STRING FILETOSEND;

Private ProgressBar PBAR;

Public Sendfile (String Strfile, 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 tentotal, int tenting)

{

IF (ITOTAL! = PBAR.MAXIMUM)

PBAR.MAXIMUM = ITAR;

PBAR.Value = isending;

}

} // End Class

Of course, you send a command to allow the server to launch a program (rely, this is not a Trojan?) You can: 俺 here only give a part of the code, the rest you can exert the following: public class executefile

{

Private string host;

PRIVATE INT Port;

PRIVATE STRING FILENAME;

Private string cmdparam;

Public Executefile (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 ();

}

}

}

Third, the following is the code of the server-side acceptance information: Create a monitor: ///

/ / Re-given the host and port creation listener

///

///

///

Private Void Building Server (String straddress, int port)

{

Ipaddress ipaddress = dns.resolve (straddress) .addresslist [0];

Try

{

Listener = New TCPListener (iPaddress, iPort);

}

Catch (Exception E)

{

AddInfo (E.MESSAGE);

}

}

Start listening: ///

/// Start listening

///

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 starting listening, the connection to each listened client is handled with a separate thread, processes through class DEALWITHSOCKET, below is 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: //rr.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 everyone is criticized.

转载请注明原文地址:https://www.9cbs.com/read-126623.html

New Post(0)