TcpClient如何指定某个IP时行链接
我的机器有多个IP地址,我想用其中一个进行TcpClient链接.C#中如何实现?多谢 --------------------编程问答-------------------- static void Connect(String server, String message){
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
--------------------编程问答-------------------- 应该取决于你server的地址吧,系统会默认取与server地址同一个网段的ip吧 --------------------编程问答-------------------- 应该是指多IP接受端的问题吧,发送出信息使用哪个IP的问题。。
这个没有什么概念。。。 --------------------编程问答-------------------- 恩,不是很清楚,楼上的可能是对的 --------------------编程问答-------------------- 比如你有两个IP (192.168.0.1 和 10.0.0.1) 那么你的TCP开放后, 192.168.0.2 连接上时就用的192.168.0.1 ,而10.0.0.2连接时就用的10.0.0.1.
如果你不想10.0.0.1网段的连接,那么只需查询RomateIP为10.0.0.X时断开或跳过即可. --------------------编程问答-------------------- --------------------编程问答-------------------- 这方面的东西太多了!!
去cnblogs搜搜!!
补充:.NET技术 , C#