• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Operation , Program hangs in the middle and stops execution.  RSS feed

 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to implement a simple client server model program to send a text document from server to Client.
I have written a sample program . But the program hangs right after the below statement.

socket  = serverSocket.accept();


Below is my full program and the output i get while running this class.
My intent is to when is this method started working fine, i will expose this method as a webservice so that any client can use this method to get this text file as stream and recover it into file again.

Please advise what mistake i'm doing or is there any better way to send a file from server to client.


Java Class :




Output :

Set up Server on the port.
Before Accepting client connection.



 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the program hangs right after the below statement.

The server would be waiting for a client to connect.  Nothing happens until a client connects.

How are you using a client to connect to the server?  Have you tried a browser using the localhost address: 127.0.0.1?
 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
But the program hangs right after the below statement.

The server would be waiting for a client to connect.  Nothing happens until a client connects.

How are you using a client to connect to the server?  Have you tried a browser using the localhost address: 127.0.0.1?


Hi Norm Radder ,

Thanks for the advise ...
I did not know that its waiting for client connection.  thought something is wrong and it got hanged..
I am writing a client program to get the connection..
will post the output here shortly !!!
Thanks again ...

 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written Client Program as below but there is one big logic flaw in my code .

I want the server to locate the text file and send it back to the client in inputstream.
The client should read the content from the input stream and create a new file out of it .

But my program is doing the opposite, the server just sends an empty file, my client program processes the actual file and writes the data to the new file.

1. Test  is the file which has some data.
2. Test2 is a new file which needs to be created once the server sends the Original Test file.

Basically I want to send the Test file from Server to Client and the Client has to process the Test file and create a new Test1 file which has the same content
I dont know how to achieve this, please throw some light on it.

Below is my Client and Server Code ...




Server Code :

package com.document.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class DocumentReturnImpl {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
  while (true) {
try {
serverSocket = new ServerSocket(1024);
System.out.println("Set up Server on the port. ");
        } catch (IOException ex) {
            System.out.println("Can't setup server on this port number. ");
        }

        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
     
        try {
        System.out.println("Before Accepting client connection. ");
        socket  = serverSocket.accept();
        System.out.println("accepted client connection. ");
        } catch (IOException ex) {
            System.out.println("Can't accept client connection. ");
        }

        System.err.println("The Cclient connection is accepted");

        try {
            in = socket.getInputStream();
        } catch (IOException ex) {
            System.out.println("Can't get socket input stream. ");
        }

        try {
            out = new FileOutputStream("C://Document for my webservice/test1.txt",true);
        } catch (FileNotFoundException ex) {
            System.out.println("File not found. ");
        }

        byte[] bytes = new byte[1024];

        int count;
        while ((count = in.read(bytes)) > 0) {
        System.out.println("Inside impl inputstream");
            out.write(bytes, 0, count);
        }

       
        out.close();
        in.close();
        socket.close();
        serverSocket.close();

        }

}

}


 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
client program processes the actual file and writes the data to the new file. 

Where does the client read from the server and write to a local file?

Where does the server read a file and write it to the client?
 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Where does the server read a file and write it to the client?


My understanding is in the server side, i need to get the file and add it to the input stream.
In the client side I need to read from the input stream and write the contents to a new file using output stream.

Could you please tell me whether my understanding is right ??
If my understanding is correct,  I just have one doubt, how to add the file to input stream so that the client can access it ..
Please advise...


 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  the server side, i need to get the file and add it to the input stream.

I don't know what that means.  What does "get the file" mean?  What does "add it" mean?
The server needs to read the file and write it to an output stream that goes to the client.

client side I need to read from the input stream and write the contents to a new file using output stream. 

That sounds about right.   Change the code so it does it that way,
The client needs to read the file from the input stream from the server and write it to a local file.
 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I don't know what that means.  What does "get the file" mean?  What does "add it" mean?
The server needs to read the file and write it to an output stream that goes to the client.


Sorry, I am trying to learn sending file through sockets , I don't have strong knowledge on the basics .

What i meant from the above quote was, I will do something like



and after that i thought like there will be some methods existing to add this inputstream  to socket.

After that , In my client I will use some method to get access to the socket and read the input stream from Socket.

After reading the input stream, I will create a new output stream only in client and write the contents to a new file.

Which means there is no need of using outputstream in the server class.

I think this logic is totally wrong and dumb but this what I have tried to explain you about my understanding in the previous post.

Please advise...




 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
InputStreams are for reading from a source.  Use one of the class's methods to read data from the source.
OutputStreams are for writing to a target.  Use one of the class's methods to write data to the target.

The DocumentReturnImpl class uses
an InputStream to read data from the socket and
a FileOutputStream to write data to a file.

To send a file to a client, it needs to
use an InputStream to read the file and
an OutputStream to write the data from the file to the socket and to the client.


The DocumentReciever(sic) class uses
a FileInputStream to read data from a file and
an OutputStream to write  data from the file to the socket and to the server.

To read a file from the server it needs to
use an InputStream from the socket to access the data sent by the server and
a FileOutputStream to write the data to a file.
 
murali jackson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detailed explanation Norm Radder !!!
I will follow this and share the updated code here ...
Thanks again...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!