private void Process(object arg) { MemoryStream stream; Socket client = (Socket)arg; try { byte[] buffer = new byte[bufferSize]; while (true) { stream = new MemoryStream(); int totalBytes = 0; Console.Write("took "); do { short bytes = (short)client.Receive(buffer); stream.Write(buffer, 0, bytes); Console.Write(bytes + ", "); totalBytes += bytes; }while (client.Available > 0); Console.WriteLine(); Console.WriteLine("Сообщение получено от " + client.RemoteEndPoint.ToString() + "в размере " + totalBytes); dbRequest request = (dbRequest)DbObject.Deserialize(stream); stream.Close(); dbResult result = ProcessDbObject(request); SendDbResult(client, result); } } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine("Разъединение " + client.RemoteEndPoint.ToString()); client.Shutdown(SocketShutdown.Both); client.Close(); } }
void listenMessages() { started = true; int comingBufferSize = socket.ReceiveBufferSize; while (true) { try { MemoryStream stream = new MemoryStream(); byte[] buffer = new byte[comingBufferSize]; int totalBytes = 0; do { int bytes = socket.Receive(buffer); stream.Write(buffer, 0, bytes); totalBytes += bytes; }while (socket.Available > 0); Console.WriteLine("Получено " + totalBytes + " байтов"); var result = (dbResult)DbObject.Deserialize(stream); Console.WriteLine(result.action.ToString()); switch (result.action) { case dbAction.VERIFY_USER: if (result.isSuccessful) { this.currentUser = (User)result.objects[0]; isVerified = true; } break; } currentForm.Invoke(messageIncome, result); stream.Close(); } catch (SerializationException e) { Console.WriteLine(e.ToString()); } catch (SocketException e) { socket.Shutdown(SocketShutdown.Both); socket.Close(); started = false; throw e; } catch (Exception e) { Console.WriteLine(e.Message); throw e; } } }