Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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!
0
errang
Asked:
errang
  • 13
  • 10
1 Solution
 
for_yanCommented:
what is the operating system of the destination location ?
0
 
for_yanCommented:
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
0
 
errangAuthor Commented:
Hm... did not know that, right now they'r running on Mac OSX
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
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?
0
 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
Its sending a message back to itself... the server's running on my machine as well.
0
 
for_yanCommented:
They are writing that end of line on new  Mac's is \x0A
0
 
for_yanCommented:
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
0
 
errangAuthor Commented:
Yea, I tried taking out the '\n' character, but that gives me a SocketException.
0
 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
The server's just sitting there, waiting for the client to send something, and then closes the connection after it receives something.
0
 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
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

0
 
for_yanCommented:
>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
0
 
errangAuthor Commented:
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.
0
 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
>> 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.
0
 
for_yanCommented:
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 ?
0
 
errangAuthor Commented:
>>You told me that server is Mac OSX. The client runs on the same machine ?

Yep
0
 
for_yanCommented:
If they are on the same system, then I' would try to change in all cases +"\n" to
System.getProperty("line.separator")
0
 
for_yanCommented:
I also don't think you need to add end of line whene you transfer just the name of the file.

And I cannot undersstand how it should say socket exception if you just remove part od the stuf your are sending (end of line)  whaereas it does not complain when you send additional system.
Check if youhave the coret line number of where the error is happening
0
 
errangAuthor Commented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now