2013-07-10 3 views
0

join 버튼을 클릭 할 때 서버에 데이터를 보내야하지만 데이터를 서버로 보내지 않고 콘솔에 메시지를 인쇄하지 않습니다. 왜?Java 서버가 클라이언트로부터 데이터를 수신하지 않습니다.

서버

package clientServer; 
import java.net.*; 
import java.io.*; 

public class Server { 
    private ServerView view; 



    private boolean serverOnline=false; 
    private ServerSocket server; 
    private InputStream serverInStream; 

    public Server(ServerView view) 
    { 
     this.view=view; 
    } 


    public void start() 
    { 
     //Manipulate model 
     System.out.println("Server is started"); 
     //Optionally update view 


     Socket listenPort; 
     try 
     { 
      this.server=new ServerSocket(13131); 

      while(this.serverOnline) 
      { 
       listenPort=this.server.accept(); 

       this.serverInStream=listenPort.getInputStream(); 

       BufferedReader bfw=new BufferedReader(new InputStreamReader(this.serverInStream)); 
       System.out.println(bfw.readLine()); 

     this.serverInStream.close(); 

      } 
     } 
     catch(IOException e) 
     { 
      System.err.println(e); 
     } 
     finally 
     { 
      this.serverOnline=true; 
     } 
    } 

    public void stop() 
    { 
     try 
     { 
     this.serverOnline=false; 
     this.server.close(); 
     } 
     catch(IOException e) 
     { 
      System.err.println("Problem in stopping server" + e); 
     } 
     finally 
     { 
      System.out.println("Server has been stopped"); 
     } 
    } 

} 

ServerView

package clientServer; 

import javax.swing.*; 
import java.awt.*; 

public class ServerView { 

    private JFrame window; 
    private Container holder; 
    private JButton serverButton; 
    private JLabel label; 
    private JPanel panel; 
    private JButton serverJoinButton; 
    private ServerController controller; 

    public ServerView(ServerController controller) { 
     this.controller = controller; 
     this.window = new JFrame("Twenty nine"); 

     this.panel = new JPanel(); 
     this.holder = this.window.getContentPane(); 

     this.serverButton = new JButton("start"); 
     this.serverButton.setActionCommand("start"); 
     this.serverButton.addActionListener(this.controller); 

     this.label = new JLabel("Serever is offline"); 

     this.holder.add(this.panel); 

     this.panel.add(this.label); 
     this.panel.add(this.serverButton); 

     this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.window.setSize(800, 900); 
     // this.window.setLayout(new BorderLayout()); 
     this.window.setVisible(true); 

    } 

    public void start() { 
     this.label.setText("Server is online"); 
     this.serverButton.setActionCommand("stop"); 
     this.serverButton.setText("stop"); 

     //Adds join buttton 
     this.serverJoinButton = new JButton("Join"); 
     this.serverJoinButton.setText("join"); 
     this.serverJoinButton.addActionListener(this.controller); 

     this.panel.add(this.serverJoinButton); 
     //this.panel.repaint(); 
     this.panel.revalidate(); 
    } 

    public void stop() 
    { 
     this.label.setText("Server is offline"); 
     this.serverButton.setActionCommand("start"); 
     this.serverButton.setText("start"); 

     this.panel.remove(this.serverJoinButton); 

     this.panel.repaint(); //Adding works properly removing dont 
     this.panel.revalidate(); 
    } 
} 

ServerController

package clientServer; 

import java.awt.event.*; 

public class ServerController implements ActionListener { 

    private Server model; 
    private ServerView view; 

    public void setModel(Server server) { 
     this.model = server; 

    } 

    public void setView(ServerView view) { 
     this.view = view; 

    } 

    public void actionPerformed(ActionEvent e) { 
     if(e.getActionCommand()=="start") 
     { 
      this.start(); 
     } 
     else if(e.getActionCommand()=="stop") 
     { 
      this.stop(); 
     } 
     else if(e.getActionCommand()=="join") 
     { 
      this.join(); 
     } 

    } 

    public void start() { 
     //Reponse to event immidiately 
     this.view.start(); 
     //Response and manipulate model 
     //Should start a new thread instead of using swing eventDispatch thread 
     this.model.start(); 
    } 
    public void stop() { 
     //Reponse to event immidiately 
     this.view.stop(); 
     //Response and manipulate model 
     this.model.stop(); 
    } 
    public void join() 
    { 
     System.out.println("Client tries to connect"); 
     Client cl=new Client(); 
     cl.join(); 
    } 
} 

클라이언트

package clientServer; 

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

public class Client { 

    private Socket socket; 

    public Client() 
    { 
     try 
     { 
     this.socket=new Socket("127.0.0.1",13131); 
     } 
     catch(UnknownHostException e) 
     { 
      System.err.println(e); 
     } 
     catch(IOException e) 
     { 
      System.err.println(e); 
     } 
    } 

    public void join() 
    { 
     System.out.println("Client join called"); 
     System.out.println("Client socket is connected:" + this.socket.isConnected()); 
     try 
     { 
     OutputStream op=this.socket.getOutputStream(); 

     BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(op)); 
     bfw.write("Client is connected \n"); 

     bfw.close(); 
     } 
     catch(IOException e) 
     { 
      System.err.println(e); 
     } 
    } 

} 
+1

오라클 자습서 Concurency in Swing, Swing GUI에 상관없는 이벤트 발송 스레드에 문제가있어 EDT에서 무언가를 변경하는 백그라운드 프로세스가 어떻게 든 알리지는 않습니다 – mKorbel

+0

@mkorbel 이 문제를 해결 하시겠습니까? – varuog

+0

@mKorbel이 맞습니다. 여기에 완전한 예제가 있습니다 (here) (http://stackoverflow.com/a/3245805/230513). – trashgod

답변

0

서버가 실행 중입니까? 그것은 serverOnline처럼 보이는

당신이 마지막으로 차단하려면 true로 설정되어

while(serverOnline) 

그것은 바로 실패에 계속에 도착 그렇게 할 때, false로 초기화합니다. 이 시점에서 서버를 다시 "시작"하는 경우 연결 대기가 시작되지만 UI에서 "중지"를 먼저 눌러야 만 serverOnline이 false로 설정됩니다. serverOnline을 true로 설정하는 Server.start() 시작 부분에 행을 추가하면 제대로 작동합니다.

두 실제로 서버가 실행지고 관련이없는 제안 : Server.close에서

1)() 당신은 당신의 소켓을 닫는. 나는 Server.start()의 finally 블록으로 옮겨 소켓이 닫히기 전에 연결을 끝낼 수 있도록했다.

2) ServerView의 생성자에 "server"라는 철자가있는 지점이있다. serever ". 죄송합니다. :-)

0

난 당신이

this.serverButton.setActionCommand("join"); 

을하지 않을 시작() 메소드에서 ServerView에서 발견하지만 당신은 시작이 작업을 수행하고 중지 할. 어쩌면 그 이유는 ServerController에서 이후에 조인이 제대로 작동하지 않는 이유와 관련이 있습니다.

else if(e.getActionCommand()=="join") 
    { 
     this.join(); 
    } 
+0

오, 나는 물어볼려고했는데 ... 다른 모든 것은 작동하지만 참여 버튼은 작동합니까? 귀하의 질문은 그것이 작동하지 않는 유일한 방법 인 것처럼 보입니다. – booleanCube

관련 문제