Como faço para corrigir o erro “Apenas um uso de cada endereço de soquete (protocolo / endereço de rede / porta) é normalmente permitido”?
Eu fiz um monte de googling, mas não tive muita sorte com meus problemas. Eu sou novo em programação de rede e tentando aprender, eu tentei configurar um servidor simples e cliente que se comunicam (seguindo um tutorial on-line localizado aqui ->http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server)
O problema que estou tendo é que eu continuo recebendo a exceção "Apenas um uso de cada endereço de soquete (protocolo / endereço de rede / porta) é normalmente permitido" ao tentar iniciar o TcpListener no servidor.
Eu tentei desativar meu firewall, alterando a porta a ser usada, movendo variáveis ao redor, mas sem sucesso (o cliente funciona bem, mas obviamente não consegue encontrar o servidor porque não consigo ativá-lo).
Eu vi soluções descrevendo o uso de Socket.Poll (), mas desde que eu estou usando apenas o objeto TcpListener, não tenho idéia de como fazer uso da função de pesquisa.
Meu código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
namespace ServerTutorial {
class Server {
private readonly Thread m_listenThread;
public Server() {
m_listenThread = new Thread(new ThreadStart(ListenForClients));
m_listenThread.Start();
}
public void ListenForClients() {
var listener = new TcpListener(IPAddress.Any, 3000);
listener.Start();
while (true) {
//Blocks until a client has connected to the server
TcpClient client = listener.AcceptTcpClient();
//Send a message to the client
var encoder = new ASCIIEncoding();
NetworkStream clientStream = client.GetStream();
byte[] buffer = encoder.GetBytes("Hello Client!");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
//Create a thread to handle communication with the connected client
var clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(client);
}
}
private void HandleClient(object clientObj) { //Param thread start can only accept object types, hence the cast
var client = (TcpClient) clientObj;
NetworkStream clientStream = client.GetStream();
var message = new byte[4096];
while (true) {
int bytesRead = 0;
try {
//Block until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
} catch {
//A socket error has occurred
System.Diagnostics.Debug.WriteLine("A socket error has occured");
break;
}
if (bytesRead == 0) {
//The client has disconnected from the server
System.Diagnostics.Debug.WriteLine("A client has disconnected from the server");
client.Close();
break;
}
//Message has been received
var encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
}
}
}
}
No meu método principal:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerTutorial {
class Program {
static void Main(string[] args) {
var server = new Server();
server.ListenForClients();
}
}
}
Qualquer ajuda é muito apreciada!