连接到另一个IP Windows窗体

编程入门 行业动态 更新时间:2024-10-27 11:21:55
本文介绍了连接到另一个IP Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这有效:

this works:

string serverIP = "localhost"; string message = "Hello"; Connect(serverIP, message);

但是当我用一个我的ip地址字符串替换localhost部分时像:12.23.21.256,它告诉我: 由于目标机器主动拒绝它,因此无法建立连接。 在连接中我有: static void连接(字符串serverIP,字符串消息) { string output =; 尝试 { //创建一个TcpClient。 //客户端需要一个连接到服务器和端口 //组合所指定的同一地址的TcpServer //。 Int32 port = 13; TcpClient client = new TcpClient(serverIP,port); //将传递的消息转换为ASCII并将其存储为字节数组。 Byte [] data = new Byte [256]; data = System.Text.Encoding.ASCII .GetBytes(消息); //获取用于读写的客户端流。 //流stream = client.GetStream(); NetworkStream stream = client.GetStream(); //将消息发送到连接的TcpServer。 stream.Write(data,0,data.Length); output =已发送:+消息; MessageBox.Show(输出); //用于存储响应字节的缓冲区。 data = new Byte [256 ]; //存储响应ASCII表示的字符串。 字符串responseData = String.Empty; //读取第一批TcpServer响应字节。 Int32 bytes = stream.Read(data,0,data.Length); responseData = System.Text.Encoding.ASCII.GetString(data,0,bytes); output =Received:+ responseData; MessageBox.Show (输出); //关闭所有内容。 stream.Close(); client.Close (); } catch(ArgumentNullException e) { output =ArgumentNullException:+ e; MessageBox.Show(输出); } catch(SocketException e) { output =SocketException:+ e.ToString(); MessageBox.Show(输出); } }

but when i replace the "localhost" part with a string of my ip address like: "12.23.21.256", it tells me: "No connection could be made because the target machine actively refused it". In connect I have: static void Connect(string serverIP, string message) { string output = ""; try { // Create a TcpClient. // The client requires a TcpServer that is connected // to the same address specified by the server and port // combination. Int32 port = 13; TcpClient client = new TcpClient(serverIP, port); // Translate the passed message into ASCII and store it as a byte array. Byte[] data = new Byte[256]; 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); output = "Sent: " + message; MessageBox.Show(output); // 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); output = "Received: " + responseData; MessageBox.Show(output); // Close everything. stream.Close(); client.Close(); } catch (ArgumentNullException e) { output = "ArgumentNullException: " + e; MessageBox.Show(output); } catch (SocketException e) { output = "SocketException: " + e.ToString(); MessageBox.Show(output); } }

推荐答案

您的计算机可能已阻止从给定端口连接到12.23.21.256 IP地址 读取在Windows防火墙中打开一个端口 [ ^ ]并打开给定的帖子再试一次,或者你可以通过临时的,你的f irewall和防病毒软件可以阻止你的端口。 your computer may have blocked conneting to 12.23.21.256 IP address from given port read Open a port in Windows Firewall[^] and open given post and try again, or you can test by temparary disssableing your firewall and antivirus software which can block your ports.

更多推荐

连接到另一个IP Windows窗体

本文发布于:2023-11-11 06:39:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   连接到   IP   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!