sraveend
asked on
java programming in network
Server program................... .......... .....
import java.net.*;
import java.io.*;
public class lab8S
{
public static final void main(String[] args)
{
while(true){
try{
ServerSocket ss=new ServerSocket(2002);
new process1(ss.accept()).star t();
try
{
Thread.currentThread().sle ep(100);
}catch(InterruptedExceptio n ie)
{
}
}
catch(IOException e)
{
}
}
}
}
class KeyboardInThread extends Thread {
PrintWriter pout = null;
OutputStream out;
BufferedReader sIn = new BufferedReader(new InputStreamReader(System.i n));
public KeyboardInThread(OutputStr eam out) {
this.out = out;
}
public void run() {
while(true) {
try {
pout = new PrintWriter(out, true);
pout.println(sIn.readLine( ));
pout.flush();
}catch(IOException ioe) {
}
}
}
}
class ScreenOutThread extends Thread {
InputStream in;
BufferedReader br=null;
public ScreenOutThread(InputStrea m inB) {
in = inB;
}
public void run() {
while(true) {
try {
br=new BufferedReader(new InputStreamReader(in));
System.out.println(br.read Line());
}catch(Exception ioe) {}}
}
}
class process1 extends Thread
{
private Socket s;
public process1(Socket soc)
{
s=soc;
}
public final void run()
{
while(true)
{
try{
new ScreenOutThread(s.getInput Stream()). start();
new KeyboardInThread(s.getOutp utStream() ).start();
try{
Thread.currentThread().sle ep(1000);
}catch(InterruptedExceptio n e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}
Client program
import java.net.*;
import java.io.*;
public class dlab8c
{
public static final void main(String[] args)
{
try{
Socket eSocket = null;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.i n));
System.out.println("Enter address of a server");
eSocket = new Socket(InetAddress.getByNa me(stdIn.r eadLine()) ,2002);
new proc(eSocket).start();
try
{
Thread.currentThread().sle ep(100);
}catch(InterruptedExceptio n ie)
{
}
}
catch(IOException e)
{
}
}
}
class KThread extends Thread {
PrintWriter pout = null;
OutputStream out;
BufferedReader sIn = new BufferedReader(new InputStreamReader(System.i n));
public KThread(OutputStream out) {
this.out = out;
}
public void run() {
while(true) {
try {
pout = new PrintWriter(out, true);
pout.println(sIn.readLine( ));
pout.flush();
}catch(IOException ioe) {
}
}
}
}
class SThread extends Thread {
InputStream in;
BufferedReader br=null;
public SThread(InputStream inB) {
in = inB;
}
public void run() {
while(true){
try {
br=new BufferedReader(new InputStreamReader(in));
System.out.println(br.read Line());
}catch(Exception ioe) {}
}
}
}
class proc extends Thread
{
private Socket s;
public proc(Socket soc)
{
s=soc;
}
public final void run()
{
while(true)
{
try{
new KThread(s.getOutputStream( )).start() ;
new SThread(s.getInputStream() ).start();
try{
Thread.currentThread().sle ep(100);
}catch(InterruptedExceptio n e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}
Please help to make the above server and client program to work properly.It just shows communication between server and client.
import java.net.*;
import java.io.*;
public class lab8S
{
public static final void main(String[] args)
{
while(true){
try{
ServerSocket ss=new ServerSocket(2002);
new process1(ss.accept()).star
try
{
Thread.currentThread().sle
}catch(InterruptedExceptio
{
}
}
catch(IOException e)
{
}
}
}
}
class KeyboardInThread extends Thread {
PrintWriter pout = null;
OutputStream out;
BufferedReader sIn = new BufferedReader(new InputStreamReader(System.i
public KeyboardInThread(OutputStr
this.out = out;
}
public void run() {
while(true) {
try {
pout = new PrintWriter(out, true);
pout.println(sIn.readLine(
pout.flush();
}catch(IOException ioe) {
}
}
}
}
class ScreenOutThread extends Thread {
InputStream in;
BufferedReader br=null;
public ScreenOutThread(InputStrea
in = inB;
}
public void run() {
while(true) {
try {
br=new BufferedReader(new InputStreamReader(in));
System.out.println(br.read
}catch(Exception ioe) {}}
}
}
class process1 extends Thread
{
private Socket s;
public process1(Socket soc)
{
s=soc;
}
public final void run()
{
while(true)
{
try{
new ScreenOutThread(s.getInput
new KeyboardInThread(s.getOutp
try{
Thread.currentThread().sle
}catch(InterruptedExceptio
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}
Client program
import java.net.*;
import java.io.*;
public class dlab8c
{
public static final void main(String[] args)
{
try{
Socket eSocket = null;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.i
System.out.println("Enter address of a server");
eSocket = new Socket(InetAddress.getByNa
new proc(eSocket).start();
try
{
Thread.currentThread().sle
}catch(InterruptedExceptio
{
}
}
catch(IOException e)
{
}
}
}
class KThread extends Thread {
PrintWriter pout = null;
OutputStream out;
BufferedReader sIn = new BufferedReader(new InputStreamReader(System.i
public KThread(OutputStream out) {
this.out = out;
}
public void run() {
while(true) {
try {
pout = new PrintWriter(out, true);
pout.println(sIn.readLine(
pout.flush();
}catch(IOException ioe) {
}
}
}
}
class SThread extends Thread {
InputStream in;
BufferedReader br=null;
public SThread(InputStream inB) {
in = inB;
}
public void run() {
while(true){
try {
br=new BufferedReader(new InputStreamReader(in));
System.out.println(br.read
}catch(Exception ioe) {}
}
}
}
class proc extends Thread
{
private Socket s;
public proc(Socket soc)
{
s=soc;
}
public final void run()
{
while(true)
{
try{
new KThread(s.getOutputStream(
new SThread(s.getInputStream()
try{
Thread.currentThread().sle
}catch(InterruptedExceptio
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}
Please help to make the above server and client program to work properly.It just shows communication between server and client.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
r u going to make chat server application?
ASKER
yes
ASKER
The server and client will need to be threaded in such a way that they can support multiple simultaneous clients and i want to tie the server and client together in such a way that any time the server executes a out.println(); it prints to the client's screen, and any time the server executes a in.readLine() method, it is reading from the client's keyboard
didn't the code i provided suffice your needs !!!!!