How do I make a socket connection to a server?
Author: Deron Eriksson
Description: This Java tutorial describes how to make a socket connection to a server.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4


Page:    1 2 >

In another tutorial, we created a server to run on port 81 that would ask "What's your name?", take a name, and then respond with "Hello, [name]". We demonstrated interacting with this server by telnetting to localhost's port 81, where we could interact with the server. In this tutorial, we'll create a client class that creates a socket to interact with this server. The client will 'talk' to the server, receiving output from the server and sending input to the server.

For convenience, the server class is shown here:

TestServerSocket.java

package test;

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

public class TestServerSocket {

	public static void main(String args[]) throws IOException {
		final int portNumber = 81;
		System.out.println("Creating server socket on port " + portNumber);
		ServerSocket serverSocket = new ServerSocket(portNumber);
		while (true) {
			Socket socket = serverSocket.accept();
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os, true);
			pw.println("What's you name?");

			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String str = br.readLine();

			pw.println("Hello, " + str);
			pw.close();
			socket.close();

			System.out.println("Just said hello to:" + str);
		}
	}
}

The client class is shown below. It reads the output from the server (which is the input to the client), and displays the message. It takes input from the user ('userInput'), and sends this to the server. The output of the client is the input to the server. The client then displays the message that the server sends back. If the user types 'exit', the client closes the socket connection and exits.

TestClientSocket.java

package test;

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

public class TestClientSocket {

	public static void main(String args[]) throws IOException {
		final String host = "localhost";
		final int portNumber = 81;
		System.out.println("Creating socket to '" + host + "' on port " + portNumber);

		while (true) {
			Socket socket = new Socket(host, portNumber);
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

			System.out.println("server says:" + br.readLine());

			BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
			String userInput = userInputBR.readLine();

			out.println(userInput);

			System.out.println("server says:" + br.readLine());

			if ("exit".equalsIgnoreCase(userInput)) {
				socket.close();
				break;
			}
		}
	}
}

(Continued on page 2)

Page:    1 2 >