日期:2014-05-18 浏览次数:21059 次
TcpClient client; const int portNO = 60000; const int localport = 2290; private void button1_Click(object sender, EventArgs e) { if (client != null) { client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); //client.Close(); //socketListen.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } //else //{ IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, localport); client = new TcpClient(ipLocalEndPoint); client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); client.Connect("127.0.0.1", portNO); string msg = "LOGEIN"; byte[] buffer = System.Text.Encoding.Default.GetBytes(msg); int i = client.Client.Send(buffer); Console.ReadLine(); //} } private void button2_Click(object sender, EventArgs e) { client.Client.Disconnect(true); //client.Dispose(); client.Close(); }