I am trying to make simple client and server that exchanges files, so, I am trying to use the writeUtf()
method to send the file name as a String
, but it is not working at all.
public class Client {
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
Socket client = new Socket("localhost", 1241);
Path currentRelativePath = Paths.get("");
String currentPath = currentRelativePath.toAbsolutePath().toString();
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
DataInputStream dataInFromServer = new DataInputStream(client.getInputStream());
Scanner keyboardInput=new Scanner(System.in);
System.out.println("Enter Operation Type(PUt, Get, Or Exit)");
String OperationType=keyboardInput.nextLine();
switch(OperationType){
case "PUT":
outToServer.writeChar('P');
System.out.println("File Or Directory name:");
String FileOrDirName=keyboardInput.nextLine();
//outToServer.writeUTF(FileOrDirName);
//outToServer.write((short)FileOrDirName.length());
outToServer.writeUTF(FileOrDirName);
File fileOrDir=new File(FileOrDirName);
if(fileOrDir.getParent()==currentPath || fileOrDir.getParent()==null){
if(fileOrDir.isDirectory()){
outToServer.writeInt(1);
FileOperations.zipDir(fileOrDir);
outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName+".zip"));
File tmp=new File(FileOrDirName+".zip");
tmp.delete();
}
else {
outToServer.writeInt(0);
byte toSend[]=FileOperations.readFileAsByteArray(FileOrDirName);
outToServer.writeDouble(toSend.length);
for(int i=0; i<toSend.length; i++)
outToServer.writeByte(toSend[i]);
// outToServer.write(ToSend,0,ToSend.length);
//outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName));
}
outToServer.flush();
outToServer.close();
keyboardInput.close();
client.close();
}
}
}
}
and here is the Server:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Path;
import java.nio.file.Paths;
class TcpFileServerThread extends Thread{
private Socket client;
public TcpFileServerThread(Socket client) {
this.client = client;
}
public void run() {
try {
DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());
DataInputStream dataInFromClient = new DataInputStream(client.getInputStream()) ;
Path currentRelativePath = Paths.get("");
String currentPath = currentRelativePath.toAbsolutePath().toString();
//File f;
//System.out.println(dataInFromClient.readInt());
/** client request PUT **/
switch(dataInFromClient.readChar()){
case 'P':
int lengthRecvd= (int)dataInFromClient.readDouble();
byte[] toRecv=new byte[lengthRecvd];
for(int i=0; i<lengthRecvd; i++){
toRecv[i]=dataInFromClient.readByte();
}
// dataInFromClient.read(file_or_dir_from_client);
//FileOrDirName="uu.txt";
String FileOrDirName=dataInFromClient.readUTF();
// int arrLen=dataInFromClient.readShort();
/** for(int i=0; i<arrLen; i++){
char o=dataInFromClient.readChar();
System.out.println(o);
}**/
System.out.println(FileOrDirName);
File f=new File(FileOrDirName);
if(f.getParent()==null || f.getParent()==currentPath){
FileOperations.writeFromByteArrayToFile(toRecv, FileOrDirName);
if(dataInFromClient.readInt()==1){
File zipFl=new File(FileOrDirName+".zip");
FileOperations.unzip(zipFl);
zipFl.delete();
}
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
I get FileNotFoundException
and the output for System.out.println(FileOrDirName)
in server is a blank line as it was empty.