目录
一 原型
二 源码
一 原型
二 源码
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TCP网络服务端通讯
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpListener listener = null;
TcpClient handler = null;
NetworkStream stream = null;
bool isrun = false;
private void openServer_Click(object sender, EventArgs e)
{
if (IP.Text.Trim().Length < 9 && Port.Text.Trim().Length == 0)
{
MessageBox.Show("IP和端口无效");
return;
}
listener = new TcpListener(IPAddress.Parse(IP.Text), int.Parse(Port.Text));
listener.Start();
isrun = true;
}
private void send_Click(object sender, EventArgs e)
{
if (stream != null)
{
byte[] buffer = Encoding.UTF8.GetBytes(log.Text);
stream.Write(buffer, 0, buffer.Length);
}
}
private void Form1_Load(object sender, EventArgs e)
{
IP.Text = "127.0.0.1";
Port.Text = "9800";
try
{
Task.Run(() =>
{
while (true)
{
if (isrun && listener != null)
{
handler = listener.AcceptTcpClient();
stream = handler.GetStream();
byte[] buffer = new byte[1024];
if (stream != null)
{
stream.Read(buffer, 0, buffer.Length);
this.BeginInvoke(new Action(() =>
{
log.Text = Encoding.UTF8.GetString(buffer);
}));
}
}
Thread.Sleep(50);
}
});
}
catch (Exception)
{
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isrun = false;
if (listener != null)
{
listener.Stop();
}
}
}
}
设计器自动生成代码:
namespace TCP网络服务端通讯
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
label1 = new Label();
label2 = new Label();
IP = new TextBox();
Port = new TextBox();
openServer = new Button();
log = new RichTextBox();
send = new Button();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(42, 52);
label1.Name = "label1";
label1.Size = new Size(22, 20);
label1.TabIndex = 0;
label1.Text = "IP";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(42, 102);
label2.Name = "label2";
label2.Size = new Size(39, 20);
label2.TabIndex = 1;
label2.Text = "端口";
//
// IP
//
IP.Location = new Point(111, 52);
IP.Name = "IP";
IP.Size = new Size(291, 27);
IP.TabIndex = 2;
//
// Port
//
Port.Location = new Point(111, 102);
Port.Name = "Port";
Port.Size = new Size(291, 27);
Port.TabIndex = 3;
//
// openServer
//
openServer.Location = new Point(42, 173);
openServer.Name = "openServer";
openServer.Size = new Size(360, 29);
openServer.TabIndex = 4;
openServer.Text = "打开服务器";
openServer.UseVisualStyleBackColor = true;
openServer.Click += openServer_Click;
//
// log
//
log.Location = new Point(42, 224);
log.Name = "log";
log.Size = new Size(360, 238);
log.TabIndex = 5;
log.Text = "";
//
// send
//
send.Location = new Point(42, 468);
send.Name = "send";
send.Size = new Size(360, 29);
send.TabIndex = 6;
send.Text = "发送";
send.UseVisualStyleBackColor = true;
send.Click += send_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(414, 516);
Controls.Add(send);
Controls.Add(log);
Controls.Add(openServer);
Controls.Add(Port);
Controls.Add(IP);
Controls.Add(label2);
Controls.Add(label1);
MaximizeBox = false;
Name = "Form1";
Text = "TCP服务端";
FormClosing += Form1_FormClosing;
Load += Form1_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label label1;
private Label label2;
private TextBox IP;
private TextBox Port;
private Button openServer;
private RichTextBox log;
private Button send;
}
}