0
因此,我創建了一個簡單的文件傳輸應用程序。當我運行它時,我得到一個Connection refused錯誤。現在,防火牆沒有問題,因爲當我嘗試從我正在遵循的書中運行示例代碼時,它運行得很完美。服務器啓動時沒有錯誤。獲取ConnectionException:在文件傳輸應用程序上拒絕連接
Server代碼:
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());
}
}
在終端錯誤時,客戶端運行:
[email protected]:~/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)
非常感謝你。有效。但請解釋一下,當我讓客戶端進入睡眠狀態(在第一個嘗試塊結束時)時發生了什麼變化。因爲早些時候,服務器甚至在accept()方法返回後應該打印「Connected」的時候甚至沒有得到println()語句 –
程序正在同時運行,因此服務器將到達Socket fileTransferSocket =新的套接字(SERVER_ADDR,TRANSFER_PORT);同時客戶也試圖打開它......可惜它不能及時打開,所以客戶端永遠無法連接 –