所以,我正在创建一个简单的文件传输应用程序。当我运行它时,我收到连接拒绝错误。现在,防火墙没有问题,因为当我试图从我正在关注的书中运行示例代码时,它运行得很好。服务器启动时没有错误。
服务器代码:
public class Server
{
final static int TRANSFER_PORT = 1215;
final static int FILEINFO_PORT = 1216;
static int filesize;
static String filename;
public static void main(String[] args) throws IOException
{
ServerSocket fileInfoSocket = new ServerSocket(FILEINFO_PORT);
try
{
System.out.println("Server Started");
Socket s = fileInfoSocket.accept();
System.out.println("Connected");
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
filesize = dis.readInt();
filename = dis.readUTF();
s.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
System.out.println("File info received.");
ServerSocket fileTransferSocket = new ServerSocket(TRANSFER_PORT);
byte[] fileByteArray = new byte[filesize];
try
{
Socket s = fileTransferSocket.accept();
InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.read(fileByteArray, 0, fileByteArray.length);
s.close();
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(fileByteArray);
bos.flush();
bos.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
System.out.println("Transfer complete!");
}
客户代码:
public class Client
{
final static int FILEINFO_PORT = 1216;
final static int TRANSFER_PORT = 1215;
final static String SERVER_ADDR = "localhost";
static int filesize;
static String filename;
public static void main(String[] args) throws IOException
{
if(args.length != 1)
{
System.out.println("Usage: Client filename");
return;
}
File file = new File(args[0]);
if(!file.isFile())
{
System.out.println("File not found!");
return;
}
filesize = (int) file.length();
filename = file.getName();
Socket fileInfoSocket = new Socket(SERVER_ADDR, FILEINFO_PORT);
try
{
OutputStream os = fileInfoSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(filesize);
dos.writeUTF(filename);
dos.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
Socket fileTransferSocket = new Socket(SERVER_ADDR, TRANSFER_PORT);
try
{
OutputStream os = fileTransferSocket.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
FileInputStream fis = new FileInputStream(filename);
byte[] byteArray = new byte[filesize];
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(byteArray, 0, byteArray.length);
bos.write(byteArray);
bos.flush();
bos.close();
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
运行客户端时终端出错:
pratyush@SuperComputer:~/JP/NetworkFileTransfer/Server$ java Client "a.png"
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at Client.main(Client.java:54)
答案 0 :(得分:0)
如果在fileTransferSocket上,或者fileTransferSocket没有时间打开,则无法正确理解抛出异常的行。试着在客户端睡觉:
Thread.sleep(100);
在:
Socket fileTransferSocket = new Socket(SERVER_ADDR, TRANSFER_PORT);