Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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 .
0
sraveend
Asked:
sraveend
  • 5
  • 2
  • 2
2 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now