Solved

Sending a file in Java

Posted on 2012-03-16
23
312 Views
Last Modified: 2012-06-27
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
Comment
Question by:errang
  • 13
  • 10
23 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37731954
what is the operating system of the destination location ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731958
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
 

Author Comment

by:errang
ID: 37731960
Hm... did not know that, right now they'r running on Mac OSX
0
Industry Leaders: 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!

 
LVL 47

Expert Comment

by:for_yan
ID: 37731971
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
 

Author Comment

by:errang
ID: 37731972
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37731977
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
 

Author Comment

by:errang
ID: 37731981
Its sending a message back to itself... the server's running on my machine as well.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731984
They are writing that end of line on new  Mac's is \x0A
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37731987
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
 

Author Comment

by:errang
ID: 37731994
Yea, I tried taking out the '\n' character, but that gives me a SocketException.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37732003
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
 

Author Comment

by:errang
ID: 37732013
The server's just sitting there, waiting for the client to send something, and then closes the connection after it receives something.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37732017
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
 

Author Comment

by:errang
ID: 37732022
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37732034
>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
 

Author Comment

by:errang
ID: 37732039
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37732043
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
 

Author Comment

by:errang
ID: 37732050
>> 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
 
LVL 47

Expert Comment

by:for_yan
ID: 37732059
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
 

Author Comment

by:errang
ID: 37732061
>>You told me that server is Mac OSX. The client runs on the same machine ?

Yep
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37732062
If they are on the same system, then I' would try to change in all cases +"\n" to
System.getProperty("line.separator")
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37732067
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
 

Author Closing Comment

by:errang
ID: 37732075
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
Convert from a json string array to a Java object 3 64
add projects t working set in maven 2 41
How to fix  socket closed error 11 28
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question