关于C# 的相互通讯提问
http://community.csdn.net/Expert/topic/4022/4022940.xml?temp=.5879633
服务端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;
![]()
![]()
class UdpSocketServer
{
public static void Main()
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9060);
ArrayList list = new ArrayList(10);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
![]()
server.Bind(localEP);
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
int recv;
byte[] data;
![]()
while(true)
{
data = new byte[1024];
recv =0;
recv = server.ReceiveFrom(data, ref remoteEP);
Console.WriteLine("Waiting for a client
");
String message = Encoding.ASCII.GetString(data, 0, recv);
switch (message)
{
case "join" :
Console.WriteLine("Received from {0}: ", remoteEP.ToString());
list.Add(remoteEP);
break;
case "quit" :
list.Remove(remoteEP);
break;
default :
int i =0;
String Data = Encoding.ASCII.GetString(data);
while (i < list.Count)
{
server.SendTo(Encoding.ASCII.GetBytes(Data), recv, SocketFlags.None, (EndPoint) list[i]);
++i;
}
break;
}
}
}
}
客户端
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
![]()
namespace chatClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button btnJoin;
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtOut;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.GroupBox groupBox2;
/// <summary>
/// Required designer variable.
/// </summary>
///
Socket client;
EndPoint remoteEP;
byte[] data;
string input;
int recv;
private System.Windows.Forms.TextBox txtIn;
![]()
private System.ComponentModel.Container components = null;
![]()
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
![]()
//
// TODO: Add any constructor code after InitializeComponent call
//
}
![]()
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.btnJoin = new System.Windows.Forms.Button();
this.btnQuit = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtOut = new System.Windows.Forms.TextBox();
this.btnSend = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtIn = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 24);
this.label1.TabIndex = 0;
this.label1.Text = "ServerIP : ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(72, 8);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(144, 20);
this.txtIP.TabIndex = 1;
this.txtIP.Text = " ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(240, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 24);
this.label2.TabIndex = 2;
this.label2.Text = "Port NO :";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(312, 8);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(72, 20);
this.txtPort.TabIndex = 3;
this.txtPort.Text = " ";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Name :";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(72, 40);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(144, 20);
this.txtName.TabIndex = 5;
this.txtName.Text = " ";
//
// btnJoin
//
this.btnJoin.Location = new System.Drawing.Point(232, 40);
this.btnJoin.Name = "btnJoin";
this.btnJoin.Size = new System.Drawing.Size(72, 24);
this.btnJoin.TabIndex = 6;
this.btnJoin.Text = "Join Chat";
this.btnJoin.Click += new System.EventHandler(this.btnJoin_Click);
//
// btnQuit
//
this.btnQuit.Location = new System.Drawing.Point(312, 40);
this.btnQuit.Name = "btnQuit";
this.btnQuit.Size = new System.Drawing.Size(72, 24);
this.btnQuit.TabIndex = 7;
this.btnQuit.Text = "Quit Chat";
this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnSend);
this.groupBox1.Controls.Add(this.txtOut);
this.groupBox1.Location = new System.Drawing.Point(8, 72);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(376, 128);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Out-going Message";
//
// txtOut
//
this.txtOut.Location = new System.Drawing.Point(8, 16);
this.txtOut.Multiline = true;
this.txtOut.Name = "txtOut";
this.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtOut.Size = new System.Drawing.Size(288, 104);
this.txtOut.TabIndex = 0;
this.txtOut.Text = " ";
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(304, 96);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(64, 24);
this.btnSend.TabIndex = 1;
this.btnSend.Text = "Send";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtIn);
this.groupBox2.Location = new System.Drawing.Point(8, 208);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(376, 160);
this.groupBox2.TabIndex = 9;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "In-comming Messages";
//
// txtIn
//
this.txtIn.Location = new System.Drawing.Point(8, 16);
this.txtIn.Multiline = true;
this.txtIn.Name = "txtIn";
this.txtIn.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtIn.Size = new System.Drawing.Size(360, 136);
this.txtIn.TabIndex = 0;
this.txtIn.Text = " ";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 374);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnQuit);
this.Controls.Add(this.btnJoin);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
![]()
}
#endregion
![]()
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
![]()
private void btnJoin_Click(object sender, System.EventArgs e)
{
client = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
remoteEP = new IPEndPoint(IPAddress.Parse(txtIP.Text.Trim()), int.Parse(txtPort.Text.Trim()));
client.SendTo(Encoding.ASCII.GetBytes("join"), remoteEP);
data = new byte[1024];
Thread thread1 = new Thread(new ThreadStart(Rec));
thread1.Start();
![]()
}
![]()
private void btnQuit_Click(object sender, System.EventArgs e)
{
client.SendTo(Encoding.ASCII.GetBytes("quit"), remoteEP);
}
![]()
private void btnSend_Click(object sender, System.EventArgs e)
{
Thread thread = new Thread (new ThreadStart(Send));
thread.Start();
}
![]()
public void Send()
{
client.SendTo(Encoding.ASCII.GetBytes(txtName.Text.Trim() + " :" + txtOut.Text.Trim()), remoteEP);
}
![]()
public void Rec() {
while(true)
{
recv = client.ReceiveFrom(data, ref remoteEP);
txtIn.Text += Encoding.ASCII.GetString(data, 0, recv) + "\r\n" ;
}
}
}
![]()
}
http://community.csdn.net/Expert/topic/4022/4022940.xml?temp=.5879633
服务端
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;

