如何在Java中设置客户端/服务器?

时间:2013-03-20 11:08:16

标签: java sockets networking

所以我使用Socket和ServerSocket类编写了一个程序。但是,如果服务器和客户端都在同一台计算机上,并且客户端连接到localhost(127.0.0.1),我只能连接到我的服务器。即使两者在同一台机器上,我也无法使用常规IP地址进行连接:我必须使用127.0.0.1。如何设置服务器以便可以从任何计算机连接到服务器?

我正在使用Java中的网络教程(http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html)。

这是来自客户端的堆栈跟踪: java.net.ConnectException:连接超时:连接     at java.net.DualStackPlainSocketImpl.connect0(Native Method)     在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)     在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)     在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)     在java.net.Socket.connect(Socket.java:579)     在java.net.Socket.connect(Socket.java:528)     在java.net.Socket。(Socket.java:425)     在java.net.Socket。(Socket.java:208)     在client.Client.connect(Client.java:38)     at client.ClientUI $ 2.mouseClicked(ClientUI.java:87)     在java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:269)     at java.awt.Component.processMouseEvent(Component.java:6508)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3321)     at java.awt.Component.processEvent(Component.java:6270)     at java.awt.Container.processEvent(Container.java:2229)     at java.awt.Component.dispatchEventImpl(Component.java:4861)     at java.awt.Container.dispatchEventImpl(Container.java:2287)     at java.awt.Component.dispatchEvent(Component.java:4687)     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)     at java.awt.Container.dispatchEventImpl(Container.java:2273)     at java.awt.Window.dispatchEventImpl(Window.java:2719)     at java.awt.Component.dispatchEvent(Component.java:4687)     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)     at java.awt.EventQueue.access $ 200(EventQueue.java:103)     at java.awt.EventQueue $ 3.run(EventQueue.java:688)     at java.awt.EventQueue $ 3.run(EventQueue.java:686)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)     at java.awt.EventQueue $ 4.run(EventQueue.java:702)     at java.awt.EventQueue $ 4.run(EventQueue.java:700)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) Java结果:-1

2 个答案:

答案 0 :(得分:1)

不幸的是,您既没有发送代码也没有发送堆栈跟踪,因此无法发现您的问题。

但是,我可以假设您可能已被计算机上运行的个人防火墙阻止。从一台机器搬到两台机器时,这是一个典型的问题。发现这一点:在两台机器上首先停止防火墙(如果它正在运行)。如果它有助于配置允许客户端连接到服务器的规则(在防火墙规则中配置端口和可能的IP。)

答案 1 :(得分:0)

检查您的操作系统防火墙和可能包含防火墙的防病毒程序。正如AlexR建议的那样,您可以禁用所有防火墙和防病毒软件,如果连接成功,您可以向防火墙添加允许连接所需的规则......