Server Multithreading Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Server Multithreading Help

Post by tappatekie »

Right I have a extremely frustrating error that I have never been able to fix.

I am using C#.Net and the bog standard System.Threading namespace and I it's fine if you give me code for VB.Net or .Net for C++.

I am trying to get a multi-threading system going in the web server so it can process more than 1 request at the same time (different environment). Every connection to the server is placed in an array (which then gets removed when it disconnects). When it comes to actually receiving from the socket, that part is in a seperate thread. I pass the index of the entry that the connection is in (with the socket) to the thread (it's a ParameterizedThreadStart).
I set the apartment state of the thread to MTA and then start it.

This is my problem, When I request a page (with images) from the server, the server sends the data to the wrong socket (as if the index is coming from the next index in the loop.

I have tried seting the apartment state to STA, and even made a bool outside the thread that is set to true when the thread has grabbed the socket from the collection, but still to no success. The server then hangs until the that bool is set

I do not know what else to try, I tried google but there was no helpful information but there where recommendations to use a seperate library, but don't really like the idea (my own preference really).

I really don't want to scrap the server requests from being non-threaded because it will slow page load times right down
Last edited by tappatekie on Wed Nov 30, 2011 5:42 pm, edited 1 time in total.
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: Help

Post by tappatekie »

Update:
I have found something using google (did'nt find before because I was being specific in the search)

Im using

Code: Select all

/*
*I will probably use my own home-brew object type for this bit
*/
private AutoResetEvent p_WaitHandle = new AutoResetEvent(false);

private void listen(TcpListener listener){
       while(true){
              IAsyncResult result = listener.BeginAcceptSocket(socketConnectHandler, listener);
              p_WaitHandle.WaitOne();
       }
}
private void socketConnectHandle(IAsyncResult result){
       TcpListener listen = (TcpListener)result.AsyncState;
       Socket socket = listen.EndAcceptSocket(result)
       p_WaitHandler.Set();
       //Do processing stuff here
       //Not entirely sure if this is where you define the thread iself...
}
This code works! I just tried it out on the server (just need to re-write a few things now :D)
I am still having trouble :evil: it is doing it but now and again, it misses a socket and causing an image to show up on the website in the wrong location.

Thanks to
http://stackoverflow.com/questions/6244 ... ads-in-net
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: Server Multithreading Help

Post by tappatekie »

Okay, Just to let you know (if you are looking a this post) that I have fixed the problem.
It turned out it was a coding error I made when the server actually processed the socket.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Server Multithreading Help

Post by dandymcgee »

tappatekie wrote:Okay, Just to let you know (if you are looking a this post) that I have fixed the problem.
It turned out it was a coding error I made when the server actually processed the socket.
Ah, glad you finally figured it out.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: Server Multithreading Help

Post by tappatekie »

dandymcgee wrote:Ah, glad you finally figured it out.
Thanks, it was a real thorn to get rid of
Post Reply