473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Server Socket Synchronization When Connecting?

When sharing a Socket between threads, are the socket operations
automatically synchronized to support multithreading?

For example:

Thread1 sets up a server socket to listen and invokes BeginAccept.

A connection is made and Thread2 (within the BeginAccept delegate) begins.

Does the server socket been to be "locked" before Thread2 calls
EndAccept? If not, what prevents corruption when Thread1 from closing
the server socket while the BeginAccept delegate (Thread2) is just about
to call EndAccept?
Oct 26 '06 #1
3 2468
You need to call EndAccept before performing any other operations with
your socket.

Once you do that you can pass your socket through threads.

On Oct 26, 12:47 pm, "O.B." <funkj...@bellsouth.netwrote:
When sharing a Socket between threads, are the socket operations
automatically synchronized to support multithreading?

For example:

Thread1 sets up a server socket to listen and invokes BeginAccept.

A connection is made and Thread2 (within the BeginAccept delegate) begins.

Does the server socket been to be "locked" before Thread2 calls
EndAccept? If not, what prevents corruption when Thread1 from closing
the server socket while the BeginAccept delegate (Thread2) is just about
to call EndAccept?
Oct 26 '06 #2
Hmm, maybe I'm not being clear enough. There are actually 3 threads.
Here's some code. See my question in the AcceptConnection operation.

The first thread starts a new thread using the AcceptConnectEventLoop as
a delegate.
// Invoked as a new thread from the main thread.
// This is the second thread.
private void AcceptConnectEventLoop() {
IAsyncResult result = null;
while (true) {
if (socket != null && socket.IsBound) {
result = socket.BeginAccept(new AsyncCallback(AcceptConnection),
socket);
} else {
break;
}
// Wait for the EndAccept before issuing a new BeginAccept
result.AsyncWaitHandle.WaitOne();
}
}

// This is the third thread; invoked when a client wants
// to make a connection.
private void AcceptConnection(IAsyncResult asyncResult) {
Socket serverSocket = (Socket)asyncResult.AsyncState;
Socket clientSocket = null;
try {
// WHAT PREVENTS THE 1ST THREAD FROM CLOSING THE SOCKET
// BEFORE THE FOLLOWING LINE EXECUTES???
clientSocket = serverSocket.EndAccept(asyncResult);
lock (connectedSockets) {
connectedSockets.Add(clientSocket);
}
} catch (SocketException) {
return;
} catch (Exception) {
return;
}
}


Michael Goldfarb wrote:
You need to call EndAccept before performing any other operations with
your socket.

Once you do that you can pass your socket through threads.

On Oct 26, 12:47 pm, "O.B." <funkj...@bellsouth.netwrote:
>When sharing a Socket between threads, are the socket operations
automatically synchronized to support multithreading?

For example:

Thread1 sets up a server socket to listen and invokes BeginAccept.

A connection is made and Thread2 (within the BeginAccept delegate) begins.

Does the server socket been to be "locked" before Thread2 calls
EndAccept? If not, what prevents corruption when Thread1 from closing
the server socket while the BeginAccept delegate (Thread2) is just about
to call EndAccept?
Oct 26 '06 #3
Nothing stops thread 1 from calling Stop() on the listener at any time -
other then good design.

--
William Stacey [C# MVP]

"O.B." <fu******@bellsouth.netwrote in message
news:45**************@bellsouth.net...
| Hmm, maybe I'm not being clear enough. There are actually 3 threads.
| Here's some code. See my question in the AcceptConnection operation.
|
| The first thread starts a new thread using the AcceptConnectEventLoop as
| a delegate.
|
|
| // Invoked as a new thread from the main thread.
| // This is the second thread.
| private void AcceptConnectEventLoop() {
| IAsyncResult result = null;
| while (true) {
| if (socket != null && socket.IsBound) {
| result = socket.BeginAccept(new AsyncCallback(AcceptConnection),
| socket);
| } else {
| break;
| }
| // Wait for the EndAccept before issuing a new BeginAccept
| result.AsyncWaitHandle.WaitOne();
| }
| }
|
| // This is the third thread; invoked when a client wants
| // to make a connection.
| private void AcceptConnection(IAsyncResult asyncResult) {
| Socket serverSocket = (Socket)asyncResult.AsyncState;
| Socket clientSocket = null;
| try {
| // WHAT PREVENTS THE 1ST THREAD FROM CLOSING THE SOCKET
| // BEFORE THE FOLLOWING LINE EXECUTES???
| clientSocket = serverSocket.EndAccept(asyncResult);
| lock (connectedSockets) {
| connectedSockets.Add(clientSocket);
| }
| } catch (SocketException) {
| return;
| } catch (Exception) {
| return;
| }
| }
|
|
|
|
|
|
| Michael Goldfarb wrote:
| You need to call EndAccept before performing any other operations with
| your socket.
| >
| Once you do that you can pass your socket through threads.
| >
| On Oct 26, 12:47 pm, "O.B." <funkj...@bellsouth.netwrote:
| >When sharing a Socket between threads, are the socket operations
| >automatically synchronized to support multithreading?
| >>
| >For example:
| >>
| >Thread1 sets up a server socket to listen and invokes BeginAccept.
| >>
| >A connection is made and Thread2 (within the BeginAccept delegate)
begins.
| >>
| >Does the server socket been to be "locked" before Thread2 calls
| >EndAccept? If not, what prevents corruption when Thread1 from closing
| >the server socket while the BeginAccept delegate (Thread2) is just
about
| >to call EndAccept?
| >
Oct 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
0
by: christian_stengel | last post by:
Hi *, I have just started to learn python and I am having a problem with an python client connecting to a perl server using ssl (I tried this with pyOpenSSL and with the build in SSL Module). ...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
5
by: Cichy | last post by:
Hello, I'm writing a Client-Server application using sockets (asynchronous). There is a Server (Master) which accepts incoming connections, and Client (Slave). Afetr establishing connections...
7
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize,...
2
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.