Link to home
Start Free TrialLog in
Avatar of sraveend
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()).start();
try
{
Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
}
class KeyboardInThread extends Thread {
      PrintWriter pout = null;
      OutputStream out;
      BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in));
    public KeyboardInThread(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 ScreenOutThread extends Thread {
      InputStream in;
      BufferedReader br=null;
    public ScreenOutThread(InputStream inB) {
            in = inB;
    }
      public void run() {
            while(true) {

               try {
                        br=new BufferedReader(new InputStreamReader(in));
                        System.out.println(br.readLine());
                        }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.getInputStream()).start();
new KeyboardInThread(s.getOutputStream()).start();
try{
Thread.currentThread().sleep(1000);
}catch(InterruptedException 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.in));
System.out.println("Enter address of a server");
eSocket = new Socket(InetAddress.getByName(stdIn.readLine()),2002);
new proc(eSocket).start();
try
{
Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
class KThread extends Thread {
      PrintWriter pout = null;
      OutputStream out;
      BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in));
    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.readLine());

                        }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().sleep(100);
}catch(InterruptedException 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.








ASKER CERTIFIED SOLUTION
Avatar of CodingExperts
CodingExperts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naeemg
Naeemg

r u going to make chat server application?
Avatar of sraveend

ASKER

yes
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 !!!!!