在发送地址里填写IPV6地址,而不是IPV4地址。
先布置窗口元素:
namespace Demo
{
partial class Form1
{
/// <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.components = new System.ComponentModel.Container();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.rtbContent = new System.Windows.Forms.RichTextBox();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.rtbSend = new System.Windows.Forms.RichTextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btn_Clear = new System.Windows.Forms.Button();
this.btn_Send = new System.Windows.Forms.Button();
this.btn_Close = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.splitContainer1.Location = new System.Drawing.Point(3, 4);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.rtbContent);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(484, 400);
this.splitContainer1.SplitterDistance = 264;
this.splitContainer1.SplitterWidth = 3;
this.splitContainer1.TabIndex = 0;
//
// rtbContent
//
this.rtbContent.BackColor = System.Drawing.Color.White;
this.rtbContent.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.rtbContent.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbContent.Location = new System.Drawing.Point(0, 0);
this.rtbContent.Name = "rtbContent";
this.rtbContent.ReadOnly = true;
this.rtbContent.Size = new System.Drawing.Size(482, 262);
this.rtbContent.TabIndex = 0;
this.rtbContent.Text = "";
//
// splitContainer2
//
this.splitContainer2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.rtbSend);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.txtName);
this.splitContainer2.Panel2.Controls.Add(this.label2);
this.splitContainer2.Panel2.Controls.Add(this.txtIP);
this.splitContainer2.Panel2.Controls.Add(this.label1);
this.splitContainer2.Panel2.Controls.Add(this.btn_Clear);
this.splitContainer2.Panel2.Controls.Add(this.btn_Send);
this.splitContainer2.Panel2.Controls.Add(this.btn_Close);
this.splitContainer2.Size = new System.Drawing.Size(484, 133);
this.splitContainer2.SplitterDistance = 101;
this.splitContainer2.SplitterWidth = 1;
this.splitContainer2.TabIndex = 0;
//
// rtbSend
//
this.rtbSend.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.rtbSend.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbSend.Location = new System.Drawing.Point(0, 0);
this.rtbSend.Name = "rtbSend";
this.rtbSend.Size = new System.Drawing.Size(482, 99);
this.rtbSend.TabIndex = 0;
this.rtbSend.Text = "";
this.rtbSend.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.rtbSend_KeyPress);
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(240, 5);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(100, 21);
this.txtName.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(200, 11);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 4;
this.label2.Text = "昵称:";
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(67, 6);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(127, 21);
this.txtIP.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 10);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 1;
this.label1.Text = "对方主机:";
//
// btn_Clear
//
this.btn_Clear.Location = new System.Drawing.Point(346, 3);
this.btn_Clear.Name = "btn_Clear";
this.btn_Clear.Size = new System.Drawing.Size(39, 23);
this.btn_Clear.TabIndex = 3;
this.btn_Clear.Text = "清屏";
this.btn_Clear.UseVisualStyleBackColor = true;
this.btn_Clear.Click += new System.EventHandler(this.btn_Clear_Click);
//
// btn_Send
//
this.btn_Send.Location = new System.Drawing.Point(391, 3);
this.btn_Send.Name = "btn_Send";
this.btn_Send.Size = new System.Drawing.Size(38, 23);
this.btn_Send.TabIndex = 2;
this.btn_Send.Text = "发送";
this.btn_Send.UseVisualStyleBackColor = true;
this.btn_Send.Click += new System.EventHandler(this.btn_Send_Click);
//
// btn_Close
//
this.btn_Close.Location = new System.Drawing.Point(435, 3);
this.btn_Close.Name = "btn_Close";
this.btn_Close.Size = new System.Drawing.Size(39, 23);
this.btn_Close.TabIndex = 4;
this.btn_Close.Text = "关闭";
this.btn_Close.UseVisualStyleBackColor = true;
this.btn_Close.Click += new System.EventHandler(this.btn_Close_Click);
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(223)))), ((int)(((byte)(233)))), ((int)(((byte)(243)))));
this.ClientSize = new System.Drawing.Size(490, 407);
this.Controls.Add(this.splitContainer1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "点对点聊天";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.frmMain_FormClosed);
this.Load += new System.EventHandler(this.frmMain_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.RichTextBox rtbContent;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.RichTextBox rtbSend;
private System.Windows.Forms.Button btn_Clear;
private System.Windows.Forms.Button btn_Send;
private System.Windows.Forms.Button btn_Close;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
}
}
再上代码,代码简单,不要过多解释。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread td;
private TcpListener tcpListener;
private static string message = "";
private void frmMain_Load(object sender, EventArgs e)
{
td = new Thread(new ThreadStart(this.StartListen));
td.Start();
timer1.Start();
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
txtIP.Text = ip[0].ToString();
}
private void StartListen()
{
message = "";
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
tcpListener = new TcpListener(ip[0],888);
tcpListener.Start();
while (true)
{
TcpClient tclient = tcpListener.AcceptTcpClient(); //接受连接请求
NetworkStream nstream = tclient.GetStream(); //获取数据流
byte[] mbyte = new byte[1024]; //建立缓存
int i = nstream.Read(mbyte, 0, mbyte.Length); //将数据流写入缓存
message = Encoding.Default.GetString(mbyte, 0, i);
}
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.tcpListener != null)
{
tcpListener.Stop();
}
if (td != null)
{
if (td.ThreadState == ThreadState.Running)
{
td.Abort();
}
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
rtbContent.Clear();
}
private void rtbSend_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
btn_Send_Click(sender, e);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (message != "")
{
rtbContent.AppendText(message);
rtbContent.ScrollToCaret();
message = "";
}
}
private void btn_Close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btn_Send_Click(object sender, EventArgs e)
{
try
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
string strmsg = " " + txtName.Text + "(" + ip[0].ToString() + ") " + DateTime.Now.ToLongTimeString() + "\n" + " " + this.rtbSend.Text + "\n";
TcpClient client = new TcpClient(txtIP.Text, 888);
NetworkStream netstream = client.GetStream();
StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);
wstream.Write(strmsg);
wstream.Flush();
wstream.Close();
client.Close();
rtbContent.AppendText(strmsg);
rtbContent.ScrollToCaret();
rtbSend.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}