[vc_row][vc_column][vc_column_text]This project shows how to use Java to create an IM chat application on pcDuino.[/vc_column_text][vc_tour][vc_tab title=”Test Result” tab_id=”1395197300-1-57″][vc_column_text]Run the server, modify the server IP address on the client side code.
[/vc_column_text][/vc_tab][vc_tab title=”Test Code” tab_id=”1395197300-2-11″][vc_column_text]Service side code
import java.net.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class ChatServer extends Frame { TextArea ta = new TextArea(); public void launchFrame() { add(ta, BorderLayout.CENTER); setBounds(0,0,200,300); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); setVisible(true); } ServerSocket server = null; Collection cClient = new ArrayList(); public ChatServer(int port) throws Exception { server = new ServerSocket(port); launchFrame(); } public void startServer() throws Exception { while(true) { Socket s = server.accept(); cClient.add( new ClientConn(s) ); ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort()); ta.append("n" + "CLIENTS-COUNT: " + cClient.size() + "nn"); } } class ClientConn implements Runnable { Socket s = null; public ClientConn(Socket s) { this.s = s; (new Thread(this)).start(); } public void send(String str) throws IOException { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(str); } public void dispose() { try { if (s != null) s.close(); cClient.remove(this); ta.append("A client out! n"); ta.append("CLIENT-COUNT: " + cClient.size() + "nn"); } catch (Exception e) { e.printStackTrace(); } } public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); String str = dis.readUTF(); while(str != null && str.length() !=0) { System.out.println(str); for(Iterator it = cClient.iterator(); it.hasNext(); ) { ClientConn cc = (ClientConn)it.next(); if(this != cc) { cc.send(str); } } str = dis.readUTF(); //send(str); } this.dispose(); } catch (Exception e) { System.out.println("client quit"); this.dispose(); } } } public static void main(String[] args) throws Exception { ChatServer cs = new ChatServer(8888); cs.startServer(); } } Client side code
import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class ChatClient extends Frame { /** * */ private String ip="192.168.137.1"; private static final long serialVersionUID = 1L; TextArea ta = new TextArea(); TextField tf = new TextField(); public void launchFrame() throws Exception { this.add(ta, BorderLayout.CENTER); this.add(tf, BorderLayout.SOUTH); tf.setText("Client side:"); tf.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { try { String sSend = tf.getText(); if(sSend.trim().length() == 0) return; ChatClient.this.send(sSend); tf.setText("Client side:"); ta.append(sSend + "n"); } catch (Exception e) { e.printStackTrace(); } } } ); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); setBounds(300,300,300,400); setVisible(true); tf.requestFocus(); } Socket s = null; public ChatClient() throws Exception { s = new Socket(ip, 8888); launchFrame(); (new Thread(new ReceiveThread())).start(); } public void send(String str) throws Exception { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(str); } public void disconnect() throws Exception { s.close(); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader ( new InputStreamReader(System.in)); ChatClient cc = new ChatClient(); String str = br.readLine(); while(str != null && str.length() != 0) { cc.send(str); str = br.readLine(); } cc.disconnect(); } class ReceiveThread implements Runnable { public void run() { if(s == null) return; try { DataInputStream dis = new DataInputStream(s.getInputStream()); String str = dis.readUTF(); while (str != null && str.length() != 0) { //System.out.println(str); ChatClient.this.ta.append(str + "n"); str = dis.readUTF(); } } catch (Exception e) { e.printStackTrace(); } } } }
[/vc_column_text][/vc_tab][/vc_tour][vc_column_text]
[/vc_column_text][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.