-1

Hi i am trying to make a multi threaded server client app where the server resends an upper cased version of the string sent by the client, here are my three classes:

The server class:

package Q2;

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

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

        ServerSocket serverSocket = null; 

        try { 
            serverSocket = new ServerSocket(10118); 
        } 
        catch (IOException e) 
        { 
            System.err.println("On ne peut pas écouter au  port: 10118."); 
            System.exit(1); 
        } 

        //Socket clientSocket = null; 
        System.out.println ("Le serveur est en marche, Attente de la connexion.....");

        try {
            MultiTh mt = new MultiTh(serverSocket.accept());
            Thread t = new Thread(mt);
            t.start();
            //clientSocket = serverSocket.accept(); 
        } 
        catch (IOException e) 
        { 
            System.err.println("Accept a échoué."); 
            System.exit(1); 
        } 

        System.out.println ("connexion réussie");
        System.out.println ("Attente de l'entrée.....");


    } 
} 

The client Class:

package Q2;

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

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

    String serverHostname = new String ("127.0.0.1");

    if (args.length > 0)
        serverHostname = args[0];
    System.out.println ("Essai de se connecter à l'hôte " +
    serverHostname + " au port 10118.");

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket(serverHostname, 10118);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Hôte inconnu: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        //System.err.println("Ne pas se connecter au serveur: " + serverHostname);
        System.exit(1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String userInput;
    System.out.print ("Entrée: ");
    while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
        System.out.print ("Entrée: ");
    }

    out.close();
    in.close();
    stdIn.close();
    echoSocket.close();
}
}

And the intermediate class that implements the runnable interface:

package Q2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;

public class MultiTh implements Runnable



{

private Socket soc;
public MultiTh(Socket s){
    this.soc = s;
}
public void run() {
    PrintWriter out = null; 
    BufferedReader in = null;
    try{
        out = new PrintWriter(soc.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader( soc.getInputStream()));

        String inputLine;


        while ((inputLine = in.readLine()) != null) 
        { 
            System.out.println ("Serveur: " + inputLine);
            inputLine = inputLine.toUpperCase();
            out.println(inputLine);
            if (inputLine.equals("Bye.")) 
                break; 


        }
        out.close();
        in.close();
        //soc.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 





}
}

Now i can open many client but the server only accepts and resends the string of the first one.

4

1 回答 1

0

问题是您的程序只接受一个连接然后死掉。您需要等待 ServerSocket.accept() 方法,因为客户端将使连接接受将返回 Socket。您可以将 Socket 传递给不同的线程以完成读/写操作。主线程可以返回阻塞接受()。

try {
       while(true) //Replace this with exit condition....exit condition for server not client.
       {
         Socket s = serverSocket.accept();
        MultiTh mt = new MultiTh(s);
        Thread t = new Thread(mt);
        t.start();
        //clientSocket = serverSocket.accept(); 
       } 
    } 
    catch (IOException e) 
    { 
        System.err.println("Accept a échoué."); 
        System.exit(1); 
    } 
于 2013-05-21T02:11:34.740 回答