public SocketNetProxy(string host, int port) { var ipAddress = Dns.GetHostAddresses(host).First(); _address = new IPEndPoint(ipAddress, port); var setting = new ClientSocketSettings(BufferSize, _address); _client = new ClientSocket(setting); _client.DataReceived += DoReceived; _client.Connect(); }
/// <summary> /// init /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="heartInterval">ms</param> public SocketRemoteClient(string host, int port, int heartInterval) { IsSocket = true; var remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(host)[0], port); var settings = new ClientSocketSettings(BufferSize, remoteEndPoint); _client = new ClientSocket(settings); _client.DataReceived += OnDataReceived; _client.Disconnected += OnDisconnected; _timer = new Timer(DoCheckHeartbeat, null, 1000, heartInterval); }
public override void CheckConnect() { if (!_isConnected) { _client = new ClientSocket(setting); _client.DataReceived += DoReceived; _client.Disconnected += DoClosed; _client.Connect(); _isConnected = true; } }
private static MessageStructure DoRequest(IPEndPoint remoteEndPoint, string param, int bufferSize) { byte[] data = Encoding.UTF8.GetBytes("?d=" + param); MessageStructure ms = null; var client = new ClientSocket(new ClientSocketSettings(1024, remoteEndPoint)); using (var singal = new ManualResetEvent(false)) { client.DataReceived += (sender, e) => { ms = new MessageStructure(e.Data); singal.Set(); }; client.Connect(); client.PostSend(data, 0, data.Length); singal.WaitOne(10000);//10s client.Close(); } return ms; }
public GameServerConnection(string ip, int port, GameProxy proxy) { _ssid = Guid.NewGuid(); this.proxy = proxy; remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(ip)[0], port); ClientSocketSettings settings = new ClientSocketSettings(bufferSize, remoteEndPoint); clientSocket = new ClientSocket(settings); clientSocket.DataReceived += new SocketEventHandler(DataReceived); clientSocket.Disconnected += new SocketEventHandler(Disconnected); EnsureConnected(); _timer = new Timer(DoCheckHeartbeat, null, 1000, 30 * 1000); //30s }
private static MessageStructure DoRequest(IPEndPoint remoteEndPoint, string param, int bufferSize) { byte[] data = Encoding.UTF8.GetBytes("?d=" + param); MessageStructure ms = null; ClientSocket client = null; HttpContext context = HttpContext.Current; if (context != null && context.Session != null) { client = context.Session["CLIENT_CONNECT"] as ClientSocket; } if (client == null) { client = new ClientSocket(new ClientSocketSettings(1024, remoteEndPoint)); if (context != null && context.Session != null) { context.Session["CLIENT_CONNECT"] = client; } } var singal = new ManualResetEvent(false); client.DataReceived += (sender, e) => { ms = new MessageStructure(e.Data); singal.Set(); }; client.Connect(); client.PostSend(data, 0, data.Length); singal.WaitOne(60000);//60s return ms; }
private void OnReceived(ClientSocket sender, SocketEventArgs e) { var token = receiveEventArg.UserToken as DataToken; if (token != null) { token.SyncSegments.Enqueue(new ArraySegment<byte>(e.Data)); } receiveWaitEvent.Set(); }
public GameServerConnection(string ip, int port, GameProxy proxy) { this.proxy = proxy; remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(ip)[0], port); ClientSocketSettings settings = new ClientSocketSettings(bufferSize, remoteEndPoint); clientSocket = new ClientSocket(settings); clientSocket.DataReceived += new SocketEventHandler(DataReceived); clientSocket.Disconnected += new SocketEventHandler(Disconnected); }
public void Connect() { var tasks = new Task[200]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew((index) => { EndPoint LocalEndPoint = null; IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); using (ClientSocket client = new ClientSocket(new ClientSocketSettings(1024, remoteEndPoint), new RequestHandler(new MessageHandler()))) { try { client.IsSyncReceived = true; client.Connect(); LocalEndPoint = client.LocalEndPoint; Trace.WriteLine(string.Format("Client connect ok,{0}", LocalEndPoint)); string str = "hello" + index; byte[] data = Encoding.UTF8.GetBytes(str); if (client.PostSend(data, 0, data.Length).Wait(3 * 1000) && client.Receive(3 * 1000)) { byte[] buffer; if (client.TryReceiveBytes(out buffer)) { Assert.AreEqual(Encoding.UTF8.GetString(buffer), str); } } } catch (Exception ex) { Trace.WriteLine(string.Format("Client {0} connect error:{1} ", LocalEndPoint == null ? "" : LocalEndPoint.ToString(), ex.Message)); Assert.Inconclusive(); } } }, i); } Task.WaitAll(tasks); Trace.WriteLine("Client connect test end"); Thread.Sleep(60000); }
public void LoadTestConnect() { var runMin = 1; var times = DateTime.Now; Task.Factory.StartNew(() => { while (DateTime.Now < times.AddMinutes(runMin)) { Trace.WriteLine(string.Format("Socket connect status: TotalCount={0}, CurrentCount={1}, CloseCount={2}, RejectedCount={3}", socketListener.Status.TotalConnectCount, socketListener.Status.CurrentConnectCount, socketListener.Status.CloseConnectCount, socketListener.Status.RejectedConnectCount)); Thread.Sleep(1000); } }); while (DateTime.Now < times.AddMinutes(runMin)) { Task.Factory.StartNew(() => { EndPoint LocalEndPoint = null; IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); using (ClientSocket client = new ClientSocket(new ClientSocketSettings(1024, remoteEndPoint), new RequestHandler(new MessageHandler()))) { try { client.Connect(); } catch (Exception ex) { Trace.WriteLine(string.Format("Client {0} connect error:{1} ", LocalEndPoint == null ? "" : LocalEndPoint.ToString(), ex.Message)); Assert.Inconclusive(); } } }); Thread.Sleep(10); } Trace.WriteLine("Client connect test end"); }