我正在開發一個服務器到Java上的客戶端文件傳輸程序,並不知道如何解決以下代碼,因爲我對套接字編程知之甚少。該代碼是客戶端的代碼:服務器端客戶端文件傳輸空指針異常
String receiverIP = null;
int serverPort = 0;
hostIP = args[0];
serverPort = Integer.parseInt(args[1]);
String fileToSend = args[2];
byte[] aByte = new byte[1];
int bytesR;
Socket clientSocket = null;
Socket connectSocket = null;
BufferedOutputStream ToClient = null;
InputStream is = null;
try {
ToClient = new BufferedOutputStream(connectSocket.getOutputStream());
clientSocket = new Socket(hostIP, serverPort);
is = clientSocket.getInputStream();
} catch (IOException ex) {
System.out.println(ex);
}
對於我的問題,我得到14行(無疑,因爲目前connectSocket爲null)一個空指針異常,但我不知道我能在connectSocket分配(如果它是在服務器端連接接受套接字可能已被分配,以在連接建立後開始寫入。)
ToClient有什麼用?它和'is'有什麼區別? – tbodt
ToClient在獲取的文件大小低於該代碼 – emenike