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;
}
Because the use is a thread, the send text is used by 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.
Similar methods for sending files:
Private vid startsendfile (String strHost, string strfile) {
Sendfile sffile = new sendfile (strHost, iport, strfile, this.pbmain);
Pbmain.value = 0;
StartThread (New ThreadStart (sffile.send);
Send a file class:
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, is this not a Trojan?) Also:
I only give some code here, the rest you can exert below:
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 for the server-side acceptance:
Create a monitor:
///
/ / Re-given the host and port creation listener
/// summary>
/// param>
/// param>
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
/// 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 starting listening, the connection to each listened client is handled with a separate thread, and the processing is completed by 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.