public void Connect(TCPAddress address) { IPEndPoint ipe = new IPEndPoint(address.IP, address.Port); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(ipe); }
/// <summary> /// 从收到的 route_bytes 中获取需与下级服务器通信相关内容 /// 本方法不产生 socket 通信 /// </summary> /// <param name="recv_bytes">接收到来自client的路由数据</param> /// <param name="address">返回代理下级服务器地址</param> /// <param name="is_aim_proxy">代理下级服务器是否为代理 (决定socket_ep包头)</param> /// <param name="bytes_to_send">需向下级代理发送的内容</param> private void GetAimInfo(byte[] recv_bytes, out TCPAddress address, out bool is_aim_proxy, out byte[] bytes_to_send) { if (recv_bytes[0] == 1) { byte count = recv_bytes[1]; if (count == 0) { address = TCPAddress.FromBytes(recv_bytes, 2); is_aim_proxy = false; bytes_to_send = new byte[recv_bytes.Length - 8]; Array.Copy(recv_bytes, 8, bytes_to_send, 0, bytes_to_send.Length); } else { address = TCPAddress.FromBytes(recv_bytes, 8); is_aim_proxy = true; bytes_to_send = new byte[recv_bytes.Length - 6]; Array.Copy(recv_bytes, bytes_to_send, 8); Array.Copy(recv_bytes, 14, bytes_to_send, 8, bytes_to_send.Length - 8); bytes_to_send[1] = (byte)(count - 1); } return; } address = new TCPAddress(); is_aim_proxy = false; bytes_to_send = new byte[0]; }
public void Connect(TCPAddress address, int send_timeout, int recv_timeout) { IPEndPoint ipe = new IPEndPoint(address.IP, address.Port); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.SendTimeout = send_timeout; client.ReceiveTimeout = recv_timeout; client.Connect(ipe); }
/// <summary> /// 解析UI字符串至 ConnectionRoute 对象 /// 字符串格式: /// server : IPx or IPx-name_x /// proxy : [empty] or IP1-name_1;IP2;IP3-name_3;...... /// 解析规则: /// IPn-name_n 解析为两个 RouteNode, 按顺序分别为 IPn 和 IPn-name_n /// IPn 解析为一个 RouteNode, 其 Name 为 "" /// server 若为挂在反向代理的server, 则代理列表最后一级为反向代理服务器IP /// 解析规则保证 ProxyRoute[0].Name == "" (若存在), 即一定不是反向代理 /// </summary> /// <param name="server_string"></param> /// <param name="proxy_string"></param> /// <returns></returns> public static ConnectionRoute FromString(string server_string, string proxy_string, int default_server_port = 12138, int default_proxy_port = 12139) { ConnectionRoute cr = new ConnectionRoute(); if (server_string.Contains("-")) { string[] strs = server_string.Split('-'); if (!strs[0].Contains(':')) { strs[0] += ":" + default_server_port.ToString(); } cr.ServerAddress = new RouteNode(strs[0], strs[1]); cr.ProxyRoute.Add(new RouteNode(strs[0])); } else { if (!server_string.Contains(':')) { server_string += ":" + default_server_port.ToString(); } cr.ServerAddress = new RouteNode(TCPAddress.FromString(server_string)); } if (!string.IsNullOrEmpty(proxy_string)) { string[] proxies = proxy_string.Split(';'); int idx = 0; foreach (string proxy0 in proxies) { if (proxy0.Contains("-")) { string[] proxy0_split = proxy0.Split('-'); if (!proxy0_split[0].Contains(':')) { proxy0_split[0] += ":" + default_proxy_port.ToString(); } cr.ProxyRoute.Insert(idx, new RouteNode(proxy0_split[0])); idx++; cr.ProxyRoute.Insert(idx, new RouteNode(proxy0_split[0], proxy0_split[1])); idx++; } else { string proxy_str = proxy0; if (!proxy_str.Contains(':')) { proxy_str += ":" + default_proxy_port.ToString(); } cr.ProxyRoute.Insert(idx, new RouteNode(proxy0)); idx++; } } } return(cr); }
public static TCPAddress FromBytes(byte[] bytes, ref int index) { TCPAddress address = new TCPAddress { IP = new IPAddress(new byte[4] { bytes[index + 0], bytes[index + 1], bytes[index + 2], bytes[index + 3] }), Port = (((int)bytes[index + 4]) << 8) + (int)bytes[index + 5] }; index += 6; return(address); }
public void ConnectWithTimeout(TCPAddress address, int timeout) { cth.Reset(); IPEndPoint ipe = new IPEndPoint(address.IP, address.Port); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.BeginConnect(ipe, asyncResult => { try { cth.IsSuccess = false; if (asyncResult.AsyncState is Socket s) { s.EndConnect(asyncResult); cth.IsSuccess = true; } } catch (Exception ex) { cth.IsSuccess = false; cth.ConnectException = ex; } finally { cth.Set(); } }, client); if (cth.WaitOne(timeout, false)) { if (cth.IsSuccess) { return; } else { throw cth.ConnectException; } } else { client.Close(); throw new TimeoutException("Connection timeout"); } }
public override bool Equals(object obj) { TCPAddress addr = obj as TCPAddress; return(this.IP.Equals(addr.IP) && this.Port.Equals(addr.Port)); }
public SocketClient(TCPAddress tcpAddress) { HostAddress = tcpAddress.Copy(); this.GetHeaderBytesFunc = this.GetHeaderBytesWithProxy; }