public bool connect() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); socket.NoDelay = true; @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeByte('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); throw new IOException("Invalid ack msg : " + line); //return false; } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); throw new IOException("Invalid ack msg : " + line); //return false; } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } if (this.userId.Length > 0 && this.password.Length > 0) { login(); } if (this.initialScript != "") { run(initialScript); } return(true); }
public virtual bool tryReconnect() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeByte('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); return(false); } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); return(false); } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } return(true); }
public bool connect(string hostName, int port) { lock (threadLock) { try { if (sessionID.Length > 0) { return(true); } this.hostName = hostName; this.port = port; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); socket.NoDelay = true; @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeChar('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); return(false); } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); return(false); } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } return(true); } catch (Exception ex) { throw ex; } } }