我有一个TCP侦听器,它应该接收来自客户端的一些消息,但是当我启动这个简单的服务器时,我会在server.Start()上得到一个类似于这样的错误:
通常只允许使用每个套接字地址(协议/网络地址/端口)一次
这是我正在使用的代码:
public class ServerThread
{
private readonly int port = 8000;
private readonly IPAddress ip = IPAddress.Parse("xxxx");
/// <summary>
/// constructor, create the server thread
/// </summary>
public ServerThread()
{
Thread serverThread = new Thread(new ThreadStart(serverThreadStart));
serverThread.Start();
Console.WriteLine("Server thread started!");
}
/// <summary>
/// start the server
/// </summary>
private void serverThreadStart()
{
TcpListener server = null;
try
{
server = new TcpListener(ip, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
Console.WriteLine("Waiting for client connection...");
TcpClient client = server.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes, 0, i);
data = data.ToUpper();
Byte[] msg = Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
}
// Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Debug.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
}主要的方法就是这样做:
static void Main(string[] args)
{
new ServerThread();
}发布于 2014-02-22 04:22:04
其他一些程序已经在TCP端口8000中列出。一次只能有一个程序在TCP端口上侦听。您可以更改侦听器正在使用的TCP端口,也可以计算我们的哪个程序也在端口8000上侦听并停止它。
通过从命令提示符运行netstat -a -b -p tcp,您可以发现端口上列出了哪些程序。
发布于 2014-02-22 04:32:13
必须有其他程序正在使用相同的端口,使用下面的命令,您将需要管理权限。
netstat -a -b 您也可以尝试TCPView utili。
https://stackoverflow.com/questions/21949585
复制相似问题