자바 코드 클라이언트 질문

조회수 1476회

import java.io.; import java.net.; import java.util.; import javax.swing.; import java.awt.; import java.awt.event.;

public class SimpleChatClient {

JTextArea incoming; JTextField outgoing; JTextField ipEdit; String ipAdress;

BufferedReader reader; PrintWriter writer; Socket sock;

public static void main(String[] args) { // TODO Auto-generated method stub SimpleChatClient client = new SimpleChatClient(); client.go();

} public void go() { JFrame frame = new JFrame("간단한 챗팅 프로그램"); JPanel mainPanel = new JPanel();

incoming = new JTextArea(15,50); incoming.setLineWrap(true); incoming.setWrapStyleWord(true); incoming.setEditable(false); JScrollPane qScroller = new JScrollPane(incoming); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); outgoing = new JTextField(20); JButton sendButton = new JButton("보내기"); sendButton.addActionListener(new SendButtonListener()); mainPanel.add(qScroller); mainPanel.add(outgoing); mainPanel.add(sendButton);

ipEdit =new JTextField(20); mainPanel.add(ipEdit); JButton ipButton = new JButton("서버가동"); ipButton.addActionListener(new SendButtonListenerforIP()); mainPanel.add(ipButton); frame.getContentPane().add(BorderLayout.CENTER, mainPanel); frame.setSize(400, 500); frame.setVisible(true); }

private void setUpNetworking() { try{ ipAdress = ipEdit.getText(); System.out.println("ip주소는 "+ipAdress); sock = new Socket("192.168.0.58",999); InputStreamReader streamReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(streamReader); writer = new PrintWriter(sock.getOutputStream()); System.out.println("네트워크가 가동됨"); } catch(IOException ex) { ex.printStackTrace(); }

Thread readerThread = new Thread(new IncomingReader()); readerThread.start(); }

public class SendButtonListener implements ActionListener { public void actionPerformed(ActionEvent ev) {

try{

writer.println(outgoing.getText()); writer.flush(); } catch(Exception ex) { ex.printStackTrace();

}

outgoing.setText(""); outgoing.requestFocus(); } }

public class SendButtonListenerforIP implements ActionListener { public void actionPerformed(ActionEvent ev) {

setUpNetworking();

} }

public class IncomingReader implements Runnable { public void run() { String message; try { while((message = reader.readLine()) != null) { System.out.println("read " + message); incoming.append(message + "\n");

} }catch (Exception ex){ ex.printStackTrace(); } } }

}

여기서 실행한 뒤 서버 구동하기를 클릭하면 그대로 멈춥니다. 멈추는 이유와 고칠 부분을 알고 싶습니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 클라이언트 프로그램인데요? 서버 구동하기 이벤트는 어디에 있나요? 정영훈 2017.9.2 06:43

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)