在java中发送一个int。有时会引发异常?
好的,我将一个 int 从一个 java 程序发送到另一个程序(此时在同一台计算机上)。 但是,有时我会遇到异常并且无法连接:
线程“主”java.net.ConnectException中出现异常:连接被拒绝:连接 在 java.net.PlainSocketImpl.socketConnect(本机方法) 在 java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) 在 java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) 在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) 在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 在 java.net.Socket.connect(Socket.java:529) 在 java.net.Socket.connect(Socket.java:478) 在 java.net.Socket.(Socket.java:375) 在 java.net.Socket.(Socket.java:189) 在 Client.main(Client.java:6)
这是发送的代码:
Socket socket = new Socket("localhost" , 8080);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeInt(5);
socket.close();
和接收的代码:
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
DataInputStream din = new DataInputStream(socket.getInputStream());
System.out.println(din.readInt());
socket.close();
这很奇怪,因为有时它会起作用,有时则不起作用。有人知道为什么吗?
ok im sending an int from one java program to another (on same computer at the minute).
However, sometimes I'm getting an exception and it wont connect:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.(Socket.java:375)
at java.net.Socket.(Socket.java:189)
at Client.main(Client.java:6)
Here is the code for sending:
Socket socket = new Socket("localhost" , 8080);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeInt(5);
socket.close();
and for receiving:
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
DataInputStream din = new DataInputStream(socket.getInputStream());
System.out.println(din.readInt());
socket.close();
It's just odd because sometimes it will work and sometimes not. Does anyone have any ideas as to why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢打赌,如果您执行以下操作,您会收到此错误:
您的服务器仅接受一个连接,然后终止。
如果您想接受无限数量的顺序连接,请使用如下循环包围您的服务器代码:
在为请求提供服务后,它将再次侦听另一个请求或接受等待的请求。
I bet you get this error if you:
Your server only accepts one connection and then terminates.
If you want to accept an indefinite number of sequential connections, surround your server code with a loop like so:
After servicing a request it will listen again for another request or take a waiting request.
引用异常的描述:
“表明尝试时发生错误将套接字连接到远程地址和端口。通常,连接被远程拒绝(例如,没有进程正在侦听远程地址/端口)。
最有可能的是没有任何东西正在监听该端口。您的服务器代码似乎接受一个连接,然后关闭,因此除非您为每个连接重新启动服务器,否则这是最有可能的原因。如果没有,请检查您的服务器应用程序是否正在运行。
Quoting from the description of the exception:
"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)."
Most likely is that nothing is listening to the port. Your server code appears to accept one connection and then shut down, so unless you are restarting the server for every connection this is your most likely cause. If not, check your server app is running.