//Java program to implement client server connection using socket.
//Socket programming in Java.
Server.java
import java.io.*;
import java.net.*;
class Server
{
ServerSocket ss;
Socket S;
BufferedReader br;
String str;
public Server()
{
try
{
ss=new ServerSocket(1025);
S=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream()));
System.out.println("Connected!!!");
do
{
str=br.readLine();
System.out.println(str);
}
while(!str.equals("e"));
ss.close();
}
catch(Exception ex)
{
}
}
public static void main(String s[]) throws Exception
{
new Server();
}
}
Client.java
import java.io.*;
import java.net.*;
class Client
{
public static void main(String s[]) throws Exception
{
try
{
Socket s1=new Socket("localhost",1025);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line;
System.out.println("Enter the string, e for exit");
do
{
line=br.readLine();
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s1.getOutputStream()),true);
pw.println(line);
}
while(!line.equals("Exit"));
s1.close();
}
catch(Exception ex)
{
System.out.println("Cannot Find Server");
}
}
}