import java.net.*;
import java.io.*;
public class SimpleServer
{
public static void main(String args[])
{
ServerSocket s = null;
try
{
s = new ServerSocket(9000);
}
catch (IOException e)
{
e.printStackTrace();
}
while (true)
{
try {
Socket s1 = s.accept();
System.out.println("Connection accepted: port=" + s1.getPort());
OutputStream s1out = s1.getOutputStream();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(s1out));
bw.write("Hello Net World!\n");
bw.close();
s1.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
Save :--SimpleClient.java
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) {
try {
Socket s1 = new Socket("localhost", 9000);
BufferedReader br = new BufferedReader(
new InputStreamReader(s1.getInputStream()));
System.out.println(br.readLine());
br.close();
s1.close();
} catch (ConnectException connExc) {
System.err.println("Could not connect to the server.");
} catch (IOException e)
{
}
}
}
how to use
1 :- open the command prompt
2 :- javac SimpleServer.java
3:- javac SimpleClient.java
4 :- java SimpleServer
5:- please open the new command prompt
6:- java SimpleClient