Client.java
import java.net.*;
import java.io.*;
class Client
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("localhost",500);
System.out.println("Client send request to server!");
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
while(true)
{
System.out.println("Client:");
dos.writeUTF(br.readLine()); //send response to server. System.out.println("Server:");
System.out.println(dis.readUTF()); //Received from server. }
}
}
Server.java
import java.net.*;
import java.io.*;
class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(500);
System.out.println("Wait for client!");
Socket s=ss.accept();
System.out.println("Server accpted request");
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
53 InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
while(true)
{
System.out.println("Client:");
System.out.println(dis.readUTF()); //Receives info from client. System.out.println("Server:");
dos.writeUTF(br.readLine()); //sends response to client }
}
}