Simple Client Server Using Java
SERVER
import java.io.*;
import java.net.*;
class server {
Thread t;
String name;
try {
static ServerSocket s = new ServerSocket(8083);
static Socket socket;
}
catch () {}
server(String threadname) {
name = threadname;
t = new Thread(name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
public void run() {
while (true) {
try {
BufferedReader input;
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String message = input.readLine();
if (message == null) {
break;
}
System.out.println(message);
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
public static void main(String[] args) throws UnknownHostException,
IOException {
int i = 0;
while (!s.accept().equals(socket)) {
new server("a" + i);
}
}
}
CLIENT
import java.net.*;
import java.io.*;
class client {
public static void main(String[] args) throws UnknownHostException,
IOException {
System.out.println("Hello World!");
Socket s = new Socket("localhost", 8083);
String lineToBeSent;
BufferedReader input;
PrintWriter output;
input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(s.getOutputStream(), true);
while (true) {
lineToBeSent = input.readLine();
if (lineToBeSent.equals(".")) {
break;
}
output.println(lineToBeSent);
}
}
}
Simple Client Server Using Java