The tcpsend window is used to send messages, and an application is written to receive messages.ProgramAfter a message is received, you must close the message receiving window to receive new messages.
Source code:
Message sending windowCode
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. net. Sockets;
Using system. IO;
Namespace tcpsocket
{
Public partial class frmtcpsend: Form
{
Public frmtcpsend ()
{
Initializecomponent ();
}
Private void buttonsendfile_click (Object sender, eventargs E)
{
// Tcpclient = new tcpclient (textboxhostname. Text, int32.parse (textboxport. Text ));
Tcpclient = new tcpclient ();
Tcpclient. Connect (IPaddress. parse (textboxhostname. Text), int32.parse (textboxport. Text ));
Networkstream NS = tcpclient. getstream ();
Filestream FS = file. Open (".. \ frmtcpsend. cs", filemode. Open );
Int DATA = FS. readbyte ();
While (Data! =-1)
{
NS. writebyte (byte) data );
Data = FS. readbyte ();
}
FS. Close ();
NS. Close ();
Tcpclient. Close ();
}
Private void buttonsendmessage_click (Object sender, eventargs E)
{
Tcpclient = new tcpclient ();
Tcpclient. Connect (IPaddress. parse (textboxhostname. Text), int32.parse (textboxport. Text ));
Networkstream NS = tcpclient. getstream ();
If (NS. canwrite)
{
Byte [] sendbytes = encoding. utf8.getbytes (textboxmessage. Text );
NS. Write (sendbytes, 0, sendbytes. Length );
}
Else
{
MessageBox. Show ("cannot write data streams", "Terminate", messageboxbuttons. OK, messageboxicon. Stop );
// Console. writeline ("You cannot write data to this stream .");
Tcpclient. Close ();
// Closing the tcpclient instance does not close the network stream.
NS. Close ();
Return;
}
NS. Close ();
Tcpclient. Close ();
}
}
}
Message Receiving Window code
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. net. Sockets;
Using system. IO;
Using system. Threading;
Namespace tcpreceive
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
// Thread = new thread (New threadstart (Listen ));
Thread thread = new thread (New threadstart (socketlisten ));
Thread. Start ();
IPaddress = IPaddress. Any; // IPaddress. parse ("172.16.102.11 ");
This. Text = IPaddress. tostring () + "listening ...";
}
Protected Delegate void updatedisplaydelegate (string text );
Public void listen ()
{
IPaddress = IPaddress. Any; // IPaddress. parse ("172.16.102.11 ");
Tcplistener = new tcplistener (IPaddress, 9999 );
Tcplistener. Start ();
Tcpclient = tcplistener. accepttcpclient ();
Networkstream NS = tcpclient. getstream ();
Streamreader sr = new streamreader (NS );
String result = Sr. readtoend ();
Invoke (New updatedisplaydelegate (updatedisplay), new object [] {result });
Tcpclient. Close ();
Tcplistener. Stop ();
}
Public void socketlisten ()
{
Socket listener = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Listener. BIND (New ipendpoint (IPaddress. Any, 9999 ));
Listener. Listen (0 );
Socket socket = listener. Accept ();
Stream netstream = new networkstream (socket );
Streamreader reader = new streamreader (netstream );
String result = reader. readtoend ();
Invoke (New updatedisplaydelegate (updatedisplay), new object [] {result });
Socket. Close ();
Listener. Close ();
}
Public void updatedisplay (string text)
{
Richtextbox1.text = text;
}
}
}