socket = serverSocket.accept();
Set up Server on the port.
Before Accepting client connection.
But the program hangs right after the below statement.
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?
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();
}
}
}
client program processes the actual file and writes the data to the new file.
Where does the server read a file and write it to the client?
the server side, i need to get the file and add it to the input stream.
client side I need to read from the input stream and write the contents to a new file using output stream.
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |