我的多线程服务器无法运行

时间:2013-05-21 02:04:38

标签: java multithreading sockets client

您好我正在尝试创建一个多线程服务器客户端应用程序,其中服务器重新发送客户端发送的字符串的高级版本,这是我的三个类:

服务器类:

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.....");


    } 
} 

客户类:

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();
}
}

实现runnable接口的中间类:

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();
    } 





}
}

现在我可以打开许多客户端,但服务器只接受并重新发送第一个客户端的字符串。

1 个答案:

答案 0 :(得分:0)

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

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); 
    }