2013-03-17 120 views
0

我有3個java類。 堆棧 - 實現堆棧。 Stackserver - 實現基於用戶動作創建Stack對象並返回結果的服務器 StackClient - 實現發送操作請求並從服務器接收reslut的客戶端。Java Socket編程客戶端服務器通信問題

動作是推動,彈出,顯示和退出。

當我輸入動作時,它總是說無效的操作。 任何人都可以在程序中找到錯誤嗎?

Stack類

public class Stack 
{ 
    private int maxSize; 
    private int[] stackArray; 
private int top,i; 

public Stack(int s) 
{ 
     maxSize = s; 
     stackArray = new int[maxSize]; 
     top = -1; 
} 

public void push(int j) 
{ 
     stackArray[++top] = j; 
} 

    public String display() 
    { 
     i=top; 
     String s=""; 
     while(i>=0) 
     { 
      s=s+" "+stackArray[i--]; 
     } 
     return s; 
    }  

public int pop() 
{ 
     return stackArray[top--]; 
} 

public int peek() 
{ 
     return stackArray[top]; 
} 

public boolean isEmpty() 
{ 
     return (top == -1); 
} 

public boolean isFull() 
{ 
     return (top == maxSize - 1); 
} 
} 

StackServer類

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

public class StackServer 
{ 

ServerSocket server; 
Socket link; 
PrintWriter out; 
BufferedReader in; 

public void run() 
{ 
    try  
    { 

    System.out.println("Creating Server Socket."); 
     server = new ServerSocket(4000); 
    System.out.println("Successfully created Server Socket."); 

    Socket link; 
    System.out.println("Waiting For Connection."); 
    link = server.accept(); 
    System.out.println("Connection received from " + link.getInetAddress().getHostName()); 

    out = new PrintWriter(link.getOutputStream(),true); 
    in = new BufferedReader(new InputStreamReader(link.getInputStream()));   

    out.println("Eneter Stack Size."); 
    int size = Integer.parseInt((in.readLine()).trim()); 
    Stack stack = new Stack(size); 

    while(true) 
    { 
     String action=""; 
     int n; 

     out.println("Eneter Stack Operation."); 
     action = in.readLine(); 

     if(action == "push") 
     { 
      if(stack.isFull()) 
      { 
      out.println("Stack Full. What next?"); 
      } 
      else 
      { 
      out.println("Enter element: "); 
      n = Integer.parseInt((in.readLine()).trim()); 
      stack.push(n); 
      out.println("Pushed item. What next?"); 
      } 

     } 
     else if(action == "pop") 
     { 
      if(stack.isFull()) 
      { 
      out.println("Stack Full. What next?"); 
      } 
      else 
      { 
       n = stack.pop(); 
      out.println(n); 
      out.println("Pushed item. What next?"); 
      } 
     } 
     else if(action == "display") 
     { 
      out.println(stack.display()); 
     } 
     else if(action == "exit") 
     { 
      link.close(); 
      break; 
     } 
     else 
     { 
      out.println("Invalid Operation"); 
     } 
     } 
      } 
     catch(Exception ex) 
     { 
     System.out.println(ex); 
     } 
     } 

    public static void main(String [] args) 
    { 
    StackServer s = new StackServer(); 
    s.run();} 
    } 

StackClient類

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

public class StackClient 
{ 
public static void main(String [] args) 
{ 
    try  
    { 
      Socket client; 
     System.out.println("Creating Client Socket."); 
     client = new Socket("localhost",4000); 
    System.out.println("Successfully created Client Socket."); 

    BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
     PrintWriter out = new PrintWriter(client.getOutputStream(),true); 
     String action =""; 
     String size,s; 

    System.out.println(in.readLine()); 
    size = read.readLine(); 
     out.println(size); 

    while(true) 
    { 

     System.out.println(in.readLine()); 
     action = read.readLine(); 
     out.println(action); 

     if(action.equals("push")) 
     { 
       System.out.println(in.readLine()); 
     s=read.readLine(); 
     System.out.println(in.readLine()); 
     } 
     else if(action == "pop") 
     { 
     System.out.println(in.readLine()); 
     } 
     else if(action == "display") 
     { 
     System.out.println(in.readLine()); 
      } 
      else if(action == "exit") 
     { 
       client.close(); 
     break; 
     } 
     else 
     { 
     out.println("Invalid Operation from Client"); 
     } 
    } 
    } 
    catch(Exception ex) 
    { 
    System.out.println(ex); 
    } 
    } 
    } 
} 
+1

請將'字符串'與'等號'進行比較:p – 2013-03-17 14:59:37

+0

請參閱http://stackoverflow.com/q/513832/758280 – Jeffrey 2013-03-17 14:59:55

回答

5

使用String.equals()來比較字符串,而不是==

if(action == "push") 
... 
else if(action == "pop") 

==如果兩個對象是相同的實例,則返回true。你可以有兩個不同的String實例具有相同的值。