当前位置:首页 > .NET中Socket编程的简单示例

.NET中Socket编程的简单示例

点击次数:1140  更新日期:2010-12-26
\n

以下示例程序实现简单的Socket通信,可以开多个客户端。本机测试通过,未做联机测试。


\n

Server:


\n

using System.Net;


\n

using System.Net.Sockets;


\n

using System.Threading;


\n

using System.Collections;

\n

namespace MySocketServer1


\n

{


\n

public partial class Form1 : Form


\n

{


\n

private IPAddress serverIP = IPAddress.Parse(“127.0.0.1″);//以本机作测试


\n

private IPEndPoint serverFullAddr;//完整终端地址


\n

private Socket sock;


\n

private System.Timers.Timer myTimer;


\n

private ArrayList alSock;//当建立了多个连接时用于保存连接

\n

public Form1()


\n

{


\n

InitializeComponent();


\n

}

\n

private void btStart_Click(object sender, EventArgs e)


\n

{


\n

serverFullAddr = new IPEndPoint(serverIP, 1000);//取端口号1000


\n

//构造Socket对象,套接字类型为“流套接字”,指定五元组中的协议元


\n

sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,


\n

ProtocolType.Tcp);


\n

//指定五元组中的本地二元,即本地主机地址和端口号


\n

sock.Bind(serverFullAddr);


\n

//监听是否有连接传入,指定挂起的连接队列的最大值为20


\n

sock.Listen(20);

\n

alSock = new ArrayList();

\n

//构造定时器,时间间隙为1秒,即每隔一秒执行一次accept()方法,以获取连接请求队列中//第一个挂起的连接请求


\n

myTimer =new System.Timers.Timer(1000);


\n

myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed);


\n

myTimer.Enabled = true;


\n

}

\n

private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)


\n

{


\n

myTimer.Enabled = false;


\n

//执行accept(),当挂起队列为空时将阻塞本线程,同时由于上一语句,定时器将停止,直//至有连接传入


\n

Socket acceptSock = sock.Accept();


\n

//将accept()产生的Socket对象存入ArrayList


\n

alSock.Add(acceptSock);


\n

// 构造Threading.Timer对象,这将导致程序另启线程。线程将执行回调函数,该委托限制//函数参数须为object型。Threading.Timer构造器的第二个参数即传入回调函数的参数;第//三个参数指定调用回调函数之前的延时,取0则立即启动;最后一个参数指定调用回调函数//的时间间隔,取0则只执行一次。


\n

System.Threading.Timer ti = new System.Threading.Timer(new


\n

TimerCallback(ReceiveMsg), acceptSock, 0, 0);


\n

myTimer.Enabled = true;


\n

}

\n

private void ReceiveMsg(object obj)


\n

{


\n

Socket acceptSock = (Socket)obj;


\n

try


\n

{


\n

while (true)


\n

{


\n

byte[] byteArray = new byte[100];


\n

acceptSock.Receive(byteArray);//接收数据


\n

//将字节数组转成字符串


\n

string strRec = System.Text.Encoding.UTF8.GetString(byteArray);


\n

if (this.rtbReceive.InvokeRequired)


\n

{


\n

this.rtbReceive.Invoke(new EventHandler(this.ChangeRickTextBox), new


\n

object[] { strRec, EventArgs.Empty });


\n

}


\n

}


\n

}


\n

catch(Exception ex)


\n

{


\n

acceptSock.Close();


\n

MessageBox.Show(“S:Receive Message Error”+ex.Message);


\n

}


\n

}

\n

private void ChangeRickTextBox(object obj,EventArgs e)


\n

{


\n

string s = System.Convert.ToString(obj);


\n

this.rtbReceive.AppendText(s + Environment.NewLine);


\n

}

\n

private void btSend_Click(object sender, EventArgs e)


\n

{


\n

Socket sc=null;


\n

byte[] byteSend =


\n

System.Text.Encoding.UTF8.GetBytes(this.tbSend.Text.ToCharArray());


\n

try


\n

{


\n

//同时存在若干个客户端连接时,在textBox1中输入要发送的是哪个连接


\n

int index = int.Parse(this.textBox1.Text.Trim());


\n

sc = (Socket)alSock[index - 1];


\n

//发送数据


\n

sc.Send(byteSend);


\n

}


\n

catch(Exception ex)


\n

{


\n

if(sc != null)


\n

{


\n

sc.Close();


\n

}


\n

MessageBox.Show(“S:Send Message Error”+ex.Message);


\n

}


\n

}

