One-to-many network communication is first added to favorites.
// Client
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. net. Sockets;
Using system. Threading;
Using system. IO;
Namespace tcpclient2
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}
Private void button#click (Object sender, eventargs E)
{
}
Private void button2_click (Object sender, eventargs E)
{
// If (new_tb.text.trim (). length> 0)
//{
// Tcpclient Tc = new tcpclient (ip_tb.text, Int. parse (point_tb.text ));
// Networkstream netstr = tc. getstream ();
// Netstr. Write (encoding. utf8.getbytes (new_tb.text), 0, encoding. utf8.getbytes (new_tb.text). Length );
// Writestring ("client:" + new_tb.text );
// New_tb.clear ();
// Byte [] newdate = new byte [1, 256];
// Int Len = netstr. Read (newdate, 0, newdate. Length );
// Writestring ("server:" + encoding. utf8.getstring (newdate ));
// Netstr. Close ();
// TC. Close ();
//}
If (new_tb.text.trim (). length> 0)
{
Tcpclient Tc = new tcpclient (ip_tb.text, Int. parse (point_tb.text ));
Byte [] DATA = encoding. utf8.getbytes (new_tb.text );
Networkstream netstr = tc. getstream ();
Netstr. Write (data, 0, Data. Length );
Writestring ("client:" + new_tb.text );
New_tb.clear ();
Byte [] newdate = new byte [256];
Int Len = netstr. Read (newdate, 0, newdate. Length );
Writestring ("server:" + encoding. utf8.getstring (newdate ));
Netstr. Close ();
TC. Close ();
}
}
Void writestring (string S)
{
Old_tb.text + = datetime. Now. tostring ("YY/MM/dd hh: mm: SS") + S;
Old_tb.text + = "/R/N ";
}
Private void form=formclosed (Object sender, formclosedeventargs E)
{
Environment. Exit (0 );
}
}
}
// Server
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. net. Sockets;
Using system. net;
Using system. Threading;
Using system. IO;
Namespace tcpclient1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}
Private void textbox2_textchanged (Object sender, eventargs E)
{
}
Private void button#click (Object sender, eventargs E)
{
Thread serverth = new thread (serverlistener );
Serverth. Start ();
}
// Void serverlistener ()
//{
// IPaddress IP = IPaddress. parse (ip_tb.text );
// Tcplistener Server = new tcplistener (IP, Int. parse (point_tb.text ));
// Server. Start ();
// Byte [] bytes = new byte [1, 256];
// String data = NULL;
// While (true)
//{
// Tcpclient client = server. accepttcpclient ();
// Networkstream netstr = client. getstream ();
// Int I = 0;
// While (I = netstr. Read (bytes, 0, bytes. Length ))! = 0)
//{
// Data = system. Text. encoding. utf8.getstring (bytes );
// Writestring ("client:" + data );
// Data = new_tb.text;
// Byte [] MSG = system. Text. encoding. utf8.getbytes (data );
// Netstr. Write (MSG, 0, MSG. Length );
// Writestring ("server:" + data );
//}
//}
//}
List <tcpclient> List = new list <tcpclient> ();
Void serverlistener ()
{
IPaddress IP = IPaddress. parse (ip_tb.text );
Tcplistener Server = new tcplistener (IP, Int. parse (point_tb.text ));
Server. Start ();
Byte [] bytes = new byte [1, 256];
String data = NULL;
While (true)
{
Tcpclient client = server. accepttcpclient ();
Networkstream netstr = client. getstream ();
Int I = 0;
While (I = netstr. Read (bytes, 0, bytes. Length ))! = 0)
{
Data = system. Text. encoding. utf8.getstring (bytes );
Writestring ("client:" + data );
Data = new_tb.text;
Byte [] MSG = system. Text. encoding. utf8.getbytes (data );
Netstr. Write (MSG, 0, MSG. Length );
Writestring ("server:" + data );;
}
}
}
Void writestring (string S)
{
Old_tb.text + = datetime. Now. tostring ("YY/MM/dd hh: mm: SS") + S;
Old_tb.text + = "/R/N ";
}
Private void button2_click (Object sender, eventargs E)
{
}
Private void form=formclosed (Object sender, formclosedeventargs E)
{
Environment. Exit (0 );
}
}
}