您的位置:首页 > 编程语言 > C#

C# 服务端与客户端示例(Socket通信)

2016-10-28 17:05 429 查看


服务器、客户端示例.exe下载

服务器端源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageServer
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(String[] args)
{
if (args.Length > 1)    // 通过命令行参数启动服务,如: call "%~dp0MessageServer.exe" 127.0.0.1 37280
{
new Server(null, args[0], args[1]).start();
}
else
{   // 通过windows窗体启动服务
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MessageServer());
}
}
}
}
namespace MessageServer
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

public class Server
{
public string ipString = "127.0.0.1";   // 服务器端ip
public int port = 37280;                // 服务器端口

public Socket socket;
public Print print;                     // 运行时的信息输出方法

public Dictionary<string, Socket> clients = new Dictionary<string, Socket>();   // 存储连接到服务器的客户端信息
public bool started = false;            // 标识当前是否启动了服务

public Server(Print print = null, string ipString = null, int port = -1)
{
this.print = print;
if (ipString != null) this.ipString = ipString;
if (port >= 0)this.port = port;
}

public Server(Print print = null, string ipString = null, string port = "-1")
{
this.print = print;
if (ipString != null) this.ipString = ipString;

int port_int = Int32.Parse(port);
if (port_int >= 0) this.port = port_int;
}

/// <summary>
/// Print用于输出Server的输出信息
/// </summary>
public delegate void Print(string info);

/// <summary>
/// 启动服务
/// </summary>
public void start()
{
try
{
IPAddress address = IPAddress.Parse(ipString);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(address, port));
socket.Listen(10000);

if (print != null)
{
try { print("启动服务【" + socket.LocalEndPoint.ToString() + "】成功"); }
catch { print = null; }
}
started = true;

new Thread(listenClientConnect).Start(socket);  // 在新的线程中监听客户端连接
}
catch (Exception exception)
{
if (print != null)
{
print("启动服务失败 " + exception.ToString());
}
started = false;
}
}

/// <summary>
/// 监听客户端的连接
/// </summary>
private void listenClientConnect(object obj)
{
Socket socket = (Socket) obj;
while (true)
{
Socket clientScoket = socket.Accept();
if (print != null)
{
print("客户端" + clientScoket.RemoteEndPoint.ToString() + "已连接");
}
new Thread(receiveData).Start(clientScoket);   // 在新的线程中接收客户端信息

Thread.Sleep(1000);                            // 延时1秒后,接收连接请求
if (!started) return;
}
}

/// <summary>
/// 发送信息
/// </summary>
public void Send(string info, string id)
{
if (clients.ContainsKey(id))
{
Socket socket = clients[id];

try
{
Send(socket, info);
}
catch(Exception ex)
{
clients.Remove(id);
if (print != null) print("客户端已断开,【" + id + "】");
}
}
}

/// <summary>
/// 通过socket发送数据data
/// </summary>
private void Send(Socket socket, string data)
{
if (socket != null && data != null && !data.Equals(""))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);   // 将data转化为byte数组
socket.Send(bytes);                            //
}
}

private string clientIp = "";
/// <summary>
/// 输出Server的输出信息到客户端
/// </summary>
public void PrintOnClient(string info)
{
Send(info, clientIp);
}

/// <summary>
/// 接收通过socket发送过来的数据
/// </summary>
private void receiveData(object obj)
{
Socket socket = (Socket) obj;

string clientIp = socket.RemoteEndPoint.ToString();                 // 获取客户端标识 ip和端口
if (!clients.ContainsKey(clientIp)) clients.Add(clientIp, socket);  // 将连接的客户端socket添加到clients中保存
else clients[clientIp] = socket;

while (true)
{
try
{
string str = Receive(socket);
if (!str.Equals(""))
{
if (str.Equals("[.Echo]"))
{
this.clientIp = clientIp;
print = new Print(PrintOnClient);     // 在客户端显示服务器输出信息
}
if (print != null) print("【" + clientIp + "】" + str);

if (str.Equals("[.Shutdown]")) Environment.Exit(0); // 服务器退出
else if (str.StartsWith("[.RunCmd]")) runCMD(str);  // 执行cmd命令
}
}
catch (Exception exception)
{
if (print != null) print("连接已自动断开,【" + clientIp + "】" + exception.Message);

socket.Shutdown(SocketShutdown.Both);
socket.Close();

if (clients.ContainsKey(clientIp)) clients.Remove(clientIp);
return;
}

if (!started) return;
Thread.Sleep(200);      // 延时0.2秒后再接收客户端发送的消息
}
}

