我想监听某机子的某端口,代码如下
this.MyServer = new System.Net.IPEndPoint(this.MyIP,Convert.ToInt32(this.textBox2.Text));
this.Sock = new Socket.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
this.Sock.Bind(this.MyServer);
this.Sock.Listen(50);但是出现如下问题:
在其上下文中,请求的地址无效这是什么错误那
this.MyServer = new System.Net.IPEndPoint(this.MyIP,Convert.ToInt32(this.textBox2.Text));
this.Sock = new Socket.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);
this.Sock.Bind(this.MyServer);
this.Sock.Listen(50);但是出现如下问题:
在其上下文中,请求的地址无效这是什么错误那
this.MyServer = new System.Net.IPEndPoint(IPAddress.Any
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class SynchronousSocketListener
{
public static string data = null; public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());//主机名
Console.WriteLine(ipHostInfo.HostName);
IPAddress ipAddress = ipHostInfo.AddressList[0];
Console.WriteLine(ipAddress);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress,3389); Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp ); try
{
listener.Bind(localEndPoint);
listener.Listen(10); while (true)
{
Console.WriteLine("Waiting for a onnection...");
Socket handler = listener.Accept();
data = null; while (true)
{
bytes = new byte[1024];
string x ="aaaaa<EOF>";
Console.WriteLine(x); bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(x.ToCharArray());
foreach(byte y in bytes)
{
Console.WriteLine(y.ToString());
} int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
} Console.WriteLine( "Text received : {0}", data); byte[] msg = Encoding.ASCII.GetBytes(data); handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
} Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
} public static int Main(String[] args)
{
StartListening();
return 0;
}
}但是还是不可以,真奇怪
起步可真困难啊
其他人怎么就没人帮忙了
太简单了吗