using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Threading; namespace StressClient { class DotnetSocketClient { private string _clientId = ""; private string _svr_ip = "127.0.0.1"; private int _svr_port = 8081; System.Net.Sockets.Socket _socket; string _last_req_time = ""; public DotnetSocketClient(string clientId) { _clientId = clientId; } // 实例化socket private void InitConnection() { try { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(_svr_ip), _svr_port); _socket.Connect(ipEndPoint); } catch (Exception) { } } public void Run() { try { string message = string.Format("{0} 客户端开始运行", _clientId); System.Console.WriteLine(message); while (true) { // 发送数据 //_socket.Send(Encoding.Default.GetBytes("dddd")); System.Threading.Thread.Sleep(2000); } //_socket.Shutdown(SocketShutdown.Both); // _socket.Close(); } catch (Exception) { } } } }