【问题标题】:Exception in opening a socket at my IP address在我的 IP 地址打开套接字时出现异常
【发布时间】:2015-06-13 16:37:28
【问题描述】:

我是 C# 套接字编程的新手。我正在尝试在两台计算机之间建立一个聊天服务器,但我无法这样做,因为我无法启动我的 Socket .....我给了服务器程序我的 IP 地址,但给了我一个例外......“请求的地址是在其上下文中无效” ...这是代码:

        IPAddress hostIPAddress = IPAddress.Parse("178.189.27.85");
        TcpListener serverSocket = new TcpListener(hostIPAddress, 8888);
        int requestCount = 0;
        TcpClient clientSocket = default(TcpClient);
        serverSocket.Start();
        Console.WriteLine(" >> Server Started");
        clientSocket = serverSocket.AcceptTcpClient();
        Console.WriteLine(" >> Accept connection from client");
        requestCount = 0;

        while ((true))
        {
            try
            {
                requestCount = requestCount + 1;
                NetworkStream networkStream = clientSocket.GetStream();
                byte[] bytesFrom = new byte[10025];
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                Console.WriteLine(" >> Data from client - " + dataFromClient);
                string serverResponse = "Last Message from client" + dataFromClient;
                Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                networkStream.Write(sendBytes, 0, sendBytes.Length);
                networkStream.Flush();
                Console.WriteLine(" >> " + serverResponse);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        clientSocket.Close();
        serverSocket.Stop();
        Console.WriteLine(" >> exit");
        Console.ReadLine();

客户端程序

try {
        TcpClient tcpclnt = new TcpClient();
        Console.WriteLine("Connecting.....");

        tcpclnt.Connect("192.168.128.1",8888);
        // use the ipaddress as in the server program

        Console.WriteLine("Connected");
        Console.Write("Enter the string to be transmitted : ");

        String str=Console.ReadLine();
        Stream stm = tcpclnt.GetStream();

        ASCIIEncoding asen= new ASCIIEncoding();
        byte[] ba=asen.GetBytes(str);
        Console.WriteLine("Transmitting.....");

        stm.Write(ba,0,ba.Length);

        byte[] bb=new byte[100];
        int k=stm.Read(bb,0,100);

        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(bb[i]));

        tcpclnt.Close();
    }

    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }

【问题讨论】:

  • 使用搜索。你必须监听机器分配给任何网卡的 IP,除非你的调制解调器处于桥接模式,否则你有一个内部 IP 地址,很可能是192.168.x.x。只需绑定到0.0.0.0
  • TcpClient clientSocket = default(TcpClient); 是多余的。稍后在实际分配变量TcpClient clientSocket = serverSocket.AcceptTcpClient();时声明TcpClient

标签: c# sockets server


【解决方案1】:
IPAddress hostIPAddress = IPAddress.Parse("178.189.27.85");

您的计算机实际上是否在公共互联网上?我希望您的计算机具有私有 IP(例如 192.168.x.y、172.16-32.x.y、10.x.y.z),除非它直接连接到您的互联网连接。

假设您使用的是 Windows,请使用命令提示符并运行 ipconfig,或访问控制面板并查找您的网络设置。例如,在 Windows 7 上,它位于网络和共享中心的“更改适配器设置”下;查找活动适配器,右键单击并选择“状态”。

找到本地 IP 地址后,使用它。或者你可以使用 0.0.0.0 作为通配符地址——实际上是说“只要接受来自任何地方的连接,我不在乎我的 IP 地址是什么。”

【讨论】:

  • 我现在使用来自 ip config 的 IP 我在客户端程序上收到此异常“连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有回应”
  • 这是一个不同的问题。您是否将代码更改为绑定到 0.0.0.0?还是其他地址?在您的客户端程序(您尚未提供代码)中,您是否在同一台计算机上运行它?
  • 不,我没有在同一台计算机上运行它。它是不同的计算机......上面我提到了客户端程序
  • 从“客户端”计算机,ping 您的“服务器”计算机。因此,如果您的服务器计算机是 192.168.128.1,您将打开命令提示符并输入 ping 192.168.128.1。如果您收到“超时”错误,则需要验证这些计算机上的 IP 地址和网络设置。这有点超出了这个问题的范围,但你可以打开一个新的。
猜你喜欢
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
相关资源
最近更新 更多