读书人

UDP服务器的有关问题(附代码)

发布时间: 2012-01-31 21:28:41 作者: rapoo

UDP服务器的问题(附代码)
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 255;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}

public class AsynchronousSocketListener
{

// Incoming data from client.
public static string data = null;

// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static int Count = 1;
public AsynchronousSocketListener()
{
}

public static void StartListening()
{
byte[] bytes = new Byte[255];

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 2020);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
listener.Bind(localEndPoint);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;
try
{
while (true)
{
allDone.Reset();


StateObject so2 = new StateObject();
so2.workSocket = listener;
listener.BeginReceiveFrom(so2.buffer, 0, StateObject.BufferSize, 0, ref tempRemoteEP,new AsyncCallback(ReadCallback), so2);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine( "\nPress ENTER to continue... ");
Console.Read();

}


public static void ReadCallback(IAsyncResult ar)
{
allDone.Set();
StateObject so = (StateObject)ar.AsyncState;
Socket s = so.workSocket;
so.sb.Remove(0, so.sb.Length);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;

int read = s.EndReceiveFrom(ar, ref tempRemoteEP);
if (read > 0)
{
//so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
//so.sb.Append(Encoding.Default.GetString(so.buffer, 0, read));


for (int i = 0; i < read; i++)
{
so.sb.Append(string.Format( "{0:X2} ", so.buffer[i]));
}
}
else
{
}
if (so.sb.Length > 1)
{
//All the data has been read, so displays it to the console.
string strContent;
strContent = so.sb.ToString();
Count = Count + 1;
Console.WriteLine(String.Format( "Count {0} Read {1} byte from socket " +
"data = {2} ", Count, strContent.Length, strContent));

if (strContent.IndexOf( "454F46 ") > = 0)//挂断指令
{
s.Close();//问题出现了,一挂断就出错
}
}

}

private static void Send(Socket handler, EndPoint remoteEP, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
handler.BeginSendTo(byteData, 0, byteData.Length, 0, remoteEP,
new AsyncCallback(SendCallback), handler);
}

private static void SendCallback(IAsyncResult ar)
{
try
{


Socket handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}


public static int Main(String[] args)
{
StartListening();
return 0;
}
}


[解决办法]
出什么错??
[解决办法]
ReadCallback函数,在中途是不能退出的
你想一下,你启动了Socket Listening,在Listening中你用ReadCallback读取数据,读完后,你将SOCKET关闭了,那么在主线程中有的还在用SOCKET,这个时候已经没有SOCKET了,所以就出现问题了
嘿嘿,所以,况且你那个读数据的函数是回调函数,更不能随便关了,你可以在程序运行结束的时候,关闭

连接的太多了的话,你这样是不行的,要使用线程池或着完成端口实现
[解决办法]
发给你了

读书人网 >C#

热点推荐