class UdpSocketServer
{
public static void Main()
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9060);
ArrayList list = new ArrayList(10);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
server.Bind(localEP);
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
int recv;
byte[] data;
while(true)
{
data = new byte[1024];
recv =0;
recv = server.ReceiveFrom(data, ref remoteEP);
Console.WriteLine("Waiting for a client
");
String message = Encoding.ASCII.GetString(data, 0, recv);
switch (message)
{
case "join" :
Console.WriteLine("Received from {0}: ", remoteEP.ToString());
list.Add(remoteEP);
break;
case "quit" :
list.Remove(remoteEP);
break;
default :
int i =0;
String Data = Encoding.ASCII.GetString(data);
while (i < list.Count)
{
server.SendTo(Encoding.ASCII.GetBytes(Data), recv, SocketFlags.None, (EndPoint) list[i]);
++i;
}
break;
}
}
}
}客户端
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace chatClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button btnJoin;
private System.Windows.Forms.Button btnQuit;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtOut;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.GroupBox groupBox2;
/// <summary>
/// Required designer variable.
/// </summary>
///
Socket client;
EndPoint remoteEP;
byte[] data;
string input;
int recv;
private System.Windows.Forms.TextBox txtIn;
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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()
{
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.btnJoin = new System.Windows.Forms.Button();
this.btnQuit = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtOut = new System.Windows.Forms.TextBox();
this.btnSend = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtIn = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 24);
this.label1.TabIndex = 0;
this.label1.Text = "ServerIP : ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtIP
//
this.txtIP.Location = new System.Drawing.Point(72, 8);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(144, 20);
this.txtIP.TabIndex = 1;
this.txtIP.Text = " ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(240, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 24);
this.label2.TabIndex = 2;
this.label2.Text = "Port NO :";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(312, 8);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(72, 20);
this.txtPort.TabIndex = 3;
this.txtPort.Text = " ";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Name :";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(72, 40);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(144, 20);
this.txtName.TabIndex = 5;
this.txtName.Text = " ";
//
// btnJoin
//
this.btnJoin.Location = new System.Drawing.Point(232, 40);
this.btnJoin.Name = "btnJoin";
this.btnJoin.Size = new System.Drawing.Size(72, 24);
this.btnJoin.TabIndex = 6;
this.btnJoin.Text = "Join Chat";
this.btnJoin.Click += new System.EventHandler(this.btnJoin_Click);
//
// btnQuit
//
this.btnQuit.Location = new System.Drawing.Point(312, 40);
this.btnQuit.Name = "btnQuit";
this.btnQuit.Size = new System.Drawing.Size(72, 24);
this.btnQuit.TabIndex = 7;
this.btnQuit.Text = "Quit Chat";
this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnSend);
this.groupBox1.Controls.Add(this.txtOut);
this.groupBox1.Location = new System.Drawing.Point(8, 72);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(376, 128);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Out-going Message";
//
// txtOut
//
this.txtOut.Location = new System.Drawing.Point(8, 16);
this.txtOut.Multiline = true;
this.txtOut.Name = "txtOut";
this.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtOut.Size = new System.Drawing.Size(288, 104);
this.txtOut.TabIndex = 0;
this.txtOut.Text = " ";
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(304, 96);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(64, 24);
this.btnSend.TabIndex = 1;
this.btnSend.Text = "Send";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtIn);
this.groupBox2.Location = new System.Drawing.Point(8, 208);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(376, 160);
this.groupBox2.TabIndex = 9;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "In-comming Messages";
//
// txtIn
//
this.txtIn.Location = new System.Drawing.Point(8, 16);
this.txtIn.Multiline = true;
this.txtIn.Name = "txtIn";
this.txtIn.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtIn.Size = new System.Drawing.Size(360, 136);
this.txtIn.TabIndex = 0;
this.txtIn.Text = " ";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 374);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnQuit);
this.Controls.Add(this.btnJoin);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtIP);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnJoin_Click(object sender, System.EventArgs e)
{
client = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
remoteEP = new IPEndPoint(IPAddress.Parse(txtIP.Text.Trim()), int.Parse(txtPort.Text.Trim()));
client.SendTo(Encoding.ASCII.GetBytes("join"), remoteEP);
data = new byte[1024];
Thread thread1 = new Thread(new ThreadStart(Rec));
thread1.Start();
}
private void btnQuit_Click(object sender, System.EventArgs e)
{
client.SendTo(Encoding.ASCII.GetBytes("quit"), remoteEP);
}
private void btnSend_Click(object sender, System.EventArgs e)
{
Thread thread = new Thread (new ThreadStart(Send));
thread.Start();
}
public void Send()
{
client.SendTo(Encoding.ASCII.GetBytes(txtName.Text.Trim() + " :" + txtOut.Text.Trim()), remoteEP);
}
public void Rec() {
while(true)
{
recv = client.ReceiveFrom(data, ref remoteEP);
txtIn.Text += Encoding.ASCII.GetString(data, 0, recv) + "\r\n" ;
}
}
}
}

浙公网安备 33010602011771号