无法将类型 string 隐式转换为 System.NET.IPAddress

junsanrao 2008-09-09 03:20:34
这是什么意思啊,我要监听一台机的端口,代码如下:
private void listen()
{
bool listenerRun=true;
Regex rx = new Regex(@"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}");
Match mh = rx.Match(textBox1.Text, 0, textBox1.Text.Length);
if (!mh.Success)
{
MessageBox.Show("你所输入的不是正确的IP信息!", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
}
try
{
IPAddress ipAddress = textBox1.Text;
TcpListener tcp = new TcpListener(ipAddress,80);
tcp.Start();
statusStrip1.Text = "正在监听......";
while (listenerRun)
{
Socket s = tcp.AcceptSocket();
Byte[] stream = new Byte[80];
int i = s.Receive(stream);
string message = Encoding.UTF8.GetString(stream);
richTextBox1.AppendText(message);

}
}
catch(System.Security.SecurityException)
{
MessageBox.Show("防火墙安全错误!", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
statusStrip1.Text = "已经停止监听...";
}
}

就是要监听输入在 textBox1.Text这个IP的,出现在上述错误,不会解决,请高手帮忙,谢谢!!
...全文
1839 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeason_jun 2008-09-09
  • 打赏
  • 举报
回复
意思是不能监听别人机子上的端口了?
wdgphc 2008-09-09
  • 打赏
  • 举报
回复
看MSDN

IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
TcpListener tcpListener = new TcpListener(ipAddress, 80);

这样就监听了啊. 你说的程序停止了是对了啊,直到就别的机器给你的80端口发信息的时候,程序才继续往下走呢.

所有一般都把监听处理放在一个线程了.
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
楼上这位大哥说的极是,可是我把自己做为监听对象,你看下代码,我输入自己的IP和端口,怎么会错呢,没反应也,你能解决不,等你,谢谢
wdgphc 2008-09-09
  • 打赏
  • 举报
回复
老兄,监听只能监听你自己的端口(监听有没有人叫你,而不能监听别人的端口). 别人的端口只是你发送数据的目标.
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
高手,我单步运行的话就在你说的地方不动了,怎么办
错在这tcp.Start(); 说请求的地址无效,可是这个地址就是坐在我前面的那个同事的IP啊,怎么会无效呢
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
是吗,就这出问题了,那我单步运行看下,谢谢你啊
CCjian 2008-09-09
  • 打赏
  • 举报
回复
while (listenerRun)
{
Socket s = tcp.AcceptSocket();
Byte[] stream = new Byte[80];
int i = s.Receive(stream);
string message = Encoding.UTF8.GetString(stream);
richTextBox1.AppendText(message);

}
这里在等着呢?怎么还能响应你别的操作?
那就要多线程了...
Wesley 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 junsanrao 的回复:]
谢谢,有见地,果然就拿到了,可是又出现另外一个错误,看看你能不能再帮下我
监听其它人的IP呢就显示“在其上下文中,该请求的地址无效。”
监听自己的IP呢就死了,不动,再点就没有响应是怎么回事啊
[/Quote]
既然是监听,就来了个接一个呗,你这个没有来,你怎么进行监听?
Wesley 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sinoknight 的回复:]
试试这个

IPAddress add = IPAddress.Parse( textBox1.Text);
[/Quote]
这样才可以
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
谢谢,有见地,果然就拿到了,可是又出现另外一个错误,看看你能不能再帮下我
监听其它人的IP呢就显示“在其上下文中,该请求的地址无效。”
监听自己的IP呢就死了,不动,再点就没有响应是怎么回事啊
Ador3 2008-09-09
  • 打赏
  • 举报
回复
你用的那个?
IPAddress ipAddress = new IpAddress(textBox1.Text);这个势必是错的!
IPAddress add = IPAddress.Parse( textBox1.Text);

huing 2008-09-09
  • 打赏
  • 举报
回复
string hostIp = textBox1.Text.trim();
IPAddress addr = IPAddress.Parse(hostIp);
baihe_591 2008-09-09
  • 打赏
  • 举报
回复

IPAddress ip =IPAddress .Parse (textBox1.Text);
sinoknight 2008-09-09
  • 打赏
  • 举报
回复
用IPAddress ipAddress = new IpAddress(textBox1.Text)的方法是肯定不行的,因为构造函数要求的参数是long或者byte[]等等,没有string形式的,如果要直接接受textBox1.Text, 请使用

IPAddress add = IPAddress.Parse( textBox1.Text);
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
高手,又提示这样的错误,呵呵,帮人帮到底吧

无法从“string”型转换成“long”型哦
panw520 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 rqx110 的回复:]
引用 2 楼 hecong875 的回复:
IPAddress ipAddress = new IpAddress(textBox1.Text);

[/Quote]
junsanrao 2008-09-09
  • 打赏
  • 举报
回复
咦,还是不行,说无法从“string”型转换成“long”型哦
rqx110 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hecong875 的回复:]
IPAddress ipAddress = new IpAddress(textBox1.Text);
[/Quote]
sinoknight 2008-09-09
  • 打赏
  • 举报
回复
试试这个

IPAddress add = IPAddress.Parse( textBox1.Text);
hecong875 2008-09-09
  • 打赏
  • 举报
回复
IPAddress ipAddress = new IpAddress(textBox1.Text);
加载更多回复(1)

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