\n

private void btClose_Click(object sender, EventArgs e)


\n

{


\n

try


\n

{


\n

Application.Exit();


\n

}


\n

catch (Exception ex)


\n

{


\n

MessageBox.Show(“S:Close Socket Error” + ex.Message);


\n

}


\n

}


\n

}


\n

}


\n

== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==


\n

Client:


\n

using System.Net;


\n

using System.Net.Sockets;


\n

using System.Threading;

\n

namespace MySocketClient1


\n

{


\n

public partial class Form1 : Form


\n

{


\n

private IPAddress serverIP = IPAddress.Parse(“127.0.0.1″);


\n

private IPEndPoint serverFullAddr;


\n

private Socket sock;

\n

public Form1()


\n

{


\n

InitializeComponent();


\n

}

\n

private void btConnect_Click(object sender, EventArgs e)


\n

{


\n

try


\n

{


\n

serverFullAddr = new IPEndPoint(serverIP, 1000);


\n

sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,


\n

ProtocolType.Tcp);


\n

sock.Connect(serverFullAddr);//建立与远程主机的连接

\n

//启动新线程用于接收数据


\n

Thread t = new Thread(new ThreadStart(ReceiveMsg));


\n

t.Name = “Receive Message”;


\n

//一个线程或者是后台线程或者是前台线程。后台线程与前台线程类似,区别是后台线//程不会防止进程终止。一旦属于某一进程的所有前台线程都终止,公共语言运行库就//会通过对任何仍然处于活动状态的后台线程调用 Abort 来结束该进程。


\n

t.IsBackground = true;


\n

t.Start();


\n

}


\n

catch(Exception ex)


\n

{


\n

MessageBox.Show(ex.Message);


\n

}


\n

}

\n

private void ReceiveMsg()


\n

{


\n

try


\n

{


\n

while (true)


\n

{


\n

byte[] byteRec = new byte[100];


\n

this.sock.Receive(byteRec);


\n

string strRec = System.Text.Encoding.UTF8.GetString(byteRec);


\n

if (this.rtbReceive.InvokeRequired)


\n

{


\n

this.rtbReceive.Invoke(new EventHandler(ChangeRtb), new object[]


\n

{ strRec, EventArgs.Empty });


\n

}


\n

}


\n

}


\n

catch(Exception ex)


\n

{


\n

MessageBox.Show(“Receive Message Error”+ex.Message);


\n

}


\n

}

\n

private void ChangeRtb(object obj, EventArgs e)


\n

{


\n

string s = System.Convert.ToString(obj);


\n

this.rtbReceive.AppendText(s + Environment.NewLine);


\n

}

\n

private void btSend_Click(object sender, EventArgs e)


\n

{


\n

byte[] byteSend =


\n

System.Text.Encoding.UTF8.GetBytes(this.tbSend.Text.ToCharArray());


\n

try


\n

{


\n

this.sock.Send(byteSend);


\n

}


\n

catch


\n

{


\n

MessageBox.Show(“Send Message Error”);


\n

}


\n

}

\n

private void btClose_Click(object sender, EventArgs e)


\n

{


\n

try


\n

{


\n

this.sock.Shutdown(SocketShutdown.Receive);


\n

this.sock.Close();


\n

Application.Exit();


\n

}


\n

catch


\n

{


\n

MessageBox.Show(“Exit Error”);


\n

}


\n

}


\n

}


\n

}


\n

不解之处:


\n

Client端红色标注语句:this.sock.Shutdown(SocketShutdown.Receive),如改成


\n

this.sock.Shutdown(SocketShutdown.Both);或this.sock.Shutdown(SocketShutdown.Send);


\n

则当点击Cloce按钮时,CPU使用率疯涨到100%,而使用this.sock.Shutdown(SocketShutdown.Receive);


\n

或不调用Shutdown()方法则没有这个问题。难道客户端不应该用Shutdown()?


\n

来源:网络

\n