?? class1.cs
字號(hào):
using System;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.Text;
namespace ChatServer
{
class UserInfo
{
public string m_strName;
public IPEndPoint m_rep;
}
class UDPServer
{
[STAThread]
static void Main(string[] args)
{
UDPServer server = new UDPServer();
server.Receive();
}
//其它函數(shù)
Socket m_UdpServer=null;
UserInfo[] m_users = new UserInfo[1024];
int m_nUserCntr = 0;
UDPServer()
{
m_UdpServer = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any,8888);
m_UdpServer.Bind(iep);
}
void Receive()
{
while(true)
{
byte[] buf = new byte[2048];
EndPoint iep = new IPEndPoint(IPAddress.Any,0);
int nLen = m_UdpServer.ReceiveFrom(buf,ref iep);
string str = new UTF8Encoding().GetString(buf,0,nLen);
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
if(doc.FirstChild.Name == "login")
{
string strUser = doc.FirstChild.Attributes["user"].Value;
m_users[m_nUserCntr] = new UserInfo();
m_users[m_nUserCntr].m_strName = strUser;
m_users[m_nUserCntr].m_rep = (IPEndPoint)iep;
m_nUserCntr++;
}
else if(doc.FirstChild.Name == "logout")
{
for(int i=0;i<m_nUserCntr;i++)
{
if(m_users[i].m_rep == iep)
{
m_nUserCntr--;
m_users[i] = m_users[m_nUserCntr];
m_users[m_nUserCntr] = null;
break;
}
}
}
else
{
string strRecv = doc.FirstChild.Attributes["recv"].Value;
for(int i=0;i<m_nUserCntr;i++)
{
if(m_users[i].m_strName == strRecv)
{
m_UdpServer.SendTo(buf,nLen,SocketFlags.None,m_users[i].m_rep);
break;
}
}
}
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -