1、TCP实现类
internal class TcpClient
{
Socket ClientSocket { get; set; }
public byte[] ReceiveBuffer { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="receiveBufferLength"></param>
public TcpClient(int receiveBufferLength)
{
ReceiveBuffer = new byte[receiveBufferLength];
}
/// <summary>
/// 连接服务器
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public void Start(string ip, string port)
{
ClientSocket = CreateSocket(ip, port);
CreateThread();
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="buffer"></param>
public void Send(byte[] buffer)
{
ClientSocket.Send(buffer);
}
/// <summary>
/// 断开服务器
/// </summary>
public void Stop()
{
ClientSocket.Close();
}
/// <summary>
/// 创建客户端Socket
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
private Socket CreateSocket(string ip, string port)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(ip);
IPEndPoint endPoint = new IPEndPoint(address, int.Parse(port));
socket.Connect(endPoint);
return socket;
}
/// <summary>
/// 创建服务端发来消息的线程并启动
/// </summary>
private void CreateThread()
{
Thread clientThread = new Thread(ReceiverThread);
clientThread.IsBackground = true;
clientThread.Start();
}
/// <summary>
/// 接收服务端发来消息的处理方法
/// </summary>
private void ReceiverThread()
{
int bufferlength = 2 * 1024;
while (true)
{
int length = 0;
byte[] buffer = new byte[bufferlength];
try
{
length = ClientSocket.Receive(buffer);
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
break;
}
Buffer.BlockCopy(buffer, 0, ReceiveBuffer, 0, length);
}
}
}
2、实现界面
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="配置" VerticalAlignment="Center"/>
<DockPanel Grid.Row="1" HorizontalAlignment="Left">
<TextBlock Text="地址" VerticalAlignment="Center" Margin="20 0 10 0"/>
<TextBox x:Name="TextBoxIp" Text="" Width="100" Height="36" VerticalContentAlignment="Center"/>
<TextBlock Text="端口" VerticalAlignment="Center" Margin="20 0 10 0"/>
<TextBox x:Name="TextBoxport" Text="" Width="100" Height="36" VerticalContentAlignment="Center"/>
<Button x:Name="ButtonStart" Content="连接" Width="75" Height="36" Margin="20,0,10,0" Click="ButtonStart_Click"/>
</DockPanel>
<TextBlock Grid.Row="2" Text="接收" VerticalAlignment="Center"/>
<TextBox Grid.Row="3" Name="TextBoxReciver" Text=""/>
<TextBlock Grid.Row="4" Text="发送" VerticalAlignment="Center"/>
<StackPanel Grid.Row="5" Orientation="Horizontal">
<TextBox x:Name="TextBoxSender" Text="" Width="650" Height="36"/>
<Button Content="发送" Click="ButtonSend_CLick" Width="75" Height="36" HorizontalAlignment="Right" Margin="10 0 20 0"/>
</StackPanel>
</Grid>
3、后台代码
TcpClient tcpClient = null;
public MainWindow()
{
InitializeComponent();
tcpClient=new TcpClient(2 * 1024);
TextBoxIp.Text = "10.11.10.44";
TextBoxport.Text = "8080";
}
private void ButtonStart_Click(object sender, RoutedEventArgs e)
{
string address = this.TextBoxIp.Text;
string[] arr = address.Split('.');
if (arr.Length == 4)
{
foreach (string item in arr)
{
int i = int.Parse(item);
if (i < 0 || i > 255)
{
return;
}
}
}
string port = this.TextBoxport.Text;
int j = int.Parse(port);
if (j < 0)
{
return;
}
tcpClient.Start(address, port);
}
private void ButtonSend_CLick(object sender, RoutedEventArgs e)
{
string str = TextBoxSender.Text;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
tcpClient.Send(bytes);
}
4、运行效果