客户端
XAML
<Window x:Class="Socket_.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
<Grid>
<TextBox Name="ESTIP" HorizontalAlignment="Left" Height="23" Margin="106,107,0,0" TextWrapping="Wrap" Text="172.18.1.7" VerticalAlignment="Top" Width="120"/>
<Label Content="目标IP:" HorizontalAlignment="Left" Margin="33,107,0,0" VerticalAlignment="Top"/>
<Label Content="目标端口:" HorizontalAlignment="Left" Margin="20,138,0,0" VerticalAlignment="Top"/>
<TextBox Name="ESTPort" HorizontalAlignment="Left" Height="23" Margin="106,138,0,0" TextWrapping="Wrap" Text="1234" VerticalAlignment="Top" Width="120"/>
<Button Name="Link" Content="连接" HorizontalAlignment="Left" Margin="235,138,0,0" VerticalAlignment="Top" Width="75" Click="Link_Click"/>
<Button Name="Stop" Content="断开" HorizontalAlignment="Left" Margin="315,138,0,0" VerticalAlignment="Top" Width="75" Click="Stop_Click"/>
<TextBox Name="fstxt" HorizontalAlignment="Left" Height="23" Margin="33,181,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="193"/>
<Button Name="fs" Content="发送" HorizontalAlignment="Left" Margin="235,181,0,0" VerticalAlignment="Top" Width="75" Click="fs_Click"/>
<ListBox Name="ListRet" HorizontalAlignment="Left" Height="100" Margin="33,210,0,0" VerticalAlignment="Top" Width="277"/>
</Grid>
</Window>
客户端C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Socket_
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ServerSocket news = new ServerSocket();
news.Show();
}
private Socket S = null;
//创建新线程
private Thread ReceiveThread;
bool isopen = false;
private void Link_Click(object sender, RoutedEventArgs e)
{
//创建一个IpEndpoint对象
S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
int TargetPort = Convert.ToInt32(ESTPort.Text);
IPAddress tarIP = IPAddress.Parse(ESTIP.Text);
IPEndPoint ipe = new IPEndPoint(tarIP, TargetPort);
S.Connect(ipe);
ReceiveThread = new Thread(ReceiveServerMessage);
//开始线程
ReceiveThread.Start();
isopen = true;
}
catch(Exception E)
{
MessageBox.Show(E.ToString());
}
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
try
{
//通知服务器我要断开
send_F("MainWindow 断开连接");
S.Shutdown(SocketShutdown.Both);
S.Disconnect(false);
//停止线程
//System.Threading.Thread.CurrentThread.Abort();
//这个是终止当前的,你自己要终止别的也可以,只要你获取到要终止的线程 使用 .Abort();
isopen = false;
ReceiveThread.Abort();
ListRet.Items.Add("已断开");
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
}
private void send_F(string text)
{
try
{
ASCIIEncoding asen = new ASCIIEncoding();
//byte[] data = asen.GetBytes(text);
byte[] data = Encoding.UTF8.GetBytes(text);
S.Send(data);
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
}
private void fs_Click(object sender, RoutedEventArgs e)
{
try
{
string text = fstxt.Text;
ASCIIEncoding asen = new ASCIIEncoding();
//byte[] data = asen.GetBytes(text);
byte[] data = Encoding.UTF8.GetBytes(text);
S.Send(data);
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
}
void ReceiveServerMessage()
{
while(true)
{
while (isopen)
{
byte[] retdata = new byte[2500];
int length = S.Receive(retdata);
//retdata = retdata.Take(length - 2).ToArray();
//string Sdata = System.Text.Encoding.Default.GetString(retdata);
string Sdata = Encoding.UTF8.GetString(retdata, 0, length);
ListRet.Dispatcher.Invoke(new Action(delegate
{
ListRet.Items.Add(Sdata);
}));
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
//通知服务器我要断开
send_F("MainWindow 断开连接");
S.Shutdown(SocketShutdown.Both);
S.Disconnect(false);
//停止线程
//System.Threading.Thread.CurrentThread.Abort();
//这个是终止当前的,你自己要终止别的也可以,只要你获取到要终止的线程 使用 .Abort();
isopen = false;
ReceiveThread.Abort();
ListRet.Items.Add("已断开");
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
}
}
}
服务端
XAML
<Window x:Class="Socket_.ServerSocket"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ServerSocket" Height="300" Width="300">
<Grid>
<TextBox Name="ip" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="172.18.1.7" VerticalAlignment="Top" Width="120"/>
<TextBox Name="port" HorizontalAlignment="Left" Height="23" Margin="148,10,0,0" TextWrapping="Wrap" Text="1234" VerticalAlignment="Top" Width="71"/>
<Button Content="开启服务器" HorizontalAlignment="Left" Margin="10,51,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<ListBox Name="listtext" HorizontalAlignment="Left" Height="100" Margin="10,137,0,0" VerticalAlignment="Top" Width="274"/>
</Grid>
</Window>
C#
using System;
using System.Collections.Generic;
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.Shapes;
namespace Socket_
{
/// <summary>
/// ServerSocket.xaml 的交互逻辑
/// </summary>
public partial class ServerSocket : Window
{
public ServerSocket()
{
InitializeComponent();
}
//新建线程
//Thread thread;
Socket serverSocket;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//thread = new Thread(ServerF);
//thread.Start();
var ip_ = ip.Text;
int port_ = Convert.ToInt32(port.Text);
ServerF(ip_, port_);
}
private void ServerF(string ip_, int port)
{
try
{
IPAddress ip = IPAddress.Parse(ip_);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, port));
serverSocket.Listen(10);
this.listtext.Items.Add("等待连接" + serverSocket.LocalEndPoint.ToString());
//通过Clientsoket发送数据
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
}
catch (EncoderFallbackException e)
{
MessageBox.Show(e.ToString());
}
finally
{ }
}
private void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
clientSocket.Send(Encoding.UTF8.GetBytes("来自服务器的:你好!"));
Thread receiveThread = new Thread(receiveMessage);
receiveThread.Start(clientSocket);
}
}
//接收消息
private void receiveMessage(object clientSocket)
{
Socket myCliensocket = (Socket)clientSocket;
byte[] data = new byte[1024];
while (true)
{
try
{
//通过clienSocket接收数据
int receveNumber = myCliensocket.Receive(data);
//data = data.Take(receveNumber).ToArray();
if (receveNumber > 0)
listtext.Dispatcher.Invoke(new Action(delegate
{
this.listtext.Items.Add(Encoding.UTF8.GetString(data, 0, receveNumber));
}));
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
}