You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Net.Sockets;
using System.Net;
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.Threading;
using System.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace SocketExample
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
IPAddress ip;
int port;
string path;
public MainWindow()
{
path = "C:\\Users\\Administrator\\source\\repos\\SocketExample\\SocketExample\\bin\\Debug\\config.txt";
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ip = IPAddress.Parse(IPtextbox.Text);
port = int.Parse(Porttextbox.Text);
// 创建TCPListener
TcpListener server = new TcpListener(ip, port);
// 开始监听
server.Start();
InfoTextBlock.Text += "正在监听...\n";
Thread thread = new Thread(Listen);
thread.IsBackground = true;
thread.Start(server);
}
TcpClient client;
void Listen(object o)
{
TcpListener socketWatch = o as TcpListener;
while (true)
{
// 接受客户端连接,进程会一直在这等待,直到客户端链接才会继续下一步。
client = socketWatch.AcceptTcpClient();
Dispatcher.Invoke(() => {
InfoTextBlock.Text += "客户端已连接!\n";
});
Thread thread = new Thread(Recive);
thread.IsBackground = true;
thread.Start(client);
}
}
void Recive(Object o)
{
TcpClient tcpClient = o as TcpClient;
NetworkStream stream = tcpClient.GetStream();
while (true)
{
// 处理客户端请求
try
{
// 读取客户端消息
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0) {
Dispatcher.Invoke(() =>
{
InfoTextBlock.Text += $"客户端已断开连接\n";
});
break;
}
string request = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Dispatcher.Invoke(() =>
{
InfoTextBlock.Text += $"接收到客户端消息: {request}\n";
});
// 发送响应
string response = $"服务器已收到你的消息: {request}";
byte[] responseBytes = Encoding.UTF8.GetBytes(response);
stream.Write(responseBytes, 0, responseBytes.Length);
Dispatcher.Invoke(() =>
{
InfoTextBlock.Text += "已发送响应给客户端\n";
});
}
catch (Exception ex)
{
Console.WriteLine($"处理请求时出错: {ex.Message}");
}
}
// 关闭连接
stream.Close();
client.Close();
}
private void Button_Click_Clear(object sender, RoutedEventArgs e)
{
this.InfoTextBlock.Text = string.Empty;
}
private void Button_Click_Create(object sender, RoutedEventArgs e)
{
ClientWindow clientWindow = new ClientWindow();
clientWindow.Show();
}
private void Button_Click_ReadFile(object sender, RoutedEventArgs e)
{
try
{
string[] ipconfig = File.ReadAllLines(path);
string[] ip = ipconfig[0].Split(':');
string[] port = ipconfig[1].Split(':');
InfoTextBlock.Text += ip[1];
InfoTextBlock.Text += port[1];
IPtextbox.Text = ip[1];
Porttextbox.Text = port[1];
}
catch(Exception ex)
{
InfoTextBlock.Text += ex.Message;
}
}
private void Button_Click_ReadSql(object sender, RoutedEventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=127.0.0.1;port=3306;database=Test;user=root;password=root;");
MySqlCommand comn = new MySqlCommand("select * from Product",conn);
if (conn.State == System.Data.ConnectionState.Closed) {
conn.Open();
}
MySqlDataReader reader = comn.ExecuteReader();
try
{
if (reader.HasRows) {
while (reader.Read()) {
InfoTextBlock.Text += "" + reader["ID"] + reader["Name"] + "\n";
}
}
}
catch (Exception ex) {
InfoTextBlock.Text += ex.Message;
}
finally
{
conn.Close();
}
}
}
}