2014-11-03 71 views
0

每次從客戶端連接到服務器時,客戶端都會向服務器發送字符串消息「11」,並且當服務器收到字符串消息「11」時,它會運行count ++。然後出現了兩個連接,它應該使計數++的count = 2運行兩次,但是當客戶端連接時,我檢查了它並且客戶端正確地向服務器發送了字符串消息「11」,但計數保持爲1並且永遠不會輸入if(count == 2)塊。一直在測試和尋找小時,但似乎無法找到問題。請幫忙!謝謝爲什麼我的計數++在Thread類的run()方法中重置?

客戶端的代碼片段:

Socket s = new Socket(hostname, port); // Plug in the socket to connect to the server 
pw = new PrintWriter(s.getOutputStream()); //Instance of sending it out 
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 

public void run() { //Deal with reading from server and print them out 

    try { 
     pw.println("11"); //Sends the message when connection is made to the server 
     pw.flush(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try{ 

      while(true){ 

       String line = br.readLine(); //Read in the message from server 

       if(line.equals("12")){ //When finally receives the string message "12" from server 

        button.setBackground(Color.white); 
        button.addActionListener(sl); 

       } 

       int update = Integer.parseInt(line); 

       if(update < 10){ 
        current-= update; 
       } 
    } 

    } catch (IOException ioe){ 
     System.out.println("ioe in ChatClient.run: " + ioe.getMessage()); 
    } 
} 

Server線程的代碼片段:

PrintWriter pw = new PrintWriter(s.getOutputStream()); 

public void run(){ 
    try{ 

     BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); 

     while(true){ 
      String line = br.readLine(); //blocking //Keep reading in messages 

      if(line.equals("11")){ //Tested it out and does receive "11" whenever a client connects and prints out "11" 
       count++; //But the problem is count stays as 1 every time it connects and never becomes 2 
       System.out.println(line); 
       System.out.println(count); 
      } 

      if(count == 2){ //Never able to reach inside this block of code 
       pw.println("12"); 
       pw.flush(); 
       count++; 
      } 
     } 

    } catch(IOException ioe){ 
     System.out.println("ioe in ChatTHread: " + ioe.getMessage()); 
    } 
} 

編輯 - 服務器代碼:

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 
import java.util.Vector; 

public class Server { 

private int count = 0; 
private Vector<FFThread> ctVector = new Vector<FFThread>(); //Parametrized 

public Server(int port){ 

    try{ 

     ServerSocket ss = new ServerSocket(port); //Server socket to connect to the port 

     while(true){ 

      Socket s = ss.accept(); // Listens for a connection to be made to this "s" socket and accepts it. 


      FFThread ct = new FFThread(s, this); //Get new socket access thread 
      ctVector.add(ct); //Appends the specified element "ct" to the end of this Vector. 
      ct.start(); 
     } 

    } catch(IOException ioe){ 
     System.out.println("ioe in ChatServer: " + ioe.getMessage()); 
    } 
} 

public int counter(){ 
    this.count = 0; 
    count++; 
    return count; 
} 

public void sendMessage(String message, FFThread ct){ 
    for(FFThread c : ctVector){ 
     if(!c.equals(ct)){ //Two different clients 
      c.sendMessage(message); 
     } 
    } 
} 

public void removeThread(FFThread ct){ 
    ctVector.remove(ct); 
} 

public static void main(String [] args){ 
    Scanner scan = new Scanner(System.in); 
    System.out.print("What port? "); 
    int port = scan.nextInt(); 
    new Server(port); 
} 

}

編輯 - 服務器的線程類:

import java.awt.List; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.util.ArrayList; 


public class FFThread extends Thread { 

private Socket s; 
private Server cs; 
private PrintWriter pw; 
private int count = 0; 
boolean ready = false; 

public FFThread(Socket s, Server cs){ 

    this.s = s; 
    this.cs = cs; 

    try{ 

     this.pw = new PrintWriter(s.getOutputStream()); //Set up to send messages to clients 

    } catch(IOException ioe){ 
     System.out.println("ioe in ChatThread constructor: " + ioe.getMessage()); 
    } 
} 
+0

檢查變量'line'的長度是否爲2,因爲比較可能始終爲false,因此不會增加'count'。例如:「11」不等於「11」。 – SkyMaster 2014-11-03 06:52:01

+0

嘗試添加「其他」塊,看看你的假設是否正確。使用IDE進行調試也很有用。 – Jayan 2014-11-03 06:54:12

+0

@JoseLuis這似乎不是問題,因爲我用System.out.println(line)和System.out.println(count)對它進行了測試,並且它們是用字符串「11」打印出來的,但是伯爵總是保持1,證明他們能夠進入if區塊。 – 2014-11-03 07:07:41

回答

0

你的服務器級別的啓動爲每個傳入連接一個新的線程(的FFThread實例)。您的run方法FFThread確實是count++,但它必須在局部變量上執行,因爲它不訪問Server類中的count變量。因此,每個線程將自己的count0增加到1,並且它永遠不會達到兩個。

您的run方法應該增加(並測試)服務器實例的count變量,以使該計數達到2.您必須以線程安全方式(即使用同步方法)對其進行增加。

我相信加入以下到您的服務器類將工作:

private volatile int count = 0; 
... 
public synchronized void incCount() 
{ 
    count++; 
} 

public int getCount() 
{ 
    return count; 
} 

然後,在你FFThread類,使用this.cs.getCount()this.cs.incCount()閱讀和增加計數。

+0

你介意嗎?我很迷茫...什麼是最方便的方法呢? – 2014-11-03 07:56:57

+0

@OM編輯答案。 – Eran 2014-11-03 08:05:58

+0

它爲啓動的第二個客戶端工作,但第一個客戶端沒有啓動。我猜第一個仍然保持計數爲1,但一旦第二個客戶端的計數達到2,服務器是否應該向兩個客戶端發送字符串「12」的消息? – 2014-11-03 08:15:13

相關問題