我一直在开发一个 Swing 应用程序,当我想添加一个选项,用户可以通过本地网络发送文件时,我遇到了一些严重的问题。
为了通过网络发送文件,我使用了 Sockets 库来创建客户端和服务器之间的连接,Client.java的源代码是:
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 = null;
try{
sock = new Socket("192.168.1.107",1234);
}catch(Exception e){
e.printStackTrace();
e.getCause();
}
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();
}
}
Server.java的源代码 是:
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(1234);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
//servsock.set
System.out.println("Accepted connection : " + sock);
// sendfile
File myFile = new File ("C:\\testing\\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();
}
}
}
如您所见,我想将test.txt文件从我的计算机发送到本地网络中存在的另一台计算机。当我运行Server.Java和Client.java时,我在终端中遇到异常,提示连接超时:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
Connecting...
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)
at Test.NewClass1.main(Client.java:27)
Exception in thread "main" java.lang.NullPointerException
at Test.Client.main(Client.java:36)
更让我困惑的是,当我使用我的Localhost运行应用程序时,文件test-copy.txt的副本创建得很好,我查找了异常的原因,我发现它可能是防火墙权限,我为端口启用了事务:1234 ,但我仍然遇到同样的错误。
我在这里要问的是,这是权限问题还是特权问题?我的计算机也设置为管理员和目的地,或者我想添加或修改什么以使其正常工作?
我希望我把我的问题说清楚了。
谢谢 :)。