public void SendMessage(TcpMessage msg, IPEndPoint epRemote) { soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); soc.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true); soc.SendBufferSize = sendBuffersize; try { soc.BeginConnect(epRemote as EndPoint, new AsyncCallback(MessageCallBack), msg); } catch (Exception ex) { soc.Close(); if (SendFail != null) { SendFail(String.Format("Send Failed! {0}", ex.Message)); } } }
private void MessageCallBack(IAsyncResult aResult) { try { soc.EndConnect(aResult); TcpMessage msg = (TcpMessage)aResult.AsyncState; soc.SendFile(msg.filePath, msg.fileinfobuffer, null, TransmitFileOptions.UseDefaultWorkerThread); FileSentFinish(); } catch (Exception ex) { if (SendFail != null) { SendFail(String.Format("Send Failed! {0}", ex.Message)); } } finally { soc.Shutdown(SocketShutdown.Both); soc.Disconnect(false); soc.Close(); } }