使用Winform 实现两个窗体间的对话
开启服务
客户端开始连接
进行通讯
服务端
后台代码
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
Socket serverSocket = null ;
private void button1_Click ( object sender, EventArgs e)
{
serverSocket = new Socket ( SocketType. Stream, ProtocolType. Tcp) ;
IPAddress ip = IPAddress. Parse ( textBox1. Text) ;
IPEndPoint ep = new IPEndPoint ( ip, int . Parse ( textBox2. Text) ) ;
try
{
serverSocket. Bind ( ep) ;
serverSocket. Listen ( 0 ) ;
richTextBox1. AppendText ( "启动成功!\r\n" ) ;
Thread th = new Thread ( WatchCilent) ;
th. Start ( ) ;
}
catch
{
richTextBox1. AppendText ( "启动失败!\r\n" ) ;
}
}
List< Socket> slist = new List < Socket > ( ) ;
public void WatchCilent ( )
{
while ( true )
{
var myCilent = serverSocket. Accept ( ) ;
slist. Add ( myCilent) ;
this . Invoke ( new Action ( ( ) = >
{
listBox1. Items. Add ( myCilent. RemoteEndPoint. ToString ( ) ) ;
richTextBox1. AppendText ( "客户端:(" + myCilent. RemoteEndPoint. ToString ( ) + "):连接成功!" + "\r\n" ) ;
} ) ) ;
Thread th = new Thread ( new ParameterizedThreadStart ( startClient) ) ;
th. IsBackground = true ;
th. Start ( myCilent) ;
}
}
public void startClient ( object myClient)
{
var cilentSocket = myClient as Socket;
while ( true )
{
var bytes = new byte [ 1024 * 1024 ] ;
var mlength = cilentSocket. Receive ( bytes) ;
string msg = Encoding. UTF8. GetString ( bytes, 0 , mlength) ;
this . Invoke ( new Action ( ( ) = >
{
richTextBox1. AppendText ( "客户端:(" + DateTime. Now + "):" + msg + "\r\n" ) ;
} ) ) ;
}
}
private void button2_Click ( object sender, EventArgs e)
{
var bytes = Encoding. UTF8. GetBytes ( textBox3. Text. Trim ( ) ) ;
foreach ( var item in slist)
{
item. Send ( bytes) ;
}
richTextBox1. AppendText ( "服务端:(" + DateTime. Now + "):" + textBox3. Text. Trim ( ) + "\r\n" ) ;
}
private void Form1_FormClosed ( object sender, FormClosedEventArgs e)
{
serverSocket. Close ( ) ;
}
}
客户端
后台代码
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
Socket cilentSocket = null ;
private void button1_Click ( object sender, EventArgs e)
{
cilentSocket = new Socket ( SocketType. Stream, ProtocolType. Tcp) ;
IPAddress ip = IPAddress. Parse ( textBox1. Text) ;
IPEndPoint ep = new IPEndPoint ( ip, int . Parse ( textBox2. Text) ) ;
try
{
cilentSocket. Connect ( ep) ;
richTextBox1. AppendText ( "连接成功!\r\n" ) ;
Thread th = new Thread ( StartMsg) ;
th. IsBackground = true ;
th. Start ( ) ;
}
catch
{
richTextBox1. AppendText ( "连接失败!\r\n" ) ;
}
}
public void StartMsg ( )
{
while ( true )
{
var bytes = new byte [ 1024 * 1024 ] ;
var mlength = cilentSocket. Receive ( bytes) ;
string msg = Encoding. UTF8. GetString ( bytes, 0 , mlength) ;
this . Invoke ( new Action ( ( ) = >
{
richTextBox1. AppendText ( "服务端:(" + DateTime. Now + "):" + msg + "\r\n" ) ;
} ) ) ;
}
}
private void button2_Click ( object sender, EventArgs e)
{
var bytes = Encoding. UTF8. GetBytes ( textBox3. Text. Trim ( ) ) ;
cilentSocket. Send ( bytes) ;
richTextBox1. AppendText ( "客户端:(" + DateTime. Now + "):" + textBox3. Text. Trim ( ) + "\r\n" ) ;
}
private void Form1_FormClosed ( object sender, FormClosedEventArgs e)
{
cilentSocket. Close ( ) ;
}
}