RICOH SC-10 SERIES
Socket Mode Function Operating Instructions Ver.1.0.0
63/66
// Callback process when connection accepted
private
void
AcceptCallback(
IAsyncResult
asyncResult)
{
// Set signal so that wait thread progresses
AllDone.Set();
// Acquire socket
var
listenerSocket = asyncResult.AsyncState
as
Socket
;
var
clientSocket = listenerSocket.EndAccept(asyncResult);
// Add client during connection
ClientSockets.Add(clientSocket);
Console
.WriteLine(
$"Connection:
{clientSocket.RemoteEndPoint}
"
);
// Create StateObject
var
state =
new
StateObject
();
state.ClientSocket = clientSocket;
// Set callback process for when receive
clientSocket.BeginReceive(state.Buffer, 0,
StateObject
.BufferSize, 0,
new
AsyncCallback
(ReceiveCallback),
state);
}
private
void
ReceiveCallback(
IAsyncResult
asyncResult)
{
// Acquire StateObject and client socket
var
state = asyncResult.AsyncState
as
StateObject
;
var
clientSocket = state.ClientSocket;
// End acquisition of data received from client socket
int
bytes = clientSocket.EndReceive(asyncResult);
if
(bytes > 0)
{
// Display received string
foreach
(
var
byint
in
state.Buffer)
Console
.Write(byint+
" "
);
// Set callback process again for when receive
clientSocket.BeginReceive(state.Buffer, 0,
StateObject
.BufferSize, 0,
new
AsyncCallback
(ReceiveCallback), state);
Console
.WriteLine(
"\nReceiving complete\n"
);
}
else
{
// Determine to be disconnected when receive 0 byte of data
clientSocket.Close();
this
.ClientSockets.Remove(clientSocket);
}
}
}