public class Server
{
private static byte[] result = new byte[1024];
//Socket server=null;
public static string text1;
public static string WifiListen()
{
//result = Encoding.UTF8.GetBytes("This is a test");
IPAddress localip = IPAddress.Parse("192.168.0.106");
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(localip, 57827));
server.Listen(10);
while (true)
{
Socket client = server.Accept();//这里跑不下去了
client.Send(Server.result);
Socket clientserver = client;
while (true)
{
try
{
int information = clientserver.Receive(result); //return text1;
text1 = "客户IP:" + clientserver.RemoteEndPoint.ToString();
}
catch (Exception ex)
{
clientserver.Shutdown(SocketShutdown.Both);
clientserver.Close();
text1 = "close";
break;
}
}
return text1;
}
}
}
运行报错补充:代码段中的ip地址是我局域网给电脑分配的地址,ESP8266也连接在这个局域网中,但是使用这个ip地址会报错System.Net.Sockets.SocketException: 'The requested address is not valid in this context';如果使用0.0.0.0的ip地址不会报错,但是ESP仍旧无法连接,程序会在Accept()那个位置一直等待