/// <summary>
/// 执行cmd命令
/// </summary>
private void runCMD(string cmd)
{
new Thread(runCMD_0).Start(cmd);
}

/// <summary>
/// 执行cmd命令
/// </summary>
private void runCMD_0(object obj)
{
string cmd = (string)obj;
string START = "[.RunCmd]";
if (cmd.StartsWith(START))
{
cmd = cmd.Substring(START.Length);  // 获取cmd信息
Cmd.Run(cmd, print);                // 执行cmd,输出执行结果到print
}
}

/// <summary>
/// 从socket接收数据
/// </summary>
private string Receive(Socket socket)
{
string data = "";

byte[] bytes = null;
int len = socket.Available;
if (len > 0)
{
bytes = new byte[len];
int receiveNumber = socket.Receive(bytes);
data = Encoding.UTF8.GetString(bytes, 0, receiveNumber);
}

return data;
}

/// <summary>
/// 停止服务
/// </summary>
public void stop()
{
started = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MessageServer
{
/// <summary>
/// 执行CMD命令,或以进程的形式打开应用程序(d:\*.exe)
/// </summary>
public class Cmd
{
/// <summary>
/// 以后台进程的形式执行应用程序(d:\*.exe)
/// </summary>
public static Process newProcess(String exe)
{
Process P = new Process();
P.StartInfo.CreateNoWindow = true;
P.StartInfo.FileName = exe;
P.StartInfo.UseShellExecute = false;
P.StartInfo.RedirectStandardError = true;
P.StartInfo.RedirectStandardInput = true;
P.StartInfo.RedirectStandardOutput = true;
//P.StartInfo.WorkingDirectory = @"C:\windows\system32";
P.Start();
return P;
}

/// <summary>
/// 执行CMD命令
/// </summary>
public static string Run(string cmd)
{
Process P = newProcess("cmd.exe");
P.StandardInput.WriteLine(cmd);
P.StandardInput.WriteLine("exit");
string outStr = P.StandardOutput.ReadToEnd();
P.Close();
return outStr;
}

/// <summary>
/// 定义委托接口处理函数,用于实时处理cmd输出信息
/// </summary>
public delegate void Callback(String line);

///// <summary>
///// 此函数用于实时显示cmd输出信息, Callback示例
///// </summary>
//private void Callback1(String line)
//{
//    textBox1.AppendText(line);
//    textBox1.AppendText(Environment.NewLine);
//    textBox1.ScrollToCaret();

//    richTextBox1.SelectionColor = Color.Green;
//    richTextBox1.AppendText(line);
//    richTextBox1.AppendText(Environment.NewLine);
//    richTextBox1.ScrollToCaret();
//}

/// <summary>
/// 执行CMD语句,实时获取cmd输出结果,输出到call函数中
/// </summary>
/// <param name="cmd">要执行的CMD命令</param>
public static string Run(string cmd, Server.Print call)
{
String CMD_FILE = "cmd.exe"; // 执行cmd命令

Process P = newProcess(CMD_FILE);
P.StandardInput.WriteLine(cmd);
P.StandardInput.WriteLine("exit");

string outStr = "";
string line = "";
string baseDir = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\');

try
{
for (int i = 0; i < 3; i++) P.StandardOutput.ReadLine();

while ((line = P.StandardOutput.ReadLine()) != null || ((line = P.StandardError.ReadToEnd()) != null && !line.Trim().Equals("")))
{
// cmd运行输出信息
if (!line.EndsWith(">exit") && !line.Equals(""))
{
if (line.StartsWith(baseDir + ">")) line = line.Replace(baseDir + ">", "cmd>\r\n"); // 识别的cmd命令行信息
line = ((line.Contains("[Fatal Error]") || line.Contains("ERROR:") || line.Contains("Exception")) ? "【E】 " : "") + line;
if (call != null) call(line);
outStr += line + "\r\n";
}
}
}
catch (Exception ex)
{
if (call != null) call(ex.Message);
//MessageBox.Show(ex.Message);
}

P.WaitForExit();
P.Close();

return outStr;
}

/// <summary>
/// 以进程的形式打开应用程序(d:\*.exe),并执行命令
/// </summary>
public static void RunProgram(string programName, string cmd)
{
Process P = newProcess(programName);
if (cmd.Length != 0)
{
P.StandardInput.WriteLine(cmd);
}
P.Close();
}

/// <summary>
/// 正常启动window应用程序(d:\*.exe)
/// </summary>
public static void Open(String exe)
{
System.Diagnostics.Process.Start(exe);
}

/// <summary>
/// 正常启动window应用程序(d:\*.exe),并传递初始命令参数
/// </summary>
public static void Open(String exe, String args)
{
System.Diagnostics.Process.Start(exe, args);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageServer
{
public partial class MessageServer : Form
{
Server server;

public MessageServer()
{
InitializeComponent();
}

// 启动服务
private void button_start_Click(object sender, EventArgs e)
{
if (server == null) server = new Server(SeverPrint, textBox_Ip.Text, textBox_Port.Text);
if (!server.started) server.start();
}

// 服务器端输出信息
private void SeverPrint(string info)
{
if (textBox_showing.IsDisposed) server.print = null;
else
{
if (textBox_showing.InvokeRequired)
{
Server.Print F = new Server.Print(SeverPrint);
this.Invoke(F, new object[] { info });
}
else
{
if (info != null)
{
textBox_showing.SelectionColor = Color.Green;
textBox_showing.AppendText(info);
textBox_showing.AppendText(Environment.NewLine);
textBox_showing.ScrollToCaret();
}
}
}
}

// 发送信息到客户端
private void button_send_Click(object sender, EventArgs e)
{
if (server != null) server.Send(textBox_send.Text, comboBox_clients.Text);
}

private void MessageServer_FormClosed(object sender, FormClosedEventArgs e)
{
//if (server != null) server.stop();
}

// 选择客户端时,更新客户端列表信息
private void comboBox_clients_DropDown(object sender, EventArgs e)
{
if (server != null)
{
List<string> clientList = server.clients.Keys.ToList<string>();
comboBox_clients.DataSource = clientList;
}
}

}
}
namespace MessageServer
{
partial class MessageServer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox_Port = new System.Windows.Forms.TextBox();
this.textBox_Ip = new System.Windows.Forms.TextBox();
this.button_start = new System.Windows.Forms.Button();
this.textBox_send = new System.Windows.Forms.TextBox();
this.button_send = new System.Windows.Forms.Button();
this.comboBox_clients = new System.Windows.Forms.ComboBox();
this.label_clientIp = new System.Windows.Forms.Label();
this.textBox_showing = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// textBox_Port
//
this.textBox_Port.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBox_Port.Location = new System.Drawing.Point(243, 3);
this.textBox_Port.Name = "textBox_Port";
this.textBox_Port.Size = new System.Drawing.Size(49, 21);
this.textBox_Port.TabIndex = 19;
this.textBox_Port.Text = "37280";
//
// textBox_Ip
//
this.textBox_Ip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_Ip.Location = new System.Drawing.Point(84, 3);
this.textBox_Ip.Name = "textBox_Ip";
this.textBox_Ip.Size = new System.Drawing.Size(153, 21);
this.textBox_Ip.TabIndex = 18;
this.textBox_Ip.Text = "127.0.0.1";
//
// button_start
//
this.button_start.Location = new System.Drawing.Point(3, 3);
this.button_start.Name = "button_start";
this.button_start.Size = new System.Drawing.Size(75, 23);
this.button_start.TabIndex = 17;
this.button_start.Text = "启动服务";
this.button_start.UseVisualStyleBackColor = true;
this.button_start.Click += new System.EventHandler(this.button_start_Click);
//
// textBox_send
//
this.textBox_send.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_send.Location = new System.Drawing.Point(3, 259);
this.textBox_send.Multiline = true;
this.textBox_send.Name = "textBox_send";
this.textBox_send.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox_send.Size = new System.Drawing.Size(289, 37);
this.textBox_send.TabIndex = 16;
//
// button_send
//
this.button_send.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button_send.Location = new System.Drawing.Point(217, 298);
this.button_send.Name = "button_send";
this.button_send.Size = new System.Drawing.Size(75, 23);
this.button_send.TabIndex = 15;
this.button_send.Text = "发送信息";
this.button_send.UseVisualStyleBackColor = true;
this.button_send.Click += new System.EventHandler(this.button_send_Click);
//
// comboBox_clients
//
this.comboBox_clients.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.comboBox_clients.FormattingEnabled = true;
this.comboBox_clients.Location = new System.Drawing.Point(73, 298);
this.comboBox_clients.Name = "comboBox_clients";
this.comboBox_clients.Size = new System.Drawing.Size(138, 20);
this.comboBox_clients.TabIndex = 20;
this.comboBox_clients.DropDown += new System.EventHandler(this.comboBox_clients_DropDown);
//
// label_clientIp
//
this.label_clientIp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label_clientIp.AutoSize = true;
this.label_clientIp.Location = new System.Drawing.Point(1, 301);
this.label_clientIp.Name = "label_clientIp";
this.label_clientIp.Size = new System.Drawing.Size(65, 12);
this.label_clientIp.TabIndex = 21;
this.label_clientIp.Text = "选择客户端";
//
// textBox_showing
//
this.textBox_showing.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_showing.Location = new System.Drawing.Point(3, 27);
this.textBox_showing.Name = "textBox_showing";
this.textBox_showing.Size = new System.Drawing.Size(289, 229);
this.textBox_showing.TabIndex = 22;
this.textBox_showing.Text = "";
//
// MessageServer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(295, 322);
this.Controls.Add(this.textBox_showing);
this.Controls.Add(this.label_clientIp);
this.Controls.Add(this.comboBox_clients);
this.Controls.Add(this.textBox_Port);
this.Controls.Add(this.textBox_Ip);
this.Controls.Add(this.button_start);
this.Controls.Add(this.textBox_send);
this.Controls.Add(this.button_send);
this.Name = "MessageServer";
this.ShowInTaskbar = false;
this.Text = "服务器";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MessageServer_FormClosed);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox textBox_Port;
private System.Windows.Forms.TextBox textBox_Ip;
private System.Windows.Forms.Button button_start;
private System.Windows.Forms.TextBox textBox_send;
private System.Windows.Forms.Button button_send;
private System.Windows.Forms.ComboBox comboBox_clients;
private System.Windows.Forms.Label label_clientIp;
private System.Windows.Forms.RichTextBox textBox_showing;

}
}


客户端源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageClient
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MessageClient());
}
}
}
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;

