• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Regarding java I/O

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am writing a socket program, in which the client has to send two information separately. First information is command i.e., "upload" or "download". Second information the filename to be download/uploaded to the server.

The code for the client side is as shown below
out.write("upload".getBytes());
out.flush();
out.write("fileName".getBytes());
out.flush();

The code for the server is as shown below
in.read(bytes,0,bytes.length);
String command = new String(bytes);
System.out.println("command:" + command);
in.read(bytes,0,bytes.length);
String fileName = new String(bytes);
System.out.println("fileName: " + fileName);

The output should be
command: upload
fileName: fileName

but the output i am getting is
command:uploadfileName
fileName: uploadfileName

Can anybody help me on this or suggest an alternative?

Thanks,
Sumanth
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're falsely assuming that there's a one-to-one correspondence between a client's write() call and a server's read() call. That's not the case.

If the client does


to write a total of N bytes, then it might take the server 1, 2, or N calls to read all the bytes the client wrote. It might even be possible for it to take an unbounded number of reads.

That is, any read() call can bring back any number of bytes, from 1 (or possibly even 0) up to the max number allowed by your buffer and parameters to read().

In your case, it appears that the first read is getting all the bytes, and the second read is getting none.

So you need a different way to distinguish between where the command ends and where the filename starts. Some possibilities are:

1) Before each piece, write a fixed, known number of bytes (such as 4, for an int) indicating the length of the piece that follows.

2) Use DataOutputStream.writeUTF() and DataInputStream.readUTF() to write and read the command and file name.

3) Put the command and file name into an Object, and use ObjectOutputStream and ObjectInputStream to write and read that object.

 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I read your reply and followed what you have mentioned, it worked. I have another question. Does the below code correct?

DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream())

Why i am asking is that, i have created two outstream reference variable one for passing Objects (i.e., dout) and another buffered stream (i.e, out) to pass file byte contents. Both are used to send data to server. The "dout" is mainly used to send commands like "UPLOAD", "DOWNLOAD" and "FILENOTFOUND", whereas "out" is used to send the actual data. Is this proper coding methods.

Thanks,
Sumanth
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumanth Prabhakar wrote:
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream())



This is bad. You shouldn't have two different paths into or out of a Stream.

 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to implement File Transfer app between client and server. So can you please suggest a way by which I can send the commands and also data. Or can I have the same server listen on two different ports. One port for uploading and another for downloading.

Thanks,
Sumanth
 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Any solution to my above mentioned problem. Please suggest a solution its urgent.

Thanks,
Sumanth
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumanth Prabhakar wrote:Hi,

Any solution to my above mentioned problem.



I gave you 3 suggestions in an earlier reply.

Please suggest a solution its urgent.



Only to you. Not to anybody here. Mentioning your urgency is a good way to discourage people from helping you.
 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

I didn't wanted to discourage anybody by my previous post. I forgot the first point that you mentioned earlier and I didn't go through the complete post again. I am sorry if my previous post has offended you.

Thanks,
Sumanth
 
Marshal
Posts: 27996
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumanth Prabhakar wrote:I need to implement File Transfer app between client and server. So can you please suggest a way by which I can send the commands and also data. Or can I have the same server listen on two different ports. One port for uploading and another for downloading.



Well, that is pretty close to what FTP actually does. So if you're in a hurry to get file transfer implemented, why not just use FTP? It's easy to set up, easy to use, and you don't need to do any programming.
 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

My restriction is that I have to use only java socket programming. So I cannot use FTP. Second I wanted to implement FTP, but there is another restriction. The restriction is that user has to enter the port number, we shouldn't randomly pick a port.

Thanks,
Sumanth
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumanth Prabhakar wrote:there is another restriction. The restriction is that user has to enter the port number, we shouldn't randomly pick a port.



Which port number? There will be 2 ports involved in any connection. The server listens on a known port, typically specified at startup by a command line option or config file. The client requests a connection to the server's IP address and port. On the client's end of the connection, usually we just let the OS assign us a port in the ephemeral port range, but you can tell it to use a specific port, and hope that that port is available.

You're really not making your requirements very clear, nor the specific problems you're having implementing them. If you can't or won't be more forthcoming with information, it will be difficult for anybody to help you.

Here's what I have so far:

  • There's a server.
  • There's a client.
  • The client sends a command, followed by a file name.
  • Then that file name is either uploaded or downloaded.
  • The user gets to specify some port at some time. It is not clear what port, for which end, or when he specifies it.
  • You are having some dificulty.


  • That's all I know.
     
    Sumanth Prabhakar
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Jeff and Paul,

    I am able to implement the file transfer app using the tip 1 mentioned by Jeff in his previous post. It is working successfully. Thanks a lot guys for your help.

    Thanks,
    Sumanth
     
    I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic