[solved] Noob trying to setup a client/server.

Hello JGO, I’m trying to write a simple project that contains 2 packages, one for the client and the other for the server.

I was wondering how to do this?

Obviously the server is setup and broadcasted first, than awaits for a client to connect.

Once the client is connected I setup I/O and attempt to send the client a message but I’m stuck.

I have the server setup perfectly.


ServerCore -> Attempting to broadcast on 'localhost : 8080'
ServerCore -> Successfuilly broadcasting, awaiting a client....
ServerCore -> A client has connected, setting up client IO
ServerCore -> Client IO successfully setup.
ServerCore -> Attempting to send client a message...

But I can’t seem to grasp how to program the client?

Here’s my client class:


public class ClientCore {

	public static void main(String[] args) throws IOException {
		Socket clientSocket = new Socket("localhost", 8080);
		PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
		BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		/*
		 * Attempt to receive our initial message.
		 */
		String line = "";
		while ( (line = in.readLine()) !=null){
			System.out.println(line);
		}
	}
}

Here’s my server class:


public class ServerCore {
	
	/*
	 * Variables.
	 */
	private String host;
	private int port;
	private int maxConnections;

	/*
	 * Main entry point into the application.
	 */
	public static void main(String[] args) {
		new ServerCore("localhost", 8080, 5).start();
	}

	public ServerCore(String host, int port, int maxconnections) {
		this.host = host;
		this.port = port;
		this.maxConnections = maxconnections;
		// TODO: Implement multi-threaded later.
	}

	private void start() {
		try {
			print("Attempting to broadcast on '" + host + " : " + port + "'");
			ServerSocket serverSocket = new ServerSocket(port, Integer.MAX_VALUE, InetAddress.getByName(host));
			
			print("Successfuilly broadcasting, awaiting a client....");
			Socket clientSocket = serverSocket.accept();
			
			print("A client has connected, setting up client IO");
			PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			print("Client IO successfully setup.");

			print("Attempting to send client a message...");
			out.write("Hey bro");
			out.flush();
		} catch (BindException e) {
			System.err.println("Server address already in use, will now exit!");
			Runtime.getRuntime().exit(0);
		}catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void print(String msg) {
		System.out.println("ServerCore -> " + msg);
	}
}

After booting up the client it gives me this error:


java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(Unknown Source)
	at java.net.SocketInputStream.read(Unknown Source)
	at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
	at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
	at sun.nio.cs.StreamDecoder.read(Unknown Source)
	at java.io.InputStreamReader.read(Unknown Source)
	at java.io.BufferedReader.fill(Unknown Source)
	at java.io.BufferedReader.readLine(Unknown Source)
	at java.io.BufferedReader.readLine(Unknown Source)
	at org.gls.client.ClientCore$1.run(ClientCore.java:24)
	at java.lang.Thread.run(Unknown Source)