Server Socket Synchronization When Connecting?

O

O.B.

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?
 
M

Michael Goldfarb

You need to call EndAccept before performing any other operations with
your socket.

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

O.B.

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;
}
}
 
W

William Stacey [C# MVP]

Nothing stops thread 1 from calling Stop() on the listener at any time -
other then good design.

--
William Stacey [C# MVP]

| 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.
| >
| >> 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?
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top