Found out in one of my studies an not so old socket system, and since i'm not using it anymore (got a new one ;P), its nearly copy+paste, but you can also check somethings if you want tho... It's not the best one, but it works without problems...
ServerSocket.cs:
Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ConquerServer_v3_Msg.Common;
namespace ConquerServer_v3_Msg.Basecode
{
public delegate void Connection(ClientSocket pClient);
public delegate void Receive(ClientSocket pClient, Byte[] bufMsg);
public class ServerSocket// : GameThread
{
protected Socket m_sock;
protected int m_nPort;
protected Thread m_hThread;
protected ManualResetEvent m_asyncAcpt;
public Connection BeginAccept;
public Connection BeginClose;
public Receive BeginReceive;
public ServerSocket(int nPort)
{
m_nPort = nPort;
m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_asyncAcpt = new ManualResetEvent(false);
m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
}
public void Init(int nBacklog)
{
try
{
m_sock.Bind(new IPEndPoint(IPAddress.Any, m_nPort));
m_sock.Listen(nBacklog);
m_hThread = new Thread(new ThreadStart(OnTimer));
m_hThread.Priority = ThreadPriority.Normal;
m_hThread.Start();
}
catch (Exception ex)
{
Kernel.Print("ServerSocket.Init()", "Failure to accept a new connection.");
Kernel.LogPrint(ex.ToString());
}
}
protected void OnTimer()
{
try
{
while (true)
{
m_asyncAcpt.Reset();
m_sock.BeginAccept(new AsyncCallback(Accept), m_sock);
m_asyncAcpt.WaitOne();
}
}
catch (Exception ex)
{
Kernel.Print("ServerSocket.OnTimer()", "Failed to use ManualResetEvent.");
Kernel.LogPrint(ex.ToString());
}
}
protected void Accept(IAsyncResult pResult)
{
try
{
Socket newSock;
try { newSock = m_sock.EndAccept(pResult); }
catch (SocketException ex)
{
Kernel.Print("ServerSocket()", "Failure to accept a new connection.");
Kernel.LogPrint(ex.ToString());
newSock = null;
}
m_asyncAcpt.Set();
if (newSock != null)
{
ClientSocket pClient = new ClientSocket();
pClient.Open(newSock, this);
if (!pClient.IsOpen())
return;
if (BeginAccept != null)
BeginAccept.Invoke(pClient);
}
}
catch (SocketException ex)
{
Kernel.Print("ServerSocket.Accept()", "Logged error.");
Kernel.LogPrint(ex.ToString());
m_asyncAcpt.Set();
}
}
}
}
ClientSocket.cs:
Code:
using System;
using System.Net.Sockets;
using System.Text;
using ConquerServer_v3_Msg.NpcKernel;
using System.Threading;
using ConquerServer_v3_Msg.WorldKernel;
namespace ConquerServer_v3_Msg.Basecode
{
public class ClientSocket
{
protected string m_szIP;
protected User m_user;
protected EncryptServer m_encServer;
protected ServerSocket m_server;
protected Socket m_sock;
protected byte[] m_bufMsg;
protected int m_nLen;
protected byte[] m_bufSendMsg;
protected enum SKT_STATE { STATE_CLOSED = 0, STATE_OPEN };
protected SKT_STATE m_nState;
protected Thread m_hThread;
protected ManualResetEvent m_asyncRecv;
public User User { get { return m_user; } set { m_user = value; } }
public EncryptServer EncServer { get { return m_encServer; } set { m_encServer = value; } }
public bool IsOpen() { return m_nState == SKT_STATE.STATE_OPEN; }
public ClientSocket()
{
m_sock = null;
m_nLen = 0;
m_nState = SKT_STATE.STATE_CLOSED;
m_szIP = "";
}
public bool Open(Socket nSock, ServerSocket nServer)
{
m_sock = nSock;
m_server = nServer;
if (m_nState == SKT_STATE.STATE_OPEN)
return true;
if (m_nState == SKT_STATE.STATE_CLOSED)
{
m_nLen = 0;
m_bufMsg = new byte[1024];
m_nState = SKT_STATE.STATE_OPEN;
m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
m_sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
m_szIP = m_sock.RemoteEndPoint.AddressFamily.ToString();
m_asyncRecv = new ManualResetEvent(false);
m_hThread = new Thread(new ThreadStart(OnTimer));
m_hThread.Priority = ThreadPriority.Normal;
m_hThread.Start();
}
return true;
}
public void CreateCipher()
{
// if need an upate, there's no need to do a mess in the source code...
// so just create the encrypt server after the real login...
m_encServer = new EncryptServer();
}
public bool SendPacket(Byte[] bufMsg)
{
if (m_nState != SKT_STATE.STATE_OPEN)
{
Kernel.Print("ClientSocket.SendPacket()", "Client socket is closed.");
return false;
}
if (bufMsg == null)
{
Kernel.Print("ClientSocket.SendPacket()", "Nulled buffer detected.");
return false;
}
if (bufMsg.Length > 1024)
{
Kernel.Print("ClientSocket.SendPacket()", "Buffer length is higher than 1024.");
return false;
}
m_bufSendMsg = new Byte[bufMsg.Length];
Buffer.BlockCopy(bufMsg, 0, m_bufSendMsg, 0, m_bufSendMsg.Length);
m_encServer.Encrypt(ref m_bufSendMsg);
try { m_sock.BeginSend(m_bufSendMsg, 0, m_bufSendMsg.Length, SocketFlags.None, new AsyncCallback(EndSend), null); }
catch (SocketException ex)
{
Kernel.Print("ClientSocket.SendPacket()", "Logged error.");
Kernel.LogPrint(ex.ToString());
Disconnect(false);
return false;
}
return true;
}
public bool Disconnect(bool bNoLinger)
{
if (m_nState == SKT_STATE.STATE_CLOSED)
return false;
Abort();
if (m_sock != null)
{
try
{
m_nState = SKT_STATE.STATE_CLOSED;
m_sock.Shutdown(SocketShutdown.Both);
m_sock.Disconnect(bNoLinger);
m_sock.Close();
m_sock = null;
}
catch (SocketException ex)
{
Kernel.Print("ClientSocket.Disconnect()", "Logged error.");
Kernel.LogPrint(ex.ToString());
return false;
}
}
if (User != null)
{
Kernel.GetManager().Del(User.GetId());
MapGroup pMapGroup = MapGroupKernel.GetGroupById(User.GetMap());
pMapGroup.Del(User);
}
return true;
}
protected void OnTimer()
{
try
{
while (true)
{
m_asyncRecv.Reset();
if (m_sock != null)
m_sock.BeginReceive(m_bufMsg, 0, 1024, SocketFlags.None, new AsyncCallback(BeginReceive), null);
else
break;
m_asyncRecv.WaitOne();
}
}
catch (SocketException ex)
{
Kernel.Print("ClientSocket.OnTimer()", "Logged error.");
Kernel.LogPrint(ex.ToString());
Disconnect(false);
return;
}
}
public void Abort()
{
try
{
if (m_hThread != null)
{
m_hThread.Abort();
m_hThread = null;
}
if (m_asyncRecv != null)
{
m_asyncRecv.Close();
m_asyncRecv = null;
}
}
catch (Exception ex)
{
Kernel.Print("ClientSocket.Abort()", "Logged error.");
Kernel.LogPrint(ex.ToString());
m_hThread = null;
m_asyncRecv = null;
}
}
protected void BeginReceive(IAsyncResult pResult)
{
try
{
m_nLen = m_sock.EndReceive(pResult);
if (m_nLen > 0)
{ // proccess te received data if trully received it...
Byte[] bufMsg = new Byte[m_nLen];
Buffer.BlockCopy(m_bufMsg, 0, bufMsg, 0, m_nLen);
m_server.BeginReceive(this, bufMsg);
if (m_asyncRecv != null)
{
m_asyncRecv.Set();
return;
}
}
// Otherwise disconnect
Disconnect(false);
}
catch (SocketException ex)
{
Kernel.Print("ClientSocket.BeginReceive()", "Logged error.");
Kernel.LogPrint(ex.ToString());
Disconnect(false);
}
}
protected void EndSend(IAsyncResult pResult)
{
if (m_sock == null)
return;
try { m_sock.EndSend(pResult); }
catch (SocketException ex)
{
Kernel.Print("ClientSocket.EndSend()", "Logged error.");
Kernel.LogPrint(ex.ToString());
Disconnect(false);
}
}
}
}
Initialization method...
Code:
// Initialize the ServerSocket....
m_pSocket = new ServerSocket(5816);
m_pSocket.BeginAccept = new Connection(Connectors.BeginAccept);
m_pSocket.BeginReceive = new Receive(Connectors.BeginReceive);
m_pSocket.BeginClose = new Connection(Connectors.BeginClose);
m_pSocket.Init(10);
// -------------------------------
Cya ;P






