原文:https://blog.csdn.net/qq_28602957/article/details/53443600
本例子为简单使用TcpListener和TcpClient实现通信,含有诸多不足,仅供初学者参考。
server:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Server
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private TcpListener myListener;
private TcpClient newClient;
public BinaryReader br;
public BinaryWriter bw;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Thread myThread = new Thread(ServerA);
myThread.Start();
}
private void ServerA() {
IPAddress ip = IPAddress.Parse("127.0.0.1");//服务器端ip
myListener = new TcpListener(ip, 7890);//创建TcpListener实例
myListener.Start();//start
newClient = myListener.AcceptTcpClient();//等待客户端连接
label.Dispatcher.Invoke(()=>label.Content = "连接成功");
while (true)
{
try
{
NetworkStream clientStream = newClient.GetStream();//利用TcpClient对象GetStream方法得到网络流
br = new BinaryReader(clientStream);
string receive = null;
receive = br.ReadString();//读取
textBox.Dispatcher.Invoke(() => textBox.Text += receive+"\r\n");
}
catch
{
MessageBox.Show("接收失败!");
}
}
}
//发送消息
private void send_Click(object sender, RoutedEventArgs e)
{
//利用TcpClient对象GetStream方法得到网络流
NetworkStream clientStream = newClient.GetStream();
bw = new BinaryWriter(clientStream);
//写入
bw.Write(message.Text);
textBox.Text += message.Text + "\r\n";
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
client:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Client
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private TcpClient client;
public BinaryReader br;
public BinaryWriter bw;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Thread myThread = new Thread(ClientA);
myThread.Start();
}
private void ClientA() {
//通过服务器的ip和端口号,创建TcpClient实例
client = new TcpClient("127.0.0.1", 7890);
label.Dispatcher.Invoke(()=> label.Content = "与服务器连接成功");
while (true)
{
try
{
NetworkStream clientStream = client.GetStream();
br = new BinaryReader(clientStream);
string receive = null;
receive = br.ReadString();
textBox.Dispatcher.Invoke(() => textBox.Text += receive + "\r\n");
}
catch
{
MessageBox.Show("接收失败!");
}
}
}
//发送消息
private void send_Click(object sender, RoutedEventArgs e)
{
NetworkStream clientStream = client.GetStream();
bw = new BinaryWriter(clientStream);
bw.Write(message.Text);
textBox.Text += message.Text + "\r\n";
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
补充:
textbox多行显示
(1)将TextWrapping设为”Wrap”
(2)将AcceptsReturn设为”True”
(3)将VerticalScrollBarVisibility设置为”Visible”
运行结果: