Posted by : Unknown
Friday, 26 June 2015
Client Side Program
import java.io.*;
import java.net.*;
class ChatClient extends Thread
{
Socket s;
static PrintWriter out;
static BufferedReader read,in;
Thread t;
ChatClient() throws Exception
{
s = new Socket("127.0.0.1",5000);
System.out.println("client connected to server");
read = new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
t=new Thread(this);
t.start();
}
public void run()
{
String str1;
while(true)
{
try
{
str1=in.readLine();
System.out.println("\n"+"From server: "+str1);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[]) throws Exception
{
String str2;
new ChatClient();
while(true)
{
str2=read.readLine();
out.println(str2);
out.flush();
}
}
}
Server Side Program
import java.io.*;
import java.net.*;
class ChatServerTCP extends Thread
{
ServerSocket server;
Socket s;
static PrintWriter out;
static BufferedReader read,in;
Thread t;
ChatServerTCP() throws Exception
{
server = new ServerSocket(5000);
System.out.println("Server is waiting for client");
s=server.accept();
System.out.println("connection is established "+s.getInetAddress());
read = new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
t=new Thread(this);
t.start();
}
public void run()
{
String str1;
while(true)
{
try
{
str1=in.readLine();
System.out.println("\n"+"From client: "+str1);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[]) throws Exception
{
ChatServerTCP cs = new ChatServerTCP();
String str2;
while(true)
{
str2=read.readLine();
out.println(str2);
out.flush();
}
}
}
import java.net.*;
class ChatServerTCP extends Thread
{
ServerSocket server;
Socket s;
static PrintWriter out;
static BufferedReader read,in;
Thread t;
ChatServerTCP() throws Exception
{
server = new ServerSocket(5000);
System.out.println("Server is waiting for client");
s=server.accept();
System.out.println("connection is established "+s.getInetAddress());
read = new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
t=new Thread(this);
t.start();
}
public void run()
{
String str1;
while(true)
{
try
{
str1=in.readLine();
System.out.println("\n"+"From client: "+str1);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[]) throws Exception
{
ChatServerTCP cs = new ChatServerTCP();
String str2;
while(true)
{
str2=read.readLine();
out.println(str2);
out.flush();
}
}
}
How to run:
- First run server program then next run client program
- Give input from client side
- Next give input in server side
Client Side Output:
client connected to server
hi
From server: hi da
how r u?
From server: fine
hi
From server: hi da
how r u?
From server: fine
Server Side Output:
Server is waiting for client
connection is established /127.0.0.1
From client: hi
hi da
From client:
From client: how r u?
fine
Related Posts :
- Back to Home »
- Client Server Program »
- Client / Server Chatting Program Using TCP