Odd performance with C# Asynchronous server socket
Posted
by The.Anti.9
on Stack Overflow
See other posts from Stack Overflow
or by The.Anti.9
Published on 2010-05-08T05:38:29Z
Indexed on
2010/05/08
5:48 UTC
Read the original article
Hit count: 350
I'm working on a web server in C# and I have it running on Asynchronous socket calls. The weird thing is that for some reason, when you start loading pages, the 3rd request is where the browser won't connect. It just keeps saying "Connecting..." and doesn't ever stop. If I hit stop. and then refresh, it will load again, but if I try another time after that it does the thing where it doesn't load again. And it continues in that cycle. I'm not really sure what is making it do that.
The code is kind of hacked together from a couple of examples and some old code I had. Any miscellaneous tips would be helpful as well.
Heres my little Listener class that handles everything
(pastied here. thought it might be easier to read this way)
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace irek.Server
{
public class Listener
{
private int port;
private Socket server;
private Byte[] data = new Byte[2048];
static ManualResetEvent allDone = new ManualResetEvent(false);
public Listener(int _port)
{
port = _port;
}
public void Run()
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, port);
server.Bind(iep);
Console.WriteLine("Server Initialized.");
server.Listen(5);
Console.WriteLine("Listening...");
while (true)
{
allDone.Reset();
server.BeginAccept(new AsyncCallback(AcceptCon), server);
allDone.WaitOne();
}
}
private void AcceptCon(IAsyncResult iar)
{
allDone.Set();
Socket s = (Socket)iar.AsyncState;
Socket s2 = s.EndAccept(iar);
SocketStateObject state = new SocketStateObject();
state.workSocket = s2;
s2.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), state);
}
private void Read(IAsyncResult iar)
{
try
{
SocketStateObject state = (SocketStateObject)iar.AsyncState;
Socket s = state.workSocket;
int read = s.EndReceive(iar);
if (read > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, read));
if (s.Available > 0)
{
s.BeginReceive(state.buffer, 0, SocketStateObject.BUFFER_SIZE, 0, new AsyncCallback(Read), state);
return;
}
}
if (state.sb.Length > 1)
{
string requestString = state.sb.ToString();
// HANDLE REQUEST HERE
// Temporary response
string resp = "<h1>It Works!</h1>";
string head = "HTTP/1.1 200 OK\r\nContent-Type: text/html;\r\nServer: irek\r\nContent-Length:"+resp.Length+"\r\n\r\n";
byte[] answer = Encoding.ASCII.GetBytes(head+resp);
// end temp.
state.workSocket.BeginSend(answer, 0, answer.Length, SocketFlags.None, new AsyncCallback(Send), state.workSocket);
}
}
catch (Exception)
{
return;
}
}
private void Send(IAsyncResult iar)
{
try
{
SocketStateObject state = (SocketStateObject)iar.AsyncState;
int sent = state.workSocket.EndSend(iar);
state.workSocket.Shutdown(SocketShutdown.Both);
state.workSocket.Close();
}
catch (Exception)
{
}
return;
}
}
}
And my SocketStateObject:
public class SocketStateObject
{
public Socket workSocket = null;
public const int BUFFER_SIZE = 1024;
public byte[] buffer = new byte[BUFFER_SIZE];
public StringBuilder sb = new StringBuilder();
}
© Stack Overflow or respective owner