【问题标题】:Java Socket Chat unable to establish connection to portJava Socket Chat 无法建立到端口的连接
【发布时间】:2016-09-18 00:00:10
【问题描述】:

我正在尝试编写一个简单的服务器和客户端聊天应用程序,但是我的代码直接跳转到异常,这意味着它无法创建到端口的连接。

服务器:

public class Server
{
   public static void main(String args[])
   {
   try
   {
       ServerSocket ss = new ServerSocket(80);
       Socket sock = ss.accept();
       DataInputStream recive = new DataInputStream(sock.getInputStream());
       DataOutputStream sendOut = new DataOutputStream(sock.getOutputStream());
       BufferedReader readOut = new BufferedReader(new InputStreamReader(System.in));

       String in = "";
       String out = "";
       Scanner scan = new Scanner(System.in);
       System.out.println("Talk with client(y/n): ");
       String start = scan.nextLine();
       while(start == "y")
       {
       in = recive.readUTF();
       System.out.println(in);
       out = readOut.readLine();
       sendOut.writeUTF(out);
       sendOut.flush();
      }
    }
    catch(IOException e)
    {System.out.println("[!]CANNOT ESTABLISH CONNECTION[!]");}

  }
}

【问题讨论】:

  • e.printStackTrace() 真的很有帮助。

标签: java sockets ports


【解决方案1】:

确保您指定的端口未被使用(端口号 80)。您可能正在同一端口上运行另一个应用程序(例如 Web 服务器)。

【讨论】:

  • 我使用了几个不同的端口,如 1234,9090 和 80。似乎没有一个工作。在我的家用电脑和学校电脑上
  • 你能给出完整的堆栈跟踪吗?
  • 当我在 Jelliot 中运行它或设置断点时,它不会超过第 19 行或 ServerSocket ss = new ServerSocket(无论我提供什么端口);
  • 防火墙中是否允许 Java 监听传入连接?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多