private void sendCallback(IAsyncResult ar) { try { sendState state = ar.AsyncState as sendState; int sizeSend = state.socket.EndSend(ar); if (!state.sent) { log("Sizeinfo sent."); state.sent = true; //sending actual data state.socket.BeginSend(state.data, 0, state.data.Length, SocketFlags.None, new AsyncCallback(sendCallback), state); } else { log($"Sent {sizeSend} Bytes to the Server."); state.Dispose(); } } catch { connected = false; log("Failed sending Message!"); } }
public void sendData(byte[] data) { if (connected) { try { //send sizeinfo byte[] sizeInfo = BitConverter.GetBytes(data.Length); sendState state = new sendState(); state.socket = clientSocket; state.data = data; //send sizeinfo clientSocket.BeginSend(sizeInfo, 0, sizeInfo.Length, SocketFlags.None, new AsyncCallback(sendCallback), state); } catch { connected = false; } } else { log("Can't send Data. You are not connected to the Server."); } }