Original Post
So I wrote up a server in java to get POSTs from clients. When I'm reading the post my thread is exited.
here is the snippet that's not working. My program goes through the while loop 4 times then exits the thread, however I know that the POSTs are 7 lines. it does not accept any new posts. Why wont it just read the lines of the post and get the full message?
I think you can run this server on on commandline if you change the methond sendMsgToWindow() to print to the terminal screen.
here is the snippet that's not working. My program goes through the while loop 4 times then exits the thread, however I know that the POSTs are 7 lines. it does not accept any new posts. Why wont it just read the lines of the post and get the full message?
I think you can run this server on on commandline if you change the methond sendMsgToWindow() to print to the terminal screen.
//get the whole POST
while( (wholemessage = input.readLine()) !=null)
{
sendMsgToWindow(wholemessage+ "\n");
}import java.net.*;
import java.io.*;
public class server extends Thread {
private ServerGui message_to;
private int port;
//constructor///you made it to the hard part!
//essentially we give the server a GUI so it has a place to output what its doing
//also we assign it a port
//lastly this.start() starts the run() function (scroll down)
public server(int ListenPort, ServerGui sendMsgTo)
{
message_to = sendMsgTo;
port = ListenPort;
this.start();
}
//this is a function that prints a message to our window
private void sendMsgToWindow(String message)
{
message_to.send_message_to_window(message);
}
//data validation method // we are handling inbout msgs from broker
//i think we should make a new class called http_handler which containts
//data validation funcions
private void http_handler(BufferedReader input, DataOutputStream output)
{
boolean isPOST = false; //check if we recieved a POST and not something else
String http = new String();
String path = new String();
String file = new String();
String user = new String();
try{
//here is where we recieve the post
//two temporary variables to hold the first line of the message
String temp = input.readLine();
String temp2 = new String(temp);
String wholemessage = new String();
//get the whole POST
while( (wholemessage = input.readLine()) !=null)
{
sendMsgToWindow(wholemessage+ "\n");
}
//convert to capital letters
temp.toUpperCase();
//check to see if we got a post
if(temp.startsWith("POST"))
isPOST = true;
//if its not a post send the broker an error messgae
if(!isPOST)
{
try{
output.writeBytes(construct_http__inbound_response_header("Not a post request\n"));
sendMsgToWindow("Submission was not a post query\n");
output.close();
return;
}catch (Exception e)
{
//diplay error
sendMsgToWindow("error in outputting bytes: "+ e.getMessage());
}
}
//get the path from post
path = TranslateHTTP.GetPathFromPost(temp2);
}catch(Exception e){
sendMsgToWindow("Error handling post: "+ e.getMessage());
}
try{
//when all is done close the buffered reader
input.close();
}catch(Exception e)
{
sendMsgToWindow("Could not close file: "+ e.getMessage());
}
}
//send an inbound message response formatted in xml
//as requested by judges
private String construct_http__inbound_response_header(String reason)
{
String response = "<?xml version=”1.0” encoding=”UTF-8”?>\n " +
"<Response>\n " +
"<Exchange>" +
reason+
"</Exchange>\n" +
" </Response>\n";
return response;
}
//this is our own thread
//it runs parallel to the main method in Exchange
//it runs when we call this.start()
//it opens a port for incoming data and runs forever
public void run()
{
//open a server socket
ServerSocket servsock = null;
//print
sendMsgToWindow( "server is up\n");
try{
sendMsgToWindow("Trying to bind to localhost on port "
+Integer.toString(port)+ "...");
//bind to localhost
servsock = new ServerSocket(port);
}catch(Exception e){
sendMsgToWindow("\n There was an error binding: "+ e.getMessage()+"\n");
return;
}
sendMsgToWindow("Ok..done\n");
while(true)
{
sendMsgToWindow("Waiting for requests...\n");
try{
//accept any incoming requests
Socket ClientConnection = servsock.accept();
//get is ip
InetAddress clientip = ClientConnection.getInetAddress();
//print who we are connected to
sendMsgToWindow("We are connected to "+ clientip.getHostName() + "\n");
//get the input from the client ie. what is he saying
BufferedReader input =
new BufferedReader(new InputStreamReader(ClientConnection.getInputStream()));
//create a stream to respond to the client
DataOutputStream output =
new DataOutputStream(ClientConnection.getOutputStream());
//this is the function to take care of validation etc.
http_handler(input, output);
}catch(Exception e){
sendMsgToWindow("We encountered an error: "+ e.getMessage());
}
}// end of forever loop
}
}