我如何修复错误"每个套接字地址(协议/网络地址/端口)的一个用法通常被允许"? [英] How do i fix the error "Only one usage of each socket address (protocol/network address/port) is normally permitted"?

查看:2627
本文介绍了我如何修复错误"每个套接字地址(协议/网络地址/端口)的一个用法通常被允许"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多谷歌上搜索,但没有多少运气与我的问题。我是新来的网络编程和努力学习,我一直试图建立一个简单的服务器和放大器;客户端进行通信(以下设在这里的在线教程 - > http://tech.pro/教程/ 704 / CSHARP教程简单的线程-TCP服务器



我遇到的问题是,我不断收到异常只有每个套接字地址(协议/网络地址/端口)的一个用法通常被允许,试图启动的TcpListener在服务器上时。



我试过禁用我的防火墙,改变使用的端口,左右,但移动变量无济于事(客户机工作正常,但它显然无法找到服务器,因为我无法启动的话)。



我见过的描述用Socket.Poll(的解决方案),但由于我只使用的TcpListener对象,我不知道如何使用轮询功能。



我的代码:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;使用的System.Net.Sockets
;使用System.Net
;
使用的System.Threading;
使用System.Text;

命名空间ServerTutorial {
级服务器{
私人只读螺纹m_listenThread;

公共服务器(){
m_listenThread =新主题(新的ThreadStart(ListenForClients));
m_listenThread.Start();
}

公共无效ListenForClients(){
变种监听器=新的TcpListener(IPAddress.Any,3000);
listener.Start();

而(真){
//阻塞,直到客户端已连接到服务器
TcpClient的客户= listener.AcceptTcpClient();

//将消息发送给客户端
变种编码器=新ASCIIEncoding();
的NetworkStream clientStream = client.GetStream();
字节[]缓冲= encoder.GetBytes(客户您好!);
clientStream.Write(缓冲液,0,buffer.Length);
clientStream.Flush();

//创建一个线程来处理与连接的客户端
变种clientThread =新主题(新ParameterizedThreadStart(HandleClient))通信;
clientThread.Start(客户端);
}
}

私人无效HandleClient(对象clientObj){//参数线程启动只能接受对象类型,所以要转换
VAR的客户=(TcpClient的) clientObj;
的NetworkStream clientStream = client.GetStream();

VAR消息=新的字节[4096];

,而(真){
INT读取动作= 0;

尝试{
//阻塞,直到客户端发送一个消息
读取动作= clientStream.Read(消息,0,4096);
} {赶上
//发生了套接字错误
System.Diagnostics.Debug.WriteLine(A套接字错误已发生);
中断;
}

如果(读取动作== 0){
//客户端从服务器
System.Diagnostics.Debug.WriteLine断开(A客户端从服务器)断开;
client.Close();
中断;
}

//消息已收到
变种编码=新ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(消息,0,读取动作));
}
}
}
}

在我的主要方式:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间ServerTutorial {
类节目{
静态无效的主要(字串[] args){
VAR服务器=新服务器();
server.ListenForClients();
}
}
}



任何帮助,非常感激!




  • 史蒂夫


解决方案<从明确的方法调用在主 - / DIV>

ListenForClients 是越来越调用两次(在两个不同的线程) 。当的的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!

  • Steve

解决方案

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.

这篇关于我如何修复错误&QUOT;每个套接字地址(协议/网络地址/端口)的一个用法通常被允许&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