namespace MessageClient
{
class Client
{
public string ipString = "127.0.0.1";   // 服务器端ip
public int port = 37280;                // 服务器端口
public Socket socket;
public Print print;                     // 运行时的信息输出方法
public bool connected = false;          // 标识当前是否连接到服务器
public string localIpPort = "";         // 记录本地ip端口信息

public Client(Print print = null, string ipString = null, int port = -1)
{
this.print = print;
if (ipString != null) this.ipString = ipString;
if (port >= 0) this.port = port;
}

public Client(Print print = null, string ipString = null, string port = "-1")
{
this.print = print;
if (ipString != null) this.ipString = ipString;

int port_int = Int32.Parse(port);
if (port_int >= 0) this.port = port_int;
}

/// <summary>
/// Print用于输出Server的输出信息
/// </summary>
public delegate void Print(string info);

/// <summary>
/// 启动客户端,连接至服务器
/// </summary>
public void start()
{
//设定服务器IP地址
IPAddress ip = IPAddress.Parse(ipString);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try
{
socket.Connect(new IPEndPoint(ip, port));   // 连接服务器
if (print != null) print("连接服务器【" + socket.RemoteEndPoint.ToString() + "】完成"); // 连接成功
localIpPort = socket.LocalEndPoint.ToString();
connected = true;

Thread thread = new Thread(receiveData);
thread.Start(socket);      // 在新的线程中接收服务器信息

}
catch (Exception ex)
{
if (print != null) print("连接服务器失败 " + ex.ToString()); // 连接失败
connected = false;
}
}

/// <summary>
/// 结束客户端
/// </summary>
public void stop()
{
connected = false;
}

/// <summary>
/// 发送信息
/// </summary>
public void Send(string info)
{
try
{
Send(socket, info);
}
catch (Exception ex)
{
if (print != null) print("服务器端已断开,【" + socket.RemoteEndPoint.ToString() + "】");
}
}

/// <summary>
/// 通过socket发送数据data
/// </summary>
private void Send(Socket socket, string data)
{
if (socket != null && data != null && !data.Equals(""))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);   // 将data转化为byte数组
socket.Send(bytes);                            //
}
}

