111,129
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace SendMessage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread trsend = new Thread(new ThreadStart(sendmsg));
trsend.Start();
trsend.Abort();
}
private void sendmsg()
{
Socket sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp );
sendsocket.Connect("127.0.0.1", 8000);
byte[] bysend = Encoding.UTF8 .GetBytes(textBox1.Text);
sendsocket.Send(bysend);
sendsocket.Shutdown(SocketShutdown.Both);
sendsocket.Close();
}
private void receivemsg()
{
Socket receivesocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp );
IPEndPoint receiveip = new IPEndPoint(IPAddress.Parse ("127.0.0.1"), 8000);
receivesocket.Bind(receiveip);
receivesocket.Listen(20);
try
{
while (true)
{
Socket sockettmp = receivesocket.Accept();
byte[] buffer = new byte[sockettmp.ReceiveBufferSize];
if (receivesocket.Receive(buffer) > 0)
{
richTextBox1.Text += Encoding.UTF8.GetString(buffer) + Environment.NewLine;
}
else
{
Thread.Sleep(1000);
}
}
}
catch(Exception ex)
{
MessageBox.Show (ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
Thread trreceive = new Thread(new ThreadStart(receivemsg));
trreceive.Start();
}
}
}
namespace SendMessage
{
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.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 231);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(292, 190);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 204);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 3;
//
// button2
//
this.button2.Location = new System.Drawing.Point(150, 231);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 4;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
}
}
trsend.Start();
trsend.Abort();
trsend.Start();
trsend.Abort();