问题描述
我做了很多谷歌搜索,但我的问题没有太多的运气。我是网络编程的新手,试图学习,我试图设置一个简单的服务器&客户端进行沟通(遵循这里的在线教程 - > )我遇到的问题是我继续获取异常当尝试在服务器上启动TcpListener时,通常只允许使用每个套接字地址(协议/网络地址/端口)。
I我试图禁用我的防火墙,改变要使用的端口,移动变量,但无济于事(客户端工作正常,但显然找不到服务器,因为我无法启动它)。
我已经看到了描述使用Socket.Poll()的解决方案,但由于我只使用TcpListener对象,我不知道如何使用Poll函数。
我的代码:
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net.Sockets;
使用System.Net;
使用System.Threading;
使用System.Text;
命名空间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){
//阻止直到客户端连接到服务器
TcpClient client = listener.AcceptTcpClient();
//向客户端发送消息
var encoder = new ASCIIEncoding();
NetworkStream clientStream = client.GetStream();
byte [] buffer = encoder.GetBytes(Hello Client!);
clientStream.Write(buffer,0,buffer.Length);
clientStream.Flush();
//创建一个线程来处理与连接的客户端的通信
var clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(client);
}
}
private void HandleClient(object clientObj){// Param线程启动只能接受对象类型,因此转换
var client =(TcpClient) clientObj;
NetworkStream clientStream = client.GetStream();
var message = new byte [4096];
while(true){
int bytesRead = 0;
try {
//阻止客户端发送消息
bytesRead = clientStream.Read(message,0,4096);
} catch {
//发生套接字错误
System.Diagnostics.Debug.WriteLine(套接字错误已发生);
break;
}
if(bytesRead == 0){
//客户端与服务器断开连接
System.Diagnostics.Debug.WriteLine(A client has断开与服务器);
client.Close();
break;
}
//已收到消息
var encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message,0,bytesRead));
}
}
}
}
我的主要方法:
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间ServerTutorial {
类程序{
static void Main(string [] args){
var server = new Server();
server.ListenForClients();
}
}
}
任何帮助都非常感谢!
ListenForClients
正在被调用两次(在两个不同的线程上)一次从构造函数中,一次从显式方法调用 Main
。当 TcpListener
的两个实例尝试在同一个端口上侦听时,您会收到该错误。
I've done a lot of googling but not had much luck with my issues. I am new to network programming and trying to learn, I've attempted to set up a simple server & client that communicate (following an online tutorial located here -> http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server)
The issue I'm having is that I keep getting the exception "Only one usage of each socket address (protocol/network address/port) is normally permitted" when trying to start the TcpListener on the server.
I've tried disabling my firewall, changing the port to be used, moving variables around but to no avail (the client works fine, but it obviously can't find the server because I cannot launch it).
I've seen solutions describing the use of Socket.Poll() but since I'm only using the TcpListener object, I have no idea how to make use of the Poll function.
My code:
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));
}
}
}
}
In my main method:
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();
}
}
}
Any help is hugely appreciated!
ListenForClients
is getting invoked twice (on two different threads) - once from the constructor, once from the explicit method call in Main
. When two instances of the TcpListener
try to listen on the same port, you get that error.
这篇关于如何解决错误“通常仅允许每个套接字地址(协议/网络地址/端口)的一个使用”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!