One-to-multiple communication in the Network

Source: Internet
Author: User
Tags getstream

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 );
}
}
}

 

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.