Here's what i've been trying to achieve for about a week now:
I'm trying to develop a simple search engine the results of searches are stored in an xml repository the only way to access the server is through TCP/IP sockets it accepts an input (xml query) and returns another xml as output with the answers.
I have to create an xml document (using dom) based on the users search (done that)
then after that i should be able to send the XML file created to the server via TCP/IP sockets (i've partially completed this
basically my code opens an input and output stream and it works. However when i send a query as a string it just returns blank results, meaning the query is in the wrong format. I have to send the XML file.
Next, when i recieve the response from the server with the answers (assuming that the query is in the right format) i'll get back a list of answers in XML format. Now what i want is to display this in a html file so the user sees it.
Any help would be appreciated.
Code:
import java.net.*;
import java.io.*;
public class SendQuery {
public static void main (String[] args) throws Exception
{
try {
// Construct data
String data = "<?xml version=\"1.0\"?>";
data += "<query />";
System.out.println(data);
// Create a socket to the host
String hostname = "testserver:313";
int port = 313;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
// Send header
String path = "/servlet/SomeServlet";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("hello");
// Send data
wr.write(data);
wr.flush();
// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}
try {
// Create a URL for the desired page
URL url = new URL("http://testserver:313");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
System.out.println(str);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}