public void ConnectSocket(string ip, string port) { if (socket == null && socketUDPWrite == null && socketUDPRead == null) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); endPointModel = new TripleEndPointModel() { EndPoint = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port)), }; socket.Connect(endPointModel.EndPoint); endPointModel.EndPointUDPRead = new IPEndPoint(((IPEndPoint)(socket.LocalEndPoint)).Address, (((IPEndPoint)(socket.LocalEndPoint)).Port + 200)); endPointModel.EndPointUDPWrite = new IPEndPoint(((IPEndPoint)(socket.RemoteEndPoint)).Address, (((IPEndPoint)(socket.LocalEndPoint)).Port + 1)); socketUDPWrite = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socketUDPRead = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socketUDPRead.Bind(endPointModel.EndPointUDPRead); } }
private void SocketCommandsListnerRun(Socket threadHandler, CommandParser threadCommandParser, CommandExecuter threadCommandExecuter) { var socketsModel = sockets.First(x => x.handler == threadHandler); var threadTripleEndPointModel = new TripleEndPointModel() { EndPoint = threadHandler.LocalEndPoint, EndPointUDPRead = endPointModel.EndPointUDPRead, EndPointUDPWrite = socketsModel.EndPointUDPWrite }; while (true) { while (threadHandler.Connected) { StringBuilder builder = new StringBuilder(); int bytes = 0; byte[] socketData = new byte[256]; do { try { bytes = threadHandler.Receive(socketData); } catch (Exception exc) { } builder.Append(Encoding.ASCII.GetString(socketData, 0, bytes)); bytes = 0; if (builder.ToString().Contains("\r\n")) { break; } }while (threadHandler.Connected && !builder.ToString().Contains("\r\n")); var commandString = builder.ToString(); threadHandler.ReceiveTimeout = 0; threadCommandExecuter.ContinueExecuteCommandThreading(threadHandler, threadCommandParser.ParseCommand(commandString), socketsModel.socketUDP, socketsModel.socketUDPRead, threadTripleEndPointModel); if (!threadHandler.Connected) { Console.WriteLine("this is SERVER"); return; } } } }
public void AwaitCommand(object data) { endPointModel = (TripleEndPointModel)data; while (true) { try { OpenSockets(); MonitorPort(); //MonitorPortThreads(); CloseSockets(); Console.WriteLine("\nDisconnect"); } catch (Exception exc) { ExceptionCloseSockets(); Console.WriteLine(exc.Message); Console.WriteLine(exc.StackTrace); } } }
public bool ContinueExecuteCommandThreading(Socket socket, ServerCommand command, Socket socketUDPWrite, Socket socketUDPRead, TripleEndPointModel endPoint) { switch (command.Type) { case CommandType.DownloadUDP: while (true) { if (downloadService.ContinueExecute(socket, endPoint.EndPointUDPWrite, endPoint.EndPointUDPRead, socketUDPWrite, socketUDPRead, command, ProtocolType.Udp)) { break; } } ; break; case CommandType.UploadUDP: while (true) { if (uploadService.ContinueExecute(socket, endPoint.EndPointUDPRead, endPoint.EndPointUDPWrite, socketUDPRead, socketUDPWrite, command, ProtocolType.Udp)) { break; } } ; break; } return(false); }
public void ExecuteCommand(Socket socket, ServerCommand command, Socket socketUDPWrite, Socket socketUDPRead, TripleEndPointModel endPoint) { switch (command.Type) { case CommandType.Echo: baseCommandService.EchoHandler(socket, command); return; case CommandType.Time: baseCommandService.TimeHandler(socket); return; case CommandType.Close: baseCommandService.CloseHandler(socket, socketUDPRead, socketUDPWrite); return; case CommandType.Download: downloadService.DownloadFile(socket, endPoint.EndPoint, socketUDPWrite, command, ProtocolType.Tcp); return; case CommandType.Upload: uploadService.UploadFile(socket, endPoint.EndPoint, socketUDPRead, command, ProtocolType.Tcp); return; case CommandType.DownloadUDP: downloadService.DownloadFile(socket, endPoint.EndPointUDPWrite, socketUDPWrite, command, ProtocolType.Udp); return; case CommandType.UploadUDP: uploadService.UploadFile(socket, endPoint.EndPointUDPRead, socketUDPRead, command, ProtocolType.Udp); return; case CommandType.Unknown: baseCommandService.UnknownHandler(socket); return; } }