一直想弄个聊天系统,我没有耍下(www.shuax.com)那么牛叉。
只能这么简简单单的玩玩这个,况且我觉得这个已经很难了。
分为客户端和服务端。参考了一下耍下的代码。毕竟线程和套接字是第一次接触。
客户端代码:
View Code JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | package chatting; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class 客户端 implements ActionListener { JTextArea jta1,jta2; JButton jb1,jb2,jb3; JTextField jtf1,jtf2; ServerSocket server=null; Socket s=null; DataInputStream in =null; DataOutputStream out = null; public 客户端(){ JFrame jf = new JFrame("chatting-客户端"); jf.setLayout(new BorderLayout()); JPanel jp1 = new JPanel(); JLabel jl1 = new JLabel(" 用户名:"); jtf1 = new JTextField(10); jb1 = new JButton("连接服务器"); jb1.addActionListener(this); JLabel jl2 = new JLabel("服务器IP:"); jtf2 = new JTextField(10); jtf2.setText("localhost"); jp1.add(jl2); jp1.add(jtf2); jp1.add(jl1); jp1.add(jtf1); jp1.add(jb1); jf.add(jp1,BorderLayout.NORTH); jta1 = new JTextArea(20,51); jta1.setEditable(false); JScrollPane jsp1 = new JScrollPane(jta1); JPanel jp2 = new JPanel(); jp2.add(jsp1); jf.add(jp2,BorderLayout.CENTER); jta2 = new JTextArea(3,39); jta2.setEditable(false); jta2.setText("请先连接服务器!\n 如果不填写服务器IP,默认为本机localhost"); JScrollPane jsp2 = new JScrollPane(jta2); JPanel jp3 = new JPanel(); jp3.add(jsp2); jb2 = new JButton("发送"); jb2.addActionListener(this); jb2.setEnabled(false); jb3 = new JButton("重写"); jb3.addActionListener(this); jb3.setEnabled(false); jp3.add(jb2);jp3.add(jb3); jf.add(jp3,BorderLayout.SOUTH); jp1.setBackground(Color.LIGHT_GRAY);//设置背景色 jp2.setBackground(Color.LIGHT_GRAY); jp3.setBackground(Color.LIGHT_GRAY); jf.setResizable(true); jf.setVisible(true); jf.setSize(600, 500); jf.setLocationRelativeTo(null); //屏幕居中显示 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ new 客户端(); } @Override public void actionPerformed(ActionEvent e) { String comm = e.getActionCommand(); if(comm.equals("连接服务器")){ if(jtf1.getText().isEmpty()){ jta2.setText("用户名不能为空,请重新填写!"); return; }else{ try { s = new Socket(jtf2.getText(),8888); in = new DataInputStream(s.getInputStream()); out = new DataOutputStream(s.getOutputStream()); out.writeUTF(jtf1.getText()+"已连接到此服务器"+"\n"); receive().start(); } catch (IOException e1) { jta2.setText("网络连接失败!请确保你已经创建服务器!或者确保你填写的服务器IP正确!"); return; } } jtf1.setEditable(false); jb1.setEnabled(false); jb2.setEnabled(true); jb3.setEnabled(true); jta2.setEditable(true); jta2.setText(null); jb2.setMnemonic(KeyEvent.VK_ENTER);//ENTER作为快捷键,回车就可以发布 }else if(comm.equals("发送")){ if(jtf1.getText().isEmpty()) return; try { String m = jtf1.getText()+": "+jta2.getText()+"\n"; jta1.append(m); out.writeUTF(m); jta2.setText(null); }catch (IOException e2){ jta2.setText("ERROR!网络发生异常,连接失败!"); return; } jta2.setText(null); }else if(comm.equals("重写")){ jta2.setText(null); } } private Thread receive() { return new Thread() { public void run(){ try { while(true){ jta1.append(in.readUTF()); Thread.sleep(50); } }catch (Exception e) {} } }; } } |
服务端代码:
View Code JAVA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | package chatting; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class 服务端 implements ActionListener { JTextArea jta1,jta2; JButton jb1,jb2,jb3; JTextField jtf1,jtf2; ServerSocket server=null; Socket s=null; DataInputStream in =null; DataOutputStream out = null; public 服务端(){ JFrame jf = new JFrame("chatting-服务端"); jf.setLayout(new BorderLayout()); JPanel jp1 = new JPanel(); JLabel jl1 = new JLabel(" 用户名:"); jtf1 = new JTextField(10); jb1 = new JButton("开启服务器"); jb1.addActionListener(this); JLabel jl2 = new JLabel("本机IP:"); jtf2 = new JTextField(10); jtf2.setEditable(false); InetAddress addr1; try { addr1 = InetAddress.getLocalHost(); jtf2.setText(addr1.getHostAddress()); } catch (UnknownHostException e) { jta2.setText("本机IP错误!"); e.printStackTrace(); } jp1.add(jl2); jp1.add(jtf2); jp1.add(jl1); jp1.add(jtf1); jp1.add(jb1); jf.add(jp1,BorderLayout.NORTH); jta1 = new JTextArea(20,51); jta1.setEditable(false); JScrollPane jsp1 = new JScrollPane(jta1); JPanel jp2 = new JPanel(); jp2.add(jsp1); jf.add(jp2,BorderLayout.CENTER); jta2 = new JTextArea(3,39); jta2.setEditable(false); jta2.setText("请先开启服务器!"); JScrollPane jsp2 = new JScrollPane(jta2); JPanel jp3 = new JPanel(); jp3.add(jsp2); jb2 = new JButton("发送"); jb2.addActionListener(this); jb2.setEnabled(false); jb3 = new JButton("重写"); jb3.addActionListener(this); jb3.setEnabled(false); jp3.add(jb2);jp3.add(jb3); jf.add(jp3,BorderLayout.SOUTH); jp1.setBackground(Color.LIGHT_GRAY);//设置背景色 jp2.setBackground(Color.LIGHT_GRAY); jp3.setBackground(Color.LIGHT_GRAY); jf.setResizable(true); jf.setVisible(true); jf.setSize(600, 500); jf.setLocationRelativeTo(null); //屏幕居中显示 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ new 服务端(); } @Override public void actionPerformed(ActionEvent e) { String comm = e.getActionCommand(); if(comm.equals("开启服务器")){ if(jtf1.getText().isEmpty()){ jta2.setText("用户名不能为空,请重新填写!"); return; }else{ try { server = new ServerSocket(8888); //服务器端口号 s = server.accept(); in = new DataInputStream(s.getInputStream()); out = new DataOutputStream(s.getOutputStream()); out.writeUTF("已连接到服务器"+jtf1.getText()+"\n"); receive().start(); } catch (IOException e1) { jta2.setText("网络连接失败!"); return; } } jtf1.setEditable(false); jb1.setEnabled(false); jb2.setEnabled(true); jb3.setEnabled(true); jta2.setEditable(true); jta2.setText(null); jb2.setMnemonic(KeyEvent.VK_ENTER);//ENTER作为快捷键,回车就可以发布 }else if(comm.equals("发送")){ if(jtf1.getText().isEmpty()) return; try { String m = jtf1.getText()+": "+jta2.getText()+"\n"; jta1.append(m); out.writeUTF(m); jta2.setText(null); }catch (IOException e2){ jta2.setText("ERROR!网络发生异常,连接失败!"); return; } jta2.setText(null); }else if(comm.equals("重写")){ jta2.setText(null); } } private Thread receive() { return new Thread() { public void run(){ try { while(true){ jta1.append(in.readUTF()); Thread.sleep(50); } }catch (Exception e) {} } }; } } |
耍下弄出个Java版的QQ了,你也不用跟他比嘛~
只能“一对一”的聊就有点郁闷,不过期待你可以在此之上修改出更好的版本~
呦,编写个qq出来
能做到这样已经很不错了。当初我是用VB写的~~当然VB写的就可以一对多了~
我也许不能再看你的帖子了,这个回帖可能是我在这个BLOG回的最后的帖子,因为我可能会不能再上网了,但是我今天能回你的这个帖子,我已经感到非常满足了,不枉此生啊!
这个聊天系统貌似还挺简单的
Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.
现在不会java,等学会了可以帮你一起更新
耍下是牛人。。。不和他比。
呦,编写个qq出来