Category Archives: sockets

JAVA : Socket Basics – Loop

Server.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private int port = 45234;
	ServerSocket serverSocket;
	Socket clientSocket;
	private PrintWriter out;
	private BufferedReader in;
	private String inputLine;
	private String outputLine;

	private void start() {
		try {
			serverSocket = new ServerSocket(port);
			clientSocket = serverSocket.accept();
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			while ((inputLine = in.readLine()) != null) {
				if (inputLine.equals("bye")) {
					out.println("closing");
					break;
				}
				out.println("srv: " + inputLine);
			}
			System.out.println("server closing");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
				clientSocket.close();
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Server().start();
	}

}

Client.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

	private int port = 45234;
	private Socket clientSocket;
	private PrintWriter out;
	private BufferedReader in;
	private BufferedReader stdIn;
	private String inputLine;
	private String outputLine;
	private void start() {

		try {
			clientSocket = new Socket("localhost", port);
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			stdIn = new BufferedReader(new InputStreamReader(System.in));
			while ((inputLine = stdIn.readLine()) != null) {
				out.println(inputLine);
				outputLine = in.readLine();
				if(outputLine.equals("closing")) {
					break;
				}
				System.out.println(outputLine);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
				stdIn.close();
				clientSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Client().start();
	}

}

JAVA : Socket Basics

Server

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {
	private ServerSocket serverSocket;
	private Socket clientSocket;
	private PrintWriter out;
	private BufferedReader in;
	private String inputLine;
	private String outputLine;

	public void runServer() {
		try {
			serverSocket = new ServerSocket(55556);
			clientSocket = serverSocket.accept();
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			out.println("Test Server...");
		} catch (Exception ex) {
			System.err.println("err in server");
		} finally {
			try {
				out.close();
				in.close();
				clientSocket.close();
				serverSocket.close();
			} catch (Exception e) {				
				System.err.println("err res closing in server");
			}
		}
	}

	public static void main(String[] args) {
		new TestServer().runServer();
	}

}

Client

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TestClient {

	Socket socket;
	PrintWriter out;
	BufferedReader in;
	String fromServer;

	public void runClient() {
		try {
			socket = new Socket("myComp", 55556);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			if ((fromServer = in.readLine()) != null) {
				System.out.println("from server" + fromServer);
			}
		} catch (Exception e) {
			System.err.println("err in client");
		} finally {
			try {
				out.close();
				in.close();
				socket.close();
			} catch (Exception e) {
				System.err.println("err res closing in client");
			}
		}
	}

	public static void main(String[] args) {
		new TestClient().runClient();
	}

}

Change myComp to your Computer name, to find the name of your computer run following code.

String computername = InetAddress.getLocalHost().getHostName();

Original Reference
All About Sockets – Oracle Tutorials