Source code for client server connection program by socket programming in Java?
Apologies in advance for the lengthy example. Below is an example of a client-server pair sending each other a message. This shows Java's TCP implementation of Server/ServerSocket classes because they're very simple and easy to use.
A few things to be aware of when testing these classes out:
- You can start one of these classes with TCPServer.start() or TCPClient.start().
- Always start the TCPServer class before the TCPClient class. If you don't, it's possible that the TCPClient will throw an exception and exit quietly, while the TCPServer class waits forever for a client to connect.
- The current setup will connect on 127.0.0.1, which is your own computer. This will work even if you have no network/internet connection. If you wish to connect to a remote computer, change the TCPClient.HOST value to the IP address (or hostname) of the computer that will be running the server.
// Code to run both parts of the program from the same computer.
// (This will make for some jumbled output)
class Main {
public static void main(String[] args) {
new TCPServer().start();
new TCPClient().start();
}
}
class TCPClient extends Thread {
// Connection properties
private static final String HOST = "127.0.0.1";
private static final int PORT = 56565;
public void run() {
// Socket class used for TCP connections
Socket sock = null;
// I/O components
BufferedReader input = null;
BufferedWriter output = null;
try {
// Connect our socket to the server.
sock = new Socket(HOST, PORT);
// Use a BufferedReader to read data from the server.
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
// Use a BufferedWriter to send data to the server.
output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
// Send some data to the server.
String toServer = "Are you there, Server?";
System.out.println("ToServer: " + toServer);
output.write(toServer);
output.newLine();
output.flush();
// Wait for a response from the server and display it.
String fromServer = input.readLine();
System.out.println("FromServer: " + fromServer);
} catch (final IOException ex) {
} finally {
// Do our best to ensure a clean close procedure.
// Closing the socket will also close input and output.
try {
if (sock != null) {
sock.close();
}
} catch (final IOException ex) {
}
}
}
}
class TCPServer extends Thread {
// Connection properties
private static final int PORT = 56565;
public void run() {
// ServerSocket class used to accept TCP connections
ServerSocket server = null;
Socket sock = null;
// I/O components
BufferedReader input = null;
BufferedWriter output = null;
try {
// Create our server on the given port.
server = new ServerSocket(PORT);
// Wait for a client to connect to us.
sock = server.accept();
// Use a BufferedReader to read data from the client.
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
// Use a BufferedWriter to send data to the client.
output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
// Wait for the client to talk to us.
String fromClient = input.readLine();
System.out.println("FromClient: " + fromClient);
// Send them a response.
String toClient = "Yes, I'm here, Client.";
System.out.println("ToClient: " + toClient);
output.write(toClient);
output.newLine();
output.flush();
} catch (final IOException ex) {
} finally {
// Do our best to ensure a clean close procedure.
// Closing the socket will also close input and output.
try {
if (sock != null) {
sock.close();
}
} catch (final IOException ex) {
}
// Close the server socket, as well.
try {
if (server != null) {
server.close();
}
} catch (final IOException ex) {
}
}
}
}