捷作服务器 修改套接字端口,如何解决错误“每个套接字地址(协议/网络地址/端口)通常只允许使用一次”?...
我遇到的问题是,我保持获取异常“尝试启动服务器上的TcpListener时,通常只允许每个套接字地址(协议/网络地址/端口)的一个用法。
我试过禁用我的防火墙,更改要使用的端口,移动变量但无济于事(客户端正常工作,但显然无法找到服务器,因为我无法启动它)。
我见过描述使用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();
}
}
}
任何帮助,非常感激!
+0
注意''mingw-w64'你需要'closesocket()'而不是'close()'来释放端口。 –
捷作服务器 修改套接字端口,如何解决错误“每个套接字地址(协议/网络地址/端口)通常只允许使用一次”?...相关推荐
- 传奇世界服务器端口被占用,Windows socket error: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 (10048), on API 'bi...
Windows socket error: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次. (10048), on API 'bind' 你们的系统80%都是XP的,这个就是XP系统的不适 ...
- python request大批量发送请求调用接口时,报错:[WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
接到一个项目需求,其中需要调用到供应商的Http API,因为有大量的测试资源,所以代码中会循环调用API. 然而在测试代码执行过程中,过程中偶尔报错: 此时看到报错,怀疑是可能是同时并发的问题, ...
- OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次. import socket s = socket.socket(socket.AF_IN ...
- 解决[WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
1 遇到问题 执行ryu控制器的python程序时,出现下列问题: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次 2 问题解决 由于在windows下ry ...
- Python之一行代码解决这个错误:OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
记录一下这个错误,也分享一下解决方法给大家. 今天偶然从一本书上获取到了解决的答案,一行代码搞定. 贴一下实例代码: s = socket.socket(socket.AF_INET,socket.S ...
- 程序报“通常每个套接字地址(协议/网络地址/端口)只允许使用一次”
产生的原因:你的udp开了以后没有正确关掉,端口就被占用了,记得用完了释放 1.打开的"CMD" 2.输入 netstat -aon|findstr 5037 5037是端口号,可 ...
- socket:通常每个套接字地址(协议/网络地址/端口)只允许使用一次
有两种解决方法,一种是加入try{}catch(){},程序就不会抱错了:一种是在设置好监听SOCKET后,将SOCKET的属性设置为可重复使用地址,如: //创建监听SOCKET socketLis ...
- 前端学习(1856)vue之电商管理系统电商系统之安装mysql出现mysql报错:Can’t start server: Bind on TCP/IP port: 通常每个套接字地址(协议/网络地址
2020-07-26T11:44:29.778919Z 0 [ERROR] [MY-010262] [Server] Can't start server: Bind on TCP/IP port: ...
- [解决]通常每个套接字地址只允许使用一次
我们在做socket的开发的时候,经常会遇到这个错误:通常每个套接字地址 (协议/网络地址/端口)只允许使用一次 错误指示很明确:通信端口被占用了,导致通信无法进行. 但为什么端口被占用了呢?什么端口 ...
最新文章
- linux 系统时间是在哪里记录的,Linux系统如何记录时间
- Nginx原子操作及自旋锁实现
- 网络工程师HCIE-RS-路由回馈问题(通俗易懂!)
- 上传multipart文件
- php动态网站程序设计课后答案,《PHP动态网站开发实例教程》课程标准
- 华硕升级bios的问题
- ESP32-C3 SPI salve示例错误
- 大学毕业生参考信函提示
- Largenbsp;Scalenbsp;Dist…
- c语言switch case解释,C语言switch case语句详解
- 新一代垃圾回收器—ZGC
- 企业中一些职位的简称
- 中国内地城市CA分布
- 视频里面的水印怎么添加
- 配置SQL Server 2016无域AlwaysOn
- JASS代码加翻译更新(第六篇)
- 钛资本研究院:未来三到五年,中国企业服务赛道将呈现黄金时代的基础
- 创业初期获取种子用户的7种方法
- 办公室文员必备python神器,将PDF文件表格转换成excel表格!
- 小白必看:谈一下兼阅万怎么做才容易上手