자바 코드 클라이언트 질문
조회수 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(); } } }
}
여기서 실행한 뒤 서버 구동하기를 클릭하면 그대로 멈춥니다. 멈추는 이유와 고칠 부분을 알고 싶습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력