java programming in network

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 process(ss.accept()).start();
try
{
Thread.currentThread().sleep(100);
}catch(InterruptedException ie)
{
}

}
catch(IOException e)
{
}
}
}
}
class KeyboardInThread extends Thread {
      PrintWriter out = null;
      BufferedReader sIn = new BufferedReader(new InputStreamReader(System.in));
    public KeyboardInThread(PrintWriter out) {
            this.out = out;
    }

      public void run() {

                  try {
                        out.println(sIn.readLine());
                        out.flush();
                        try{
                        Thread.currentThread().sleep(100);
                        }catch(InterruptedException e)
    {}
                  }catch(IOException ioe) {
                        }
      }
}
class ScreenOutThread extends Thread {
      BufferedReader in;
    public ScreenOutThread(BufferedReader inB) {
            in = inB;
    }

      public void run() {

                  try {
                        System.out.println(in.readLine());

                  }catch(IOException ioe) {
                                          }
      }
}
class process extends Thread
{
private Socket s;
public process(Socket soc)
{
s=soc;
}

public final void run()
{
BufferedReader inB=null;

PrintWriter ot1 = null;
String machineName;
try
{
ot1 = new PrintWriter(s.getOutputStream(), true);
inB=new BufferedReader(new InputStreamReader(s.getInputStream()));
//machineName = InetAddress.getLocalHost().getHostName();
//System.out.println("Machine Name: " + machineName);
}
catch(IOException eo)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
while(true)
{
try{

new ScreenOutThread(inB).start();
new KeyboardInThread(ot1).start();
try{
Thread.currentThread().sleep(100);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}




import java.io.*;
import java.net.*;

public class lab8C {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
            String machineName=null;
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        try {
                  System.out.println("Enter address of a server");
            echoSocket = new Socket(InetAddress.getByName(stdIn.readLine()),2002);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
            machineName = InetAddress.getLocalHost().getHostName();
                  System.out.println("Machine Name: " + machineName);

        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to");
            System.exit(1);
        }


try {
while(true){
                  out.println( stdIn.readLine());
                  System.out.println( machineName+in.readLine());
             out.flush();
            }
            }catch (IOException ioe) {
            }


    }
}


Iam getting errors from the above program ,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.
I just want to make both the server & client  threaded in such a way that they can support multiple simultaneous clients .
sraveendAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
first of al u r creating a new Socket in each while loop! ur main method in class lab8S should be:

ServerSocket ss=new ServerSocket(2002);
while(true){
try{
new process(ss.accept()).start();
try
//Continue
0
petmagdyCommented:
correction it is:

ServerSocket ss=new ServerSocket(2002);
new process(ss.accept()).start();

while(true){
try{
ry
//Continue
0
petmagdyCommented:
sorry please neglect my comments
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

petmagdyCommented:
sorry Again my first comment was correct, please review this sample it is multithreaded Socket Server:

http://utenti.lycos.it/yanorel6/2/ch32.htm#Heading3
0
petmagdyCommented:
sorry again, It seems I must go to bed ;) the URL is:

http://utenti.lycos.it/yanorel6/2/

Read the title "A Multithreaded Server Framework"
0
Mayank SAssociate Director - Product EngineeringCommented:
I would prefer to make a class which implements Runnable instead of extending Thread. Something like:

ServerSocket ssListener = new ServerSocket ( port ) ;
boolean bRunning = true ;

while ( bRunning )
  new ServerThread ( ssListener.accept () ) ; // end while

....
....

class ServerThread impements Runnable
{
  private Socket sClient ;

  public ServerThread ( Socket sClient )
  {
    this.sClient = sClient ;
    Thread t = new Thread ( this ) ;
    t.start () ;

  } // end of constuctor ()

  public void run ()
  {
    // do your stuff

  } // end of run ()

} // class definition over
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sraveendAuthor Commented:
I've changed my server program to
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() {
try {
pout = new PrintWriter(out, true);
pout.println(sIn.readLine());
pout.flush();
try{
Thread.currentThread().sleep(100);
}catch(InterruptedException e)
{}
}catch(IOException ioe) {}
}
}

class ScreenOutThread extends Thread {
InputStream in;
BufferedReader br=null;
public ScreenOutThread(InputStream inB) {
in = inB;
}
public void run() {
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(100);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}
}
}
}


My program works correctly,what's th problem is it's sleeping more time,i couldn't recieve reply from server immediately?
(Using implementaion instead of extend there's not  much difference)



0
sraveendAuthor Commented:
Please help me to apply threading concept properly?
0
Mayank SAssociate Director - Product EngineeringCommented:
>> ServerSocket ss=new ServerSocket(2002);

Again inside the while loop. We told you to put it outside (before) the loop.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.