java 套接字在网络中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12802068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sockets won't work in network
提问by Zakaria Marrah
I am working on an application that sends files via the network.
我正在开发一个通过网络发送文件的应用程序。
I used 2 classes to send and to receive the file that I selected.
我使用了 2 个类来发送和接收我选择的文件。
The problem that I have faced, when I am working on localhost, is that the process goes correctly, but when I change the IP address to the network IP, it does not work.
当我在本地主机上工作时,我遇到的问题是该过程正确进行,但是当我将 IP 地址更改为网络 IP 时,它不起作用。
Here is the two classes that I am using.
这是我正在使用的两个类。
Class Server :
班级服务器:
package Test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server{
public static void main (String [] args ) throws IOException {
// create socket
ServerSocket servsock = new ServerSocket(41111);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// sendfile
File myFile = new File ("C:\Users\Marrah.Zakaria\Desktop\test\test.txt");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
}
}
Class Client:
班级客户:
package Test;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class Client{
public static void main (String [] args ) throws IOException {
int filesize=6022386; // filesize temporary hardcoded
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
Socket sock = new Socket("192.168.1.100",41111);
System.out.println("Connecting...");
// receive file
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\Test\test-copy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
sock.getPort();
}
}
after running an exception shows up :
运行后出现异常:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at 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)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
Please would you tell me what shall i do to get rid of it.
请你告诉我我该怎么做才能摆脱它。
I dis-activated the receivers firewall a different exception occurred :
我停用了接收器防火墙,发生了不同的异常:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at 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)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
回答by mkhelif
Have you check if there is any firewall blocking the custom port (41111) on your network (also check Windows firewall)?
您是否检查过是否有任何防火墙阻止了网络上的自定义端口 (41111)(还要检查 Windows 防火墙)?
This is the first thing to check when you have a timeout.
这是您超时时要检查的第一件事。
回答by Brian Agnew
If you start up the server, can you telnet
to this combination ?
如果你启动服务器,你能用telnet
这个组合吗?
telnet 192.168.1.100 41111
That'll tell you immediately if you have a routing issue (telnet
will refuse to connect)
如果您有路由问题,这会立即告诉您(telnet
将拒绝连接)
回答by Rohini Kumar
also try to ping 192.168.1.100 from the machine where you are running the client (i.e from the command prompt if you are in windows box)
还尝试从您运行客户端的机器上 ping 192.168.1.100(即,如果您在 Windows 框中,则从命令提示符)