31.9 (Chat) Write a program that enables two users to chat. Implement one user
as the server (Figure 31.21a) and the other as the client (Figure 31.21b). The
server has two text areas: one for entering text and the other (noneditable) for
displaying text received from the client. When the user presses the Enter key,
the current line is sent to the client. The client has two text areas: one (nonedit-
able) for displaying text from the server and the other for entering text. When
the user presses the Enter key, the current line is sent to the server. Name the
client Exercise31_09Client and the server Exercise31_09Server.
as the server (Figure 31.21a) and the other as the client (Figure 31.21b). The
server has two text areas: one for entering text and the other (noneditable) for
displaying text received from the client. When the user presses the Enter key,
the current line is sent to the client. The client has two text areas: one (nonedit-
able) for displaying text from the server and the other for entering text. When
the user presses the Enter key, the current line is sent to the server. Name the
client Exercise31_09Client and the server Exercise31_09Server.
import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.TitledBorder; public class Exercise09s extends JFrame { private static final long serialVersionUID = 1L; private JTextArea jta1 = new JTextArea(); private JTextArea jta2 = new JTextArea(); private PrintWriter toClient; public Exercise09s() { setLayout(new GridLayout(2, 1, 5, 5)); jta1.setWrapStyleWord(true); jta1.setWrapStyleWord(true); jta2.setLineWrap(true); jta2.setLineWrap(true); jta2.setEditable(false); JScrollPane jsp1 = new JScrollPane(jta1); JScrollPane jsp2 = new JScrollPane(jta2); jsp1.setBorder(new TitledBorder("Client")); jsp2.setBorder(new TitledBorder("Server")); add(jsp2); add(jsp1); setTitle("Exercise09s"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 500); setLocationRelativeTo(null); setVisible(true); jta1.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { try { toClient.print(jta1.getText().split("\n")[jta1.getLineCount() - 1] + "\n"); toClient.flush(); } catch (ArrayIndexOutOfBoundsException e2) { } } } }); try { @SuppressWarnings("resource") ServerSocket serverSocket = new ServerSocket(8000); Socket socket = serverSocket.accept(); toClient = new PrintWriter(socket.getOutputStream()); new ReceiveMessage(socket); } catch (IOException e) { e.printStackTrace(); } } class ReceiveMessage implements Runnable { private Socket socket; public ReceiveMessage(Socket socket) { this.socket = socket; Thread thread = new Thread(this); thread.start(); } public void run() { try { @SuppressWarnings("resource") Scanner fromClient = new Scanner(socket.getInputStream()); while(true) { String text = fromClient.nextLine(); jta2.append(text + "\n"); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new Exercise09s(); } }
import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.TitledBorder; public class Exercise09c extends JFrame { private static final long serialVersionUID = 1L; private JTextArea jta1 = new JTextArea(); private JTextArea jta2 = new JTextArea(); private PrintWriter toServer; public Exercise09c() { setLayout(new GridLayout(2, 1, 5, 5)); jta1.setWrapStyleWord(true); jta1.setWrapStyleWord(true); jta2.setLineWrap(true); jta2.setLineWrap(true); jta1.setEditable(false); JScrollPane jsp1 = new JScrollPane(jta1); JScrollPane jsp2 = new JScrollPane(jta2); jsp1.setBorder(new TitledBorder("Client")); jsp2.setBorder(new TitledBorder("Server")); add(jsp1); add(jsp2); setTitle("Exercise09c"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 500); setLocationRelativeTo(null); setVisible(true); jta2.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { try { toServer.print(jta2.getText().split("\n")[jta2.getLineCount() - 1] + "\n"); toServer.flush(); } catch (ArrayIndexOutOfBoundsException e2) { } } } }); try { Socket socket = new Socket("localhost", 8000); toServer = new PrintWriter(socket.getOutputStream()); new ReceiveMessage(socket); } catch (IOException e) { e.printStackTrace(); } } class ReceiveMessage implements Runnable { private Socket socket; public ReceiveMessage(Socket socket) { this.socket = socket; Thread thread = new Thread(this); thread.start(); } public void run() { try { @SuppressWarnings("resource") Scanner fromServer = new Scanner(socket.getInputStream()); while(true) { String text = fromServer.nextLine(); jta1.append(text + "\n"); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new Exercise09c(); } }
No comments:
Post a Comment