Socket isvery useful in the communication between the processes in the same computer or in the network.
This is a simple example of a socket between 2 processes in Java. If you want to see the result, you can first start the provider.java and then start the requester.java.
Usefullinks:
English: http://stackoverflow.com/questions/1776457/java-client-server-application-with-sockets
French: http://www.siteduzero.com/informatique/tutoriels/introduction-aux-sockets-1*
Code:
Provider.Java
package Socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Provider {
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider() {}
void run() {
try {
//1. creating a server socket
this.providerSocket = new ServerSocket(2013, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
this.connection = this.providerSocket.accept();
System.out.println("Connection received from "
+ this.connection.getInetAddress().getHostName());
//3. get Input and Output streams
this.out = new ObjectOutputStream(this.connection.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(this.connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do {
try {
this.message = (String) this.in.readObject();
System.out.println("client>" + this.message);
if (this.message.equals("bye")) {
sendMessage("bye");
}
} catch (ClassNotFoundException classnot) {
System.err.println("Data received in unknown format");
}
} while (!this.message.equals("bye"));
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
//4: Closing connection
try {
this.in.close();
this.out.close();
this.providerSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
void sendMessage(final String msg) {
try {
this.out.writeObject(msg);
this.out.flush();
System.out.println("server>" + msg);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(final String args[]) {
Provider server = new Provider();
while (true) {
server.run();
}
}
}
Requester.Java
package Socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Requester {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester() {}
void run() {
try {
//1. creating a socket to connect to the server
this.requestSocket = new Socket("localhost", 2013);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
this.out = new ObjectOutputStream(this.requestSocket
.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(this.requestSocket.getInputStream());
//3: Communicating with the server
do {
try {
this.message = (String) this.in.readObject();
System.out.println("server>" + this.message);
sendMessage("Hi my server");
sendMessage("Hi my server2");
sendMessage("Hi my server3");
this.message = "bye";
sendMessage(this.message);
} catch (ClassNotFoundException classNot) {
System.err.println("data received in unknown format");
}
} while (!this.message.equals("bye"));
} catch (UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown host!");
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
//4: Closing connection
try {
this.in.close();
this.out.close();
this.requestSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
void sendMessage(final String msg) {
try {
this.out.writeObject(msg);
this.out.flush();
System.out.println("client>" + msg);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(final String args[]) {
Requester client = new Requester();
client.run();
}
}