/// <summary>
/// 接收通过socket发送过来的数据
/// </summary>
private void receiveData(object socket)
{
Socket ortherSocket = (Socket)socket;

while (true)
{
try
{
String data = Receive(ortherSocket);       // 接收客户端发送的信息
if (!data.Equals(""))
{
//if (print != null) print("服务器" + ortherSocket.RemoteEndPoint.ToString() + "信息:\r\n" + data);
if (print != null) print(data);
if (data.Equals("[.Shutdown]")) System.Environment.Exit(0);
}
}
catch (Exception ex)
{
if (print != null) print("连接已自动断开," + ex.Message);
ortherSocket.Shutdown(SocketShutdown.Both);
ortherSocket.Close();
connected = false;
break;
}

if (!connected) break;
Thread.Sleep(200);      // 延时0.2后处理接收到的信息
}
}

/// <summary>
/// 从socket接收数据
/// </summary>
private string Receive(Socket socket)
{
string data = "";

byte[] bytes = null;
int len = socket.Available;
if (len > 0)
{
bytes = new byte[len];
int receiveNumber = socket.Receive(bytes);
data = Encoding.UTF8.GetString(bytes, 0, receiveNumber);
}

return data;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageClient
{
public partial class MessageClient : Form
{
Client client;    // 客户端实例

public MessageClient()
{
InitializeComponent();
}

// 连接服务器
private void button_connect_Click(object sender, EventArgs e)
{
if (client == null) client = new Client(ClientPrint, textBox_Ip.Text, textBox_Port.Text);
if (!client.connected) client.start();
if (client != null) this.Text = "客户端 " + client.localIpPort;
}

// 客户端输出信息
private void ClientPrint(string info)
{
if (textBox_showing.InvokeRequired)
{
Client.Print F = new Client.Print(ClientPrint);
this.Invoke(F, new object[] { info });
}
else
{
if (info != null)
{
textBox_showing.SelectionColor = Color.Green;
textBox_showing.AppendText(info);
textBox_showing.AppendText(Environment.NewLine);
textBox_showing.ScrollToCaret();
}
}
}

// 发送信息到服务器
private void button_send_Click(object sender, EventArgs e)
{
if (client != null && client.connected)
{
string info = textBox_send.Text;
if (checkBox_isCmd.Checked && !info.StartsWith("[.RunCmd]"))    // 添加cmd串头信息
info = "[.RunCmd]" + info;

client.Send(info);
}
}

// 关闭界面停止服务运行
private void MessageClient_FormClosed(object sender, FormClosedEventArgs e)
{
if (client != null && client.connected)
client.stop();
}

private void linkLabel_closeServer_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (client != null && client.connected)
client.Send("[.Shutdown]");
}

private void linkLabel_Echo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (client != null && client.connected)
client.Send("[.Echo]");
}
}
}
namespace MessageClient
{
partial class MessageClient
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button_connect = new System.Windows.Forms.Button();
this.textBox_send = new System.Windows.Forms.TextBox();
this.button_send = new System.Windows.Forms.Button();
this.textBox_Ip = new System.Windows.Forms.TextBox();
this.textBox_Port = new System.Windows.Forms.TextBox();
this.linkLabel_closeServer = new System.Windows.Forms.LinkLabel();
this.checkBox_isCmd = new System.Windows.Forms.CheckBox();
this.textBox_showing = new System.Windows.Forms.RichTextBox();
this.linkLabel_Echo = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// button_connect
//
this.button_connect.Location = new System.Drawing.Point(3, 7);
this.button_connect.Name = "button_connect";
this.button_connect.Size = new System.Drawing.Size(75, 23);
this.button_connect.TabIndex = 11;
this.button_connect.Text = "连接服务器";
this.button_connect.UseVisualStyleBackColor = true;
this.button_connect.Click += new System.EventHandler(this.button_connect_Click);
//
// textBox_send
//
this.textBox_send.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_send.Location = new System.Drawing.Point(3, 263);
this.textBox_send.Multiline = true;
this.textBox_send.Name = "textBox_send";
this.textBox_send.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox_send.Size = new System.Drawing.Size(289, 37);
this.textBox_send.TabIndex = 10;
//
// button_send
//
this.button_send.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button_send.Location = new System.Drawing.Point(217, 302);
this.button_send.Name = "button_send";
this.button_send.Size = new System.Drawing.Size(75, 23);
this.button_send.TabIndex = 9;
this.button_send.Text = "发送信息";
this.button_send.UseVisualStyleBackColor = true;
this.button_send.Click += new System.EventHandler(this.button_send_Click);
//
// textBox_Ip
//
this.textBox_Ip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_Ip.Location = new System.Drawing.Point(84, 7);
this.textBox_Ip.Name = "textBox_Ip";
this.textBox_Ip.Size = new System.Drawing.Size(153, 21);
this.textBox_Ip.TabIndex = 12;
this.textBox_Ip.Text = "127.0.0.1";
//
// textBox_Port
//
this.textBox_Port.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBox_Port.Location = new System.Drawing.Point(243, 7);
this.textBox_Port.Name = "textBox_Port";
this.textBox_Port.Size = new System.Drawing.Size(49, 21);
this.textBox_Port.TabIndex = 13;
this.textBox_Port.Text = "37280";
//
// linkLabel_closeServer
//
this.linkLabel_closeServer.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.linkLabel_closeServer.AutoSize = true;
this.linkLabel_closeServer.Location = new System.Drawing.Point(1, 306);
this.linkLabel_closeServer.Name = "linkLabel_closeServer";
this.linkLabel_closeServer.Size = new System.Drawing.Size(65, 12);
this.linkLabel_closeServer.TabIndex = 14;
this.linkLabel_closeServer.TabStop = true;
this.linkLabel_closeServer.Text = "关闭服务器";
this.linkLabel_closeServer.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel_closeServer_LinkClicked);
//
// checkBox_isCmd
//
this.checkBox_isCmd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.checkBox_isCmd.AutoSize = true;
this.checkBox_isCmd.Location = new System.Drawing.Point(109, 306);
this.checkBox_isCmd.Name = "checkBox_isCmd";
this.checkBox_isCmd.Size = new System.Drawing.Size(102, 16);
this.checkBox_isCmd.TabIndex = 15;
this.checkBox_isCmd.Text = "以cmd格式发送";
this.checkBox_isCmd.UseVisualStyleBackColor = true;
//
// textBox_showing
//
this.textBox_showing.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox_showing.Location = new System.Drawing.Point(4, 31);
this.textBox_showing.Name = "textBox_showing";
this.textBox_showing.Size = new System.Drawing.Size(287, 230);
this.textBox_showing.TabIndex = 16;
this.textBox_showing.Text = "";
//
// linkLabel_Echo
//
this.linkLabel_Echo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.linkLabel_Echo.AutoSize = true;
this.linkLabel_Echo.Location = new System.Drawing.Point(2, 248);
this.linkLabel_Echo.Name = "linkLabel_Echo";
this.linkLabel_Echo.Size = new System.Drawing.Size(65, 12);
this.linkLabel_Echo.TabIndex = 17;
this.linkLabel_Echo.TabStop = true;
this.linkLabel_Echo.Text = "服务器输出";
this.linkLabel_Echo.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel_Echo_LinkClicked);
//
// MessageClient
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(295, 327);
this.Controls.Add(this.linkLabel_Echo);
this.Controls.Add(this.textBox_showing);
this.Controls.Add(this.checkBox_isCmd);
this.Controls.Add(this.linkLabel_closeServer);
this.Controls.Add(this.textBox_Port);
this.Controls.Add(this.textBox_Ip);
this.Controls.Add(this.button_connect);
this.Controls.Add(this.textBox_send);
this.Controls.Add(this.button_send);
this.Name = "MessageClient";
this.Text = "客户端";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MessageClient_FormClosed);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button_connect;
private System.Windows.Forms.TextBox textBox_send;
private System.Windows.Forms.Button button_send;
private System.Windows.Forms.TextBox textBox_Ip;
private System.Windows.Forms.TextBox textBox_Port;
private System.Windows.Forms.LinkLabel linkLabel_closeServer;
private System.Windows.Forms.CheckBox checkBox_isCmd;
private System.Windows.Forms.RichTextBox textBox_showing;
private System.Windows.Forms.LinkLabel linkLabel_Echo;
}
}


示例项目源码下载

开源地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息