代码如下:
try
{
IPAddress serverAddress;
serverAddress = IPAddress.Parse( "192.168.0.137 ");
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(serverAddress, 8001);
bool a = clientSocket.Connected;
clientSocket.Connect(iep);
byte[] buffer = new byte[3];
buffer[0] = 50;
buffer[1] = 51;
buffer[2] = 52;
clientSocket.Send(buffer);
byte[] buffer2 = new byte[3];
clientSocket.Receive(buffer2);
// this.labelReceiveText.Text = System.Text.ASCIIEncoding.ASCII.GetString(buffer2, 0, 3);
System.Windows.Forms.MessageBox.Show( "Conect Ok ");
}
catch
{
System.Windows.Forms.MessageBox.Show( "连接失败 ");