2013-11-23 155 views
0

我正在實現一個多線程服務器的客戶端 - 服務器程序,它有一個ArrayList,各種客戶端向其發送數據並將它們添加到ArrayList。線程加入多線程服務器

客戶:

package p2pclient; 

import java.io.*; 
import java.net.*; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class client { 
    public static void main(String[] args) { 
     Socket smtpSocket = null; 
     DataOutputStream os = null; 
     DataInputStream is = null; 
     Scanner s = new Scanner(System.in); 

     try { 
      smtpSocket = new Socket("localhost", 8888); 
      os = new DataOutputStream(smtpSocket.getOutputStream()); 
      is = new DataInputStream(smtpSocket.getInputStream()); 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host: hostname"); 
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for the connection to: hostname"); 
     } 

     if (smtpSocket != null && os != null && is != null) { 
      try { 
       while(true) 
       { 
        os.flush(); 
        System.out.println("enter line"); 
        String a = s.nextLine(); 
        os.writeBytes(a + "\n"); 

        System.out.println("wrote bytes"); 
        String responseLine; 

        responseLine = is.readLine(); 
        System.out.println("Server: " + responseLine); 
        if (responseLine.indexOf("Ok") != -1) { 
         System.out.println("breaking"); 
         break; 
        } 
       } 
       os.close(); 
       is.close(); 
       smtpSocket.close(); 
      } catch (UnknownHostException e) { 
       System.err.println("Trying to connect to unknown host: " + e); 
      } catch (IOException e) { 
       System.err.println("IOException: " + e); 
      } 
     } 
     System.out.println("out of while"); 
    }   
} 

Server線程:

package server; 

import java.io.*; 
import java.net.*; 
import java.util.*; 
import java.util.concurrent.CopyOnWriteArrayList; 

public class ServerThread { 

    public static void main(String args[]) throws InterruptedException { 
     ServerSocket echoServer = null; 
     Scanner s = new Scanner(System.in); 
     String line; 
     ArrayList<String> add_list = new ArrayList<String>(); 
     Collections.synchronizedList(add_list); 
     int number=0; 
     DataInputStream is; 
     PrintStream os; 
     Socket clientSocket = null; 
     try { 
      echoServer = new ServerSocket(8888); 
     } 
     catch (IOException e) { 
      System.out.println(e); 
     } 

     List<Thread> threads = new ArrayList<Thread>(); 
     while(true) { 
      try { 
       clientSocket = echoServer.accept(); 

       /* start a new thread to handle this client */ 
       number++; 

       Thread t = new Thread(new ClientConn(clientSocket,number,add_list)); 
       t.start(); 
       threads.add(t); 

      }catch (IOException e) { 
       System.err.println("Accept failed."); 
       System.err.println(e); 
       System.exit(1); 
      } 
      for (Thread t2 : threads) 
      { 
       t2.join(); 
      } 
      System.out.println(add_list); 
     }  
    } 
} 

服務器運行進程:

package server; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.Socket; 
import java.util.ArrayList; 

class ClientConn implements Runnable { 

    Socket client; 
    DataInputStream in; 
    DataOutputStream out; 
    int s; 
    ArrayList<String> al = new ArrayList<String>(); 

    ClientConn(Socket client,int s, ArrayList<String> al){ 
     this.client = client; 
     this.s = s; 
     this.al = al; 

     try { 
      in = new DataInputStream(client.getInputStream()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      out = new DataOutputStream(client.getOutputStream()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @SuppressWarnings("deprecation") 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     String response = ""; 

     try { 
      while (true) { 
      response = ""; 
      response = in.readLine(); 
      out.writeBytes(response+"\n"); 
      if (response.indexOf("Ok") == -1) 
       al.add(response); 
      // System.out.println(response); 
      out.flush(); 
      if (response.indexOf("Ok") != -1) { 
       //System.out.println("breaking"); 
       break; 
      } 
     } 
     } catch (IOException e) { 
      System.err.println(e); 
     } 
    } 
} 

我正在嘗試將所有線程放入一個ArrayList中,並加入主線程上的每個線程。這不起作用。相反,每個線程在第一個線程上等待。

如何讓主線程在打印數組列表之前等待其他線程結束?

+0

我不明白 - 你等待接受(),當客戶端連接,創建一個客戶端<>服務器線程立即等待它來完成?在第一個客戶端線程終止之前,您不會再接受()。 –

回答

0

嘗試改變while迴路Server主題到:

while(true) { 
    try { 
     clientSocket = echoServer.accept(); 
     number++; 
     Thread t = new Thread(new ClientConn(clientSocket,number,add_list)); 
     t.start(); 
     t.join(); 
     threads.add(t); 
    } catch (IOException e) { 
      System.err.println("Accept failed."); 
      System.err.println(e); 
      System.exit(1); 
    } 
    System.out.println(add_list); 
}