Sign in to follow this  
Dead_Zone

need some help with async sockets

Recommended Posts

this is my code [CODE] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace TSNE { public class TSNE { TcpListener listener; public Thread listenthread; EventLog eL; int port; public TSNE(int Port) { eL = new EventLog(true); eL.AddToLog("TSNE Initializing..."); port = Port; if(port < 0 || port == 0){ eL.AddToLog("No Port Specified..."); eL.AddToLog("Please Enter Port to Listen On: "); port = Convert.ToInt32(Console.ReadLine()); } try { listener = new TcpListener(port); listener.Start(15); listenthread = new Thread(new ThreadStart(Accept)); } catch (System.Exception e) { Console.WriteLine(e); } } public TSNE() { eL.AddToLog("TSNE Initializing..."); if (port < 0 || port == 0) { eL.AddToLog("No Port Specified..."); eL.AddToLog("Please Enter Port to Listen On: "); port = Convert.ToInt32(Console.ReadLine()); } try { listener = new TcpListener(port); listener.Start(15); listenthread = new Thread(new ThreadStart(Accept)); } catch (System.Exception e) { Console.WriteLine(e); } } private void Accept() { listener.BeginAcceptSocket(OnAccept, listener); } public void StartAccept() { listenthread.Start(); } public void Run() { } public void Initialize() { } private void OnAccept(IAsyncResult ar) { TcpListener thislistener = (TcpListener)ar.AsyncState; StateObject handle = new StateObject(); handle.workSocket = thislistener.EndAcceptSocket(ar); eL.AddToLog("Client Connected: " + handle.workSocket.RemoteEndPoint.ToString()); handle.buffer = new byte[handle.workSocket.Available]; handle.workSocket.BeginReceive(handle.buffer, 0, handle.buffer, SocketFlags.None, OnReadComplete, handle); thislistener.BeginAcceptSocket(OnAccept, thislistener); } public virtual void OnReadComplete(IAsyncResult ar) { StateObject handle = (StateObject)ar.AsyncState; eL.AddToLog("Recieved " + handle.buffer.Length + " bytes of data."); } } public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer. public const int BufferSize = 0x800; // Receive buffer. public byte[] buffer; } } [/CODE] the problem is that when clients connect simultaniously the returned available size for the recieved data is 0 so its like only a few connections are fine at sending the data and the rest my server doesnt get, could anyone help me spot out my problem?

Share this post


Link to post
Share on other sites

1). Is this valid ? I'm not aware of any Socket's BeginReceive overload for BeginReceive( byte[] buffer,int offset ,byte[] buffer,.,.,.,...)

handle.workSocket.BeginReceive(handle.buffer, 0, handle.buffer, SocketFlags.None, OnReadComplete, handle);

I think the third parameter should be Int not byte[] ? which is the maximum alotted buffer size. BeginReceive( byte[] buffer,int offset , int size,.,.,...)

handle.workSocket.BeginReceive(handle.buffer, 0, handle.buffer.Length, SocketFlags.None, OnReadComplete, handle);


2). After OnReadComplete has been processed, what next ? I think you should also call handle.workSocket.BeginReceive before leaving OnReadComplete call back.


3). Try to replace your buffer allocation size with something like 512,1024,Etc.. not the previous socket's available bytes read.

handle.buffer = new byte[ 1024 ];

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this