每个套接字地址只有一种用法(Only one usage of each socket address)

编程入门 行业动态 更新时间:2024-10-28 09:20:05
每个套接字地址只有一种用法(Only one usage of each socket address)

这是我第一次使用Windows窗体应用程序来执行套接字服务器/客户端。

我的public void Listen()下我的Socket.bind(IPEndpoint)遇到了问题。 我收到了错误

通常只允许使用每个套接字地址(协议/网络地址/端口)

我有什么想法可以解决这个问题吗?

public Form1() { CheckForIllegalCrossThreadCalls = false; InitializeComponent(); } Socket sck; Socket acc; IPEndPoint ipe; List<Socket> lstClient = new List<Socket>(); Thread handleClient; string myIP = ""; public void IP() { IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress address in host.AddressList) { if (address.AddressFamily.ToString() == "InterNetwork") { myIP = address.ToString(); } } ipe = new IPEndPoint(IPAddress.Parse(myIP), 2001); sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); } private void btnListen_Click(object sender, EventArgs e) { this.Form1_Load(sender, e); } private void rtbMain_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = true; } private void Form1_Load(object sender, EventArgs e) { IP(); handleClient = new Thread(new ThreadStart(Listen)); handleClient.IsBackground = true; handleClient.Start(); } public void Listen() { sck.Bind(ipe); sck.Listen(3); while(true) { Socket acc=sck.Accept(); lstClient.Add(acc); Thread clientProcess = new Thread(myThreadClient); clientProcess.IsBackground = true; clientProcess.Start(acc); rtbMain.SelectionFont = new Font("Arial", 18, FontStyle.Bold); rtbMain.SelectionColor = Color.Green; rtbMain.AppendText("accept connections from" + acc.RemoteEndPoint.ToString()); rtbMain.ScrollToCaret(); } } private void myThreadClient(object obj) { Socket clientACC = (Socket)obj; while(true) { byte[] sizeBuf = new byte[1024]; int rec = clientACC.Receive(sizeBuf); foreach (Socket acc in lstClient) { acc.Send(sizeBuf,sizeBuf.Length,SocketFlags.None); } } }

This is the first time I have used Windows Form application to do a socket server / client.

I have ran into a problem with my Socket.bind(IPEndpoint) under my public void Listen(). I get the error

Only one usage of each socket address (protocol/network address/port) is normally permitted

Any ideas how I could go about fixing this?

public Form1() { CheckForIllegalCrossThreadCalls = false; InitializeComponent(); } Socket sck; Socket acc; IPEndPoint ipe; List<Socket> lstClient = new List<Socket>(); Thread handleClient; string myIP = ""; public void IP() { IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress address in host.AddressList) { if (address.AddressFamily.ToString() == "InterNetwork") { myIP = address.ToString(); } } ipe = new IPEndPoint(IPAddress.Parse(myIP), 2001); sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); } private void btnListen_Click(object sender, EventArgs e) { this.Form1_Load(sender, e); } private void rtbMain_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = true; } private void Form1_Load(object sender, EventArgs e) { IP(); handleClient = new Thread(new ThreadStart(Listen)); handleClient.IsBackground = true; handleClient.Start(); } public void Listen() { sck.Bind(ipe); sck.Listen(3); while(true) { Socket acc=sck.Accept(); lstClient.Add(acc); Thread clientProcess = new Thread(myThreadClient); clientProcess.IsBackground = true; clientProcess.Start(acc); rtbMain.SelectionFont = new Font("Arial", 18, FontStyle.Bold); rtbMain.SelectionColor = Color.Green; rtbMain.AppendText("accept connections from" + acc.RemoteEndPoint.ToString()); rtbMain.ScrollToCaret(); } } private void myThreadClient(object obj) { Socket clientACC = (Socket)obj; while(true) { byte[] sizeBuf = new byte[1024]; int rec = clientACC.Receive(sizeBuf); foreach (Socket acc in lstClient) { acc.Send(sizeBuf,sizeBuf.Length,SocketFlags.None); } } }

最满意答案

如果套接字已经在应用程序的任何其他进程中的同一端口中侦听,则可能会发生这种情

只需确保没有应用程序使用它,并确保您不运行同一应用程序的多个实例(使用taskmanager验证它)。


在旁边:

切勿使用CheckForIllegalCrossThreadCalls = false 有意义的方法名称。

将IP名称命名为IP ,没有意义。 我宁愿把它命名为GetIPAndCreateSocket或类似的东西。

方法名称应该告诉我们该方法将要做什么。

This could happen if a socket is already listening in same port in your application any other process.

Just make sure no apps us it, and make sure you don't run multiple instances of your same application(verify it with taskmanager).


Aside:

Never use CheckForIllegalCrossThreadCalls = false Have meaningful method names.

Your IP method doesn't makes sense to name it as IP. I'd rather name it as GetIPAndCreateSocket or something like that.

Method name should tell us what that method is going to do.

更多推荐

本文发布于:2023-07-08 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地址   usage   address   socket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!