Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

Sending a file in Java

Hey,

      I'm trying to transmit a simple file using TCP with Java, but it keeps printing the escape characters, like "\n", in the new file... is there a simple way to avoid this? Rather than manually replacing the characters?

This is what I have:


Appreciate any help on this!
Avatar of for_yan
for_yan
Flag of United States of America image

what is the operating system of the destination location ?
I gues if you are using sockets, not some FTP package - you should take care  of end of lines yourself and you should append end of line which corresponds to operating system of your destination
Avatar of errang

ASKER

Hm... did not know that, right now they'r running on Mac OSX
so I guess you should add end of line as on Mac OSX
let us google and see what it is

I think "\n" will add the end of line as it is on your Operating system of origin.
What is that system ?
Avatar of errang

ASKER

According to http://www.twam.info/software/tune-terminal-in-os-x-lion

\e[4~ is the end line character for Mac OSX, but that's not working... am I looking for the right thing?

Is there a way for Java to check what OS its running on and use the right escape characters?
But this code runs on your client - so you can determine what kind of OS you have here but you need to know what is the end of line on the server

Can't you  you use Apache commons ftp library to ftp file to your server ?
Avatar of errang

ASKER

Its sending a message back to itself... the server's running on my machine as well.
They are writing that end of line on new  Mac's is \x0A
this line executes on client:
   outToServer.writeBytes(fileName + '\n');

and supposed to add a line to the file on the server - correct ?
If so, then /n is problematic
Avatar of errang

ASKER

Yea, I tried taking out the '\n' character, but that gives me a SocketException.
That is really strange - you should be able to send any data over the socket - especially when you removed data which you send - why should it complain, if it send more data before?

what is the fiull text of exception ?

maybe you try to use it like that:
  outToServer.writeBytes(fileName + "\0x0A");


In genaral I'm not sure I understand  what is your server part doing.
You are trying to send file form client to server - that is understandable.
So what are you receiveing form the server on the client then ?
Avatar of errang

ASKER

The server's just sitting there, waiting for the client to send something, and then closes the connection after it receives something.
then what is this part doing :

BufferedReader inFromServer =
         new BufferedReader(new InputStreamReader(
           clientSocket.getInputStream()));


and then this one:

  fileConts = inFromServer.toString();
      System.out.println(fileConts);


do you have the code of the server part ?
Avatar of errang

ASKER

Here is the server code:

class FTPServerUsingTCP 
   {
   public static final int TCP_SERVER_PORT = 6790;

   public static void main(String argv[]) throws Exception
      {
	   String clientFileName;

      /* Create a 'door' that listens on a TCP port */
      ServerSocket welcomeSocket = new ServerSocket(TCP_SERVER_PORT);

      while(true) 
         {
         /* Create a new socket when a client 'knocks' on the door */
         /* Accept opening of the TCP connection                   */
         Socket connectionSocket = welcomeSocket.accept();

         BufferedReader inFromClient =
            new BufferedReader(new InputStreamReader(
               connectionSocket.getInputStream()));

         DataOutputStream outToClient =
            new DataOutputStream(
               connectionSocket.getOutputStream());

         //waits for client to send server something
         /* Get user's line from client sent over TCP connection */
         clientFileName = inFromClient.readLine();
         System.out.println("fileName: " + clientFileName);
         
         //read in contents of given file
         BufferedReader readFile = new BufferedReader(new FileReader(clientFileName));
         
         String partialContents;
         while((partialContents = (readFile.readLine())) != null){
        	 outToClient.writeBytes(partialContents+"\n");
        	 System.out.print("readFile.readLine(): "+partialContents);
         }
         System.out.print("readFile.readLine(): "+partialContents);
         partialContents = "\n";
         outToClient.writeBytes(partialContents);
         System.out.println("finished");
         }
      }
   }

Open in new window

>The server's just sitting there, waiting for the client to send something, and then closes the connection after it receives something.

This does not seem to be what code does.
I think it is supposed to receive the filename form the client then it opens the file corresponding to that file name - reads this file and sends data from that file back to the client.
That is at least what seeems to have been intended - correct?

Then what do you observe on both sides when you try to execute it?

As I mentrioned before I'd rather intall ftp server on the server side and then use apache commons ftp client library to get the files I need form the server
It will be not so easy to write something functional along those lines comparabale with what FTP is doing
Avatar of errang

ASKER

Well... this isn't actually a "professional" FTP server, lol.  Its just supposed to be functional enough to work as a class assignment... not trying to rewrite any of apache's functionality here.
anyawy if this is  class in socket programming that may beanother story; I thought it is some practical suff

Anyway you agree that I was right at least  with the interpreatation of what it should do ?

If so explain what is happening when a user enters the file name - what do you observe bot on server and on client side ?

this remark:
> but it keeps printing the escape characters, like "\n", in the new file...
makes me think that at least you acheve connection from client to server?

So indeed describe, what is really happenning ?
Avatar of errang

ASKER

>> Anyway you agree that I was right at least  with the interpreatation of what it should do ?

Yea, think I do, but its a bit late here, lol... getting hard to stay awake.

>> So indeed describe, what is really happening ?

Initially, what was happening is that the server is reading in the entire file, with the end line characters and all, and then the server is receiving that information, and it creates a file with that data.
so both client and server are running on the same type of system?
You told me that server is Mac OSX. The client runs on the same machine ?
Avatar of errang

ASKER

>>You told me that server is Mac OSX. The client runs on the same machine ?

Yep
If they are on the same system, then I' would try to change in all cases +"\n" to
System.getProperty("line.separator")
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

Alright, fixed that bug...

Thanks to your help, I considered looking at the server... I thought all the processing was done on the client... who would have thought the silly server would be doing the reading? lol...

The problem with the server reading the file, was that it was using the regular loop to read through a file (till it reaches null), but the problem with that, is the server doesn't send anything back to the client when it does hit null... lol.

I added the line to send the null signal back to the client, and put the readLine() part in a do while loop, and everything's working fine now =).

Thanks for helping me figure it out.