These are some old of mine.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace YukaServer.Network.Sockets
{
public class EndResult
{
/// <summary>
/// The server socket.
/// </summary>
private Socket Socket;
/// <summary>
/// The async result.
/// </summary>
private IAsyncResult asyncResult;
/// <summary>
/// Creates a new instance of EndResult.
/// </summary>
/// <param name="Socket">The socket.</param>
/// <param name="asyncResult">The async result.</param>
public EndResult(Socket Socket, IAsyncResult asyncResult)
{
this.Socket = Socket;
this.asyncResult = asyncResult;
}
/// <summary>
/// Gets the socket from the endaccept result.
/// </summary>
/// <returns>Returns null if there was no socket accepted.</returns>
public Socket GetSocket()
{
try
{
return Socket.EndAccept(asyncResult);
}
catch
{
return null;
}
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace YukaServer.Network.Sockets
{
public class SocketClient
{
/// <summary>
/// The event invoked when a client disconnects.
/// </summary>
public SocketConnectionHandler OnDisconnection;
/// <summary>
/// The event invoked when the server receives data from a client.
/// </summary>
public SocketBufferHandler OnReceive;
/// <summary>
/// The actual client socket.
/// </summary>
private Socket clientSocket;
/// <summary>
/// The socket owner.
/// </summary>
public object Owner;
/// <summary>
/// Gets the client socket.
/// </summary>
public Socket ClientSocket
{
get
{
return clientSocket;
}
}
/// <summary>
/// The data-holding buffer.
/// </summary>
public byte[] Buffer;
/// <summary>
/// Gets the IPAddress of the EndPoint.
/// </summary>
public IPAddress Address
{
get
{
return (ClientSocket.RemoteEndPoint as IPEndPoint).Address;
}
}
/// <summary>
/// Gets the IPAddress of the EndPoint as a string.
/// </summary>
public string IP
{
get
{
return Address.ToString();
}
}
/// <summary>
/// Ends the accepting of the socket.
/// </summary>
/// <param name="endResult">The EndResult.</param>
/// <returns>Returns true if the sockets connection was accepted.</returns>
public bool EndAccept(EndResult endResult)
{
return (clientSocket = endResult.GetSocket()) != null;
}
/// <summary>
/// The method handling all receive callbacks from the client socket.
/// </summary>
/// <param name="result">The async result.</param>
private void Receive_Callback(IAsyncResult asyncResult)
{
try
{
if (ClientSocket.Connected)
{
SocketError err;
int recSize = ClientSocket.EndReceive(asyncResult, out err);
if (err == SocketError.Success)
{
if (recSize >= 4 && recSize <= 1024)
{
byte[] recPacket = new byte[recSize];
Native.Msvcrt.MemoryCopy(recPacket, Buffer, recSize);
if (OnReceive != null)
OnReceive.Invoke(this, recPacket);
BeginReceive();
}
}
else
Disconnect();
}
else
Disconnect();
}
catch
{
Disconnect();
}
}
/// <summary>
/// Beginning to receive data from a client.
/// </summary>
public void BeginReceive()
{
Buffer = new byte[1024];
ClientSocket.BeginReceive(Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(Receive_Callback), null);
}
/// <summary>
/// Sending data to the client.
/// </summary>
/// <param name="Buffer">The buffer to send.</param>
public void Send(byte[] Buffer)
{
try
{
if (ClientSocket.Connected)
{
ClientSocket.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(Send_Callback), null);
}
else
Disconnect();
}
catch
{
Disconnect();
}
}
/// <summary>
/// The method handling all send callbacks from the client socket.
/// </summary>
/// <param name="asyncResult">The async result.</param>
private void Send_Callback(IAsyncResult asyncResult)
{
try
{
int send = ClientSocket.EndSend(asyncResult);
if (send < 4)
Disconnect();
}
catch
{
}
}
/// <summary>
/// This will disconnect a client, but this is also called when a disconnection happen.
/// </summary>
public void Disconnect()
{
try
{
ClientSocket.Disconnect(false);
}
catch { }
if (OnDisconnection != null)
OnDisconnection.Invoke(this);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YukaServer.Network.Sockets
{
public delegate void SocketConnectionHandler(SocketClient sClient);
public delegate void SocketBufferHandler(SocketClient sClient, byte[] Buffer);
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace YukaServer.Network.Sockets
{
public class SocketServer
{
/// <summary>
/// The server socket.
/// </summary>
public Socket ServerSocket;
/// <summary>
/// The port the server is bound to.
/// </summary>
private int port;
/// <summary>
/// Gets the port the server is bound to.
/// </summary>
public int Port
{
get
{
return port;
}
}
/// <summary>
/// If the server should use any ip for the endpoint.
/// </summary>
private bool anyIP;
/// <summary>
/// The event invoked when a client connects.
/// </summary>
public SocketConnectionHandler OnConnection;
/// <summary>
/// Creates a new instance of the socket server.
/// </summary>
/// <param name="Port">The port, which the server should be bound to.</param>
public SocketServer(int Port, bool anyIP)
{
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
port = Port;
this.anyIP = anyIP;
}
/// <summary>
/// Binding the socket to the port assigned with the server.
/// </summary>
/// <param name="IP">The IP. Should be null, if the server is set to anyIP.</param>
public void Bind(IPAddress IP)
{
IPAddress ip = (anyIP) ? IPAddress.Any : (IP == null) ? IPAddress.Any : IP;
ServerSocket.Bind(new IPEndPoint(ip, Port));
ServerSocket.Listen(100);
}
/// <summary>
/// Calling System.Net.Sockets.Socket.BeginAccept() with the necessary parameters.
/// </summary>
public void BeginAccept()
{
ServerSocket.BeginAccept(new AsyncCallback(Accept_Callback), new SocketClient());
}
/// <summary>
/// The method handling all accept callbacks from the client socket.
/// </summary>
/// <param name="asyncResult">The async result.</param>
private void Accept_Callback(IAsyncResult asyncResult)
{
try
{
if (asyncResult.AsyncState != null)
{
SocketClient sClient = asyncResult.AsyncState as SocketClient;
if (sClient.EndAccept(new EndResult(ServerSocket, asyncResult)))
{
if (OnConnection != null)
OnConnection.Invoke(sClient);
}
}
}
catch
{
}
BeginAccept();
}
}
}
You could compare yours to those and ermm you're always running a while loop around the beginaccept, which is not necessary as you just call the beginaccept in the accept callback and it should work just fine.
Oh and your ConsumerPool. Use
[Only registered and activated users can see links. Click Here To Register...]