私は多くの検索をしましたが、私の問題ではあまり運がありませんでした。私はネットワークプログラミングを初めて学び、学びたいと思っています。簡単なサーバーをセットアップしようとしました「各ソケットアドレス(プロトコル/ネットワークアドレス/ポート)の1つの使用法のみが通常許可されています」というエラーを修正するにはどうすればよいですか?
私が抱えている問題は、サーバー上でTcpListenerを起動しようとすると、「各ソケットアドレス(プロトコル/ネットワークアドレス/ポート)の1つの使用しか通常許可されていません」例外が発生する
私はファイアウォールを無効にしようとしました。使用するポートを変更し、変数を移動しても役に立たなくしました。(クライアントはうまく動作しますが、起動できないため明らかにサーバーを見つけることができません。
私はSocket.Poll()の使用方法について説明していますが、TcpListenerオブジェクトしか使用していないため、どのようにPoll関数を使用するかわかりません。
マイコード:私の主な方法で
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));
}
}
}
}
:
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();
}
}
}
すべてのヘルプは非常歓迎です!
'mingw-w64'には、' closesocket() 'が必要であり、' close() 'ではポートを解放する必要があります。 – Jeroen