public void ReadConfirmationCallback(IAsyncResult ar) { String content = String.Empty; StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; try { int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.Unicode.GetString(state.buffer, 0, bytesRead)); content = state.sb.ToString(); // MessageBox.Show(content); // Read the confirmation Shot shot = Shot.jsonToShot(content); if (backend.shotResult(shot)) { receiveShot(); } } } catch (SocketException e) { MessageBox.Show(e.ToString()); handler.Shutdown(SocketShutdown.Both); handler.Close(); } }
private void ReadShotCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; try { // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.Unicode.GetString( state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. content = state.sb.ToString(); // MessageBox.Show(content); // Convert the receivedData to a shot Shot shot = Shot.jsonToShot(content); shot = backend.enemyFired(shot); sendConfirmation(shot); backend.endOrPlay(); } } catch (SocketException e) { MessageBox.Show(e.ToString()); handler.Shutdown(SocketShutdown.Both); handler.Close(); } }