By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Server Socket Synchronization When Connecting?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.