채팅 프로그램 짜는데 왜 버퍼리더가 안되는지 모르겟어요 ㅠㅠ
Sweeety
질문 클라이언트에서 버퍼리더를 해서 서버에서 뿌려주는 문자를 받을려고 하는데 안되네요 ㅠㅠ
//서버 소스
package Chating;
import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.Vector;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneConstants;
public class ChatServer extends JFrame{ public JTextArea com_area; JTextField com_tf; JButton send; String Id; ServerSocket sc; Socket s; String data; String user_id; BufferedReader br; InputStreamReader isr; InputStream is; PrintWriter pw; Vector v; boolean flag=false; public ChatServer(){ this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); design(); try{ sc = new ServerSocket(7777); s = sc.accept(); br = new BufferedReader(new InputStreamReader(s.getInputStream())); pw = new PrintWriter(s.getOutputStream()); user_id=(String)br.readLine(); com_area.append([+user_id+]+님이 접속 하였습니다.\n); while((data=(String)br.readLine())!=null){ com_area.append(user_id+:+data+\n); com_area.setSelectionStart(com_area.getText().length()); pw.println(data); } }catch(Exception e){ e.printStackTrace(); } }
public void Broadcast(){ /*try{ } }catch(Exception e){ e.printStackTrace(); }*/ } public void design(){ Container ca=this.getContentPane(); com_tf = new JTextField(12); com_area = new JTextArea(18,22); com_area.setLineWrap(true); JScrollPane JS = new JScrollPane(com_area); JS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); JS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); com_area.setEditable(false); JPanel north= new JPanel(); north.add(JS); JPanel center= new JPanel(); center.add(com_tf); ca.add(north, BorderLayout.CENTER); ca.add(center, BorderLayout.SOUTH); setTitle(Server); setBounds(300, 300, 350, 450); setVisible(true); } public static void main(String[] args){ ChatServer cs = new ChatServer(); }}
//클라 소스
package Chating;
import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.ScrollPaneConstants;
public class ChatClient extends JFrame implements ActionListener, Runnable{ PrintWriter pw; OutputStream os; Socket s; String user_id; JTextField com_tf; JTextArea com_area; JButton send; String data; String str; BufferedReader br; public ChatClient(){ super(); }
public ChatClient(String name){ this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); Design(); user_id=name; try{ s = new Socket(127.0.0.1,7777); System.out.println(서버에 접속 하였습니다.); br = new BufferedReader(new InputStreamReader(s.getInputStream())); pw = new PrintWriter(s.getOutputStream(),true); pw.println(user_id); //대화명 전송 }catch(Exception e){ } try{ while((str=br.readLine())!=null){ com_area.append(str); } }catch(Exception e){ e.printStackTrace(); } Thread ct = new Thread(this); ct.start(); } public void run(){
} public void Design(){ Container ca=this.getContentPane(); send = new JButton(전송); com_tf = new JTextField(12); com_area = new JTextArea(18,22); com_area.setLineWrap(true); JScrollPane JS = new JScrollPane(com_area); com_area.setSelectionStart(com_area.getText().length()); JS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); JS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); com_area.setEditable(false); JPanel north= new JPanel(); north.add(JS); JPanel center= new JPanel(); center.add(com_tf); center.add(send); send.addActionListener(this); ca.add(north, BorderLayout.CENTER); ca.add(center, BorderLayout.SOUTH); setBounds(300,300,350,450); setVisible(true); setTitle(Client); } public synchronized void sendMsg(){ }
public void actionPerformed(ActionEvent e){ data=com_tf.getText(); pw.println(data); com_tf.setText(); com_tf.requestFocus(); } public static void main(String[] args){ ChatClient cc = new ChatClient();
}}흐어엉 ㅠㅠ 쓰레드도 구현해야하는뎅 ㅠㅠ 오늘 잠은 다잔거 같습니다 ㅠㅋㅋ
-
카제 2023-12-15
감사합니다// 오늘 집에가서 C숙제 다하고(이것도 밤샘 스멜이 스물스물 -0-;;) 내일 쉬는 날에 해보겠습니다!
-
봄 2023-12-15
기대는 하지 마시구요. 소스를 자세히 본게 아니라..
그냥 죽 내리면서 훑어 봤는데 뭐 문법적인 오류가 있는지는 잘 모르겠고
flush가 없는 것 같은데요.
PrintWriter는 기본 Buffer에 데이터를 저장했다가 보냅니다.
println 시점에?? 아니죠.
flush 시점에.
pw.println(user_id);
pw.flush(); //추가