|
-
April 14th, 2010, 03:51 AM
#2
Re: TCP Server on embedded system
I've rewritten part of the above code in an attempt to clean it up and get it to do what I need.
Code:
package TCPServer;
import java.io.*;
import java.net.*;
/**
* TCP server that receives a single byte, increments the value, and
* sends the incremented value back.
*
* This file and other embedded Ethernet and Internet code and resources are
* available from www.Lvr.com.
*
* @author Jan Axelson
* @author Shawn Silverman
*/
public class TcpServer implements Runnable {
private ServerSocket server;
private int readTimeout;
private Thread serverThread;
private volatile boolean runServer;
/**
* Main method that tests the class.
*/
public static void main(String[] args) throws IOException {
// The port that clients will connect on.
int localPort = 5551;
// The timeout in milliseconds to wait for data from a client
// after connecting.
int readTimeout = 5000;
// Create a TcpServer object using localPort and readTimeout.
TcpServer myTcpServer = new TcpServer(localPort, readTimeout);
while (true){
// Endless loop that executes while waiting for connections.
try {
// The thread can spend most of its time sleeping.
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.print("InterruptedException: ");
System.out.println(e.getMessage() );
}
} // end while(true)
} // end main()
/**
* Creates a thread to handle connection requests.
*
* @param localPort the port to listen for connections on.
* @param readTimeout the time in milliseconds to wait for bytes from a remote host.
* @throws IOException on error in opening the server socket.
*/
public TcpServer(int localPort, int readTimeout) throws IOException {
// Create a server socket on localPort.
// The ServerSocket object creates a socket object on receiving a
// communication request.
server = new ServerSocket(localPort);
// The serverThread thread uses the readTimeout value passed to this constuctor.
this.readTimeout = readTimeout;
// Create a thread to handle connection requests.
serverThread = new Thread(this);
// The thread is a Daemon thread, which ends when no user (non-Daemon)
// threads are running.
serverThread.setDaemon(true);
// The start() method calls the thread's run() routine.
serverThread.start();
} // end TcpServer constructor
/**
* Stops the server.
*/
public void stop() {
// When runServer is false, the run method ends.
runServer = false;
try {
// Close the socket. Do nothing on an exception.
server.close();
} catch (IOException e) {
}
} // end stop()
/**
* Main method for the thread.
*/
public void run() {
// runServer remains true until an exception or the stop() method sets it false.
runServer = true;
while (runServer) {
try {
// Wait for and accept a connection request.
Socket socket = server.accept();
try {
// Call a method to handle the connection.
handleConnection(socket);
} catch (IOException e) {
System.out.print("Error while working with socket: ");
System.out.println(e.getMessage());
} finally {
// Close the socket to release resources.
// Do nothing on an exception.
try {
socket.close();
} catch (IOException e) {
}
}
} catch (IOException e) {
// There was an error while accepting a new socket.
// Stop the server thread.
runServer = false;
}
} // end while (runServer)
} // end run()
/**
* Handles a single conection.
*
* @param socket a connected socket
* @throws IOException on error when communicating with the remote
* host.
*/
private void handleConnection(Socket socket) throws IOException {
// Set the read timeout for waiting for data from the remote host.
socket.setSoTimeout(readTimeout);
// An InputStream object reads data from the remote host.
InputStream in = socket.getInputStream();
// An Outputstream object writes to the remote host.
OutputStream out = socket.getOutputStream();
try {
// Try to read a byte from the remote host.
int b = 0;
while (in.available()==0)
{
b=0;
}
b=in.available();
byte[] http_request = new byte[b];
in.read(http_request);
String info = new String (http_request);
System.out.println("Received: ");
System.out.print(info);
if(info.substring(0, 3).equals("GET"))
{
System.out.println("Sent: ");
byte[] response="HTTP/1.1 200 OK\r\n\r\n<HTML><BODY>SIMPLE WEB PAGE</BODY></HTML>".getBytes();
out.write(response);
String write = new String (response);
System.out.println(write);
}
else
{
byte[] BadForm = "HTTP/1.1 400 Bad Request\r\n\r\n".getBytes();
out.write(BadForm);
String error = new String (BadForm);
System.out.println(error);
}
out.flush();
out.close();
} catch (InterruptedIOException ex) {
// The read attempt has timed out.
System.out.print("The remote host sent no data within ");
System.out.print(readTimeout / 1000);
System.out.println(" seconds.");
}
} // end handleConnection()
} // end TcpServer class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|