I have a server which takes a request for a WSDL and sends back the WSDL's XML line by line. I know the request is being received and processed correctly because I have the server printing out the XML to the console as it's writing to the server socket. My problem right now is my client app is supposed to be reading in the xml and then printing out method signatures using the xml. I'm going to use DOM / DocumentBuilder to get the parts of the method signature(s) from the WSDL, but I need to first put the lines read in into a file. How can I do this? Currently I'm trying to do it this way:
//request WSDL from server
System.out.println("Client requesting \"MathServices?wsdl\"...");
socketWriter.write("GET MathServices?wsdl");
socketWriter.close();
//read XML response into file
try {
File wsdlXML = new File("MathServices.xml");
FileOutputStream wsdlXmlWriter = new FileOutputStream(wsdlXML);
String xmlLine;
while((xmlLine = socketReader.readLine()) != null){
wsdlXmlWriter.write(xmlLine.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
But am getting this error:
Client requesting "MathServices?wsdl"... java.net.SocketException: socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:129) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at client.Client.main(Client.java:50)
EDIT: server code
package server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
public class RequestHandler extends Thread {
Object block;
ServerSocket serverSocket;
BufferedReader socketReader;
PrintWriter socketWriter;
public RequestHandler(Object block, ServerSocket serverSocket){
this.block = block;
this.serverSocket = serverSocket;
}
@Override
public void run() {
try{
System.out.println("Waiting for connection...");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection made.");
synchronized(block){
System.out.print("Notifying server thread...");
block.notify();
System.out.println("...done");
System.out.println();
}
System.out.println("Setting up streams...");
socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
socketWriter = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("Reading request");
String input;
while((input = socketReader.readLine()) != "\n"){
//System.out.println("Input: " +input);
if(input.startsWith("GET")){
System.out.println("GET received.");
getResource(input);
}
}
socketWriter.close();
socketReader.close();
clientSocket.close();
System.out.println("Streams closed.");
}catch(IOException e){
System.out.println("IOException!");
e.printStackTrace();
}
}
public void getResource(String getRequest){
String[] parts = getRequest.split("\\s+");
String filename = parts[1].substring(1);
if(filename.equals("MathServices?wsdl")){
filename = "MathServices.wsdl";
}
System.out.println(filename);
File resource = new File(filename);
sendResponse(resource, 1);
}
public void sendResponse(File resource, int type){
System.out.println(resource.getAbsolutePath());
Scanner fileReader;
try {
fileReader = new Scanner(resource);
while(fileReader.hasNext()){
String line = fileReader.nextLine();
System.out.println(line);
socketWriter.println(line);
}
socketWriter.println("\n");
System.out.println("end of response");
socketWriter.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
e.printStackTrace();
}
}
}