So as my project I had to write a client class and a simple server class that will ECHO message written by the client.
For some reason I either get I/O exception or a loop of exceptions that socket's closed.
I would really appreciate some help, because I struggle with this program for 2 days straight and cannot find any solutions.
My Server class:
import java.io.*;
import java.net.*;
import java.util.regex.Pattern;
public class SimpleServer {
private ServerSocket ss = null;
private BufferedReader in = null;
private PrintWriter out = null;
public SimpleServer(String host, int port) {
try {
InetSocketAddress isa = new InetSocketAddress(host, port);
ss = new ServerSocket();
ss.bind(isa);
} catch (IOException exc) {
System.out.println("Error in constructor");
System.exit(1);
}
System.out.println("Server started.");
System.out.println("on port: " + ss.getLocalPort());
System.out.println("bind address: " + ss.getInetAddress());
serviceConnections();
}//constructor
private void serviceConnections() {
boolean serverRunning = true;
while(serverRunning) {
try {
Socket conn = ss.accept();
System.out.println("Connection established!");
serviceRequests(conn);
} catch (Exception exc) {
exc.printStackTrace();
}
try { ss.close(); } catch (Exception exc) {}
}
}//serviceConnections
private void serviceRequests(Socket connection) throws IOException {
try {
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out = new PrintWriter(connection.getOutputStream(), true);
String line = in.readLine();
out.println(line);
} catch (Exception exc) {
exc.printStackTrace();
} finally {
try {
in.close();
out.close();
connection.close();
connection = null;
} catch(Exception exc) {}
}
}//serviceReq
public static void main(String[] args) {
String host = "localhost";
int port = 2401;
new SimpleServer(host,port);
}//main
}//class
My client class:
import java.net.*;
import java.io.*;
public class SimpleServerClient {
private Socket sock = null;
private PrintWriter out = null;
private BufferedReader in = null;
public SimpleServerClient (String host, int port) {
try {
sock = new Socket(host, port);
out = new PrintWriter(sock.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
// System.out.println("Connected to the: " + sock.getInetAddress() );
makeRequest("ECHO Howdy boy");
} catch (UnknownHostException e) {
System.err.println("Unknown host: "+host);
System.exit(2);
} catch (IOException e) {
System.err.println("I/O error for");
System.exit(3);
} catch (Exception exc) {
exc.printStackTrace();
System.exit(4);
}
}//constructor
private void makeRequest(String req) throws IOException {
System.out.println("Request: " + req);
out.println(req);
String resp = in.readLine();
System.out.println(resp);
disconnect();
}//method
public void disconnect() {
try {
out.close();
sock.close();
in.close();
} catch(IOException exc) {
System.out.println("Error while closing");
System.exit(5);
}
}//method
public static void main(String[] args) {
new SimpleServerClient("localhost", 2401);
}//main
}//class