{"id":378,"date":"2012-06-11T14:59:00","date_gmt":"2012-06-11T06:59:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=378"},"modified":"2020-06-06T15:01:00","modified_gmt":"2020-06-06T07:01:00","slug":"java-%e5%9c%a8%e7%ba%bf%e5%a4%9a%e5%ae%a2%e6%88%b7%e7%ab%af%e8%81%8a%e5%a4%a9-j2se%e5%b0%8f%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=378","title":{"rendered":"Java \u5728\u7ebf\u591a\u5ba2\u6237\u7aef\u804a\u5929 J2SE\u5c0f\u7ec3\u4e60"},"content":{"rendered":"\n<p>server\u7aef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.DataInputStream;\nimport java.io.DataOutputStream;\nimport java.io.IOException;\nimport java.net.ServerSocket;\nimport java.net.Socket;\nimport java.net.SocketException;\nimport java.util.ArrayList;\n \npublic class ChatSever {\n \n\tArrayList&lt;Client> clients = new ArrayList&lt;Client>();\n \n\tpublic static void main(String&#91;] args){\n\t\tnew ChatSever().start();\n\t}\n \n\tvoid start(){\n\t\tServerSocket ss = null;\n\t\ttry {\n\t\t\tss = new ServerSocket(8081);\n\t\t} catch (IOException e) {\n\t\t\tSystem.out.println(\"\u7aef\u53e3\u5df2\u7ecf\u88ab\u5360\u7528...\");\n\t\t\treturn ;\n\t\t}\n\t\twhile (true) {\n\t\t\tSocket s = null;\n\t\t\ttry {\n\t\t\t\ts = ss.accept();\n\t\t\t} catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t\tClient c = new Client(s);\n\t\t\tclients.add(c);\n\t\t\tnew Thread(c).start();\n\t\t}\n\t}\n \n\tclass Client implements Runnable {\n\t\tprivate Socket s = null;\n\t\tDataInputStream dis = null;\n\t\tDataOutputStream dos = null;\n\t\tClient c=null;\n \n\t\tClient(Socket s){\n\t\t\tthis.s = s;\n\t\t\ttry {\n\t\t\t\tdis = new DataInputStream(s.getInputStream());\n\t\t\t} catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tdos = new DataOutputStream(s.getOutputStream());\n\t\t\t} catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n \n\t\tpublic void sentAll(String str){\n\t\t\ttry {\n\t\t\t\tdos.writeUTF(str);\n\t\t\t} catch (SocketException e) {\n\t\t\t\tif(c!=null)\n\t\t\t\t\tclients.remove(c);\n\t\t\t} catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t} \n\t\t}\n\t\t\n\t\tpublic void run() {\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tString receive = dis.readUTF();\n\t\t\t\t\tSystem.out.println(receive);\n\t\t\t\t\t\/\/ \u8f6c\u53d1\n\t\t\t\t\tfor(int i=0;i&lt;clients.size();i++){\n\t\t\t\t\t\tc=clients.get(i);\n\t\t\t\t\t\tc.sentAll(receive);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (SocketException e) {\n\t\t\t}catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t}\n \n}<\/code><\/pre>\n\n\n\n<p>client\u7aef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.awt.BorderLayout;\nimport java.awt.Frame;\nimport java.awt.Panel;\nimport java.awt.TextArea;\nimport java.awt.TextField;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.awt.event.WindowAdapter;\nimport java.awt.event.WindowEvent;\nimport java.io.DataInputStream;\nimport java.io.DataOutputStream;\nimport java.io.IOException;\nimport java.net.ConnectException;\nimport java.net.Socket;\nimport java.net.UnknownHostException;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\n \npublic class ChatClient extends Frame {\n \n\tTextField txf = new TextField();\n\tTextArea txa = new TextArea();\n\tSocket s = null;\n\tDataOutputStream dos = null;\n\tDataInputStream dis = null;\n\tfinal String name = \"user\" + (int) (Math.random() * 100);\n \n\tpublic static void main(String&#91;] args) {\n\t\tChatClient cc = new ChatClient();\n\t\tcc.launchFrame();\n\t\tnew Thread(cc.new RecvThread()).start();\n\t}\n \n\tpublic void launchFrame() {\n\t\tthis.setTitle(\"Chat\");\n\t\tthis.setLocation(400, 300);\n\t\tthis.setSize(300, 400);\n\t\ttxa.setText(\"Let's Talk !\");\n\t\ttxf.addActionListener(new ActionListener() {\n \n\t\t\tpublic void actionPerformed(ActionEvent arg0) {\n\t\t\t\tsendToServer(txf.getText());\n\t\t\t\ttxf.setText(\"\");\n\t\t\t}\n \n\t\t});\n \n\t\tthis.add(txa, BorderLayout.NORTH);\n\t\tthis.add(txf, BorderLayout.SOUTH);\n\t\tthis.pack();\n \n\t\tthis.addWindowListener(new WindowAdapter() {\n \n\t\t\tpublic void windowClosing(WindowEvent arg0) {\n\t\t\t\tsendToServer(\"\u6211\u4e0b\u7ebf\u4e86\uff01\");\n\t\t\t\tsetVisible(false);\n\t\t\t\tSystem.exit(0);\n\t\t\t}\n \n\t\t});\n\t\tthis.setVisible(true);\n\t\tthis.connect();\n\t\tsendToServer(\"\u6211\u4e0a\u7ebf\u5566\uff01\");\n\t}\n \n\tpublic void connect() {\n\t\ttry {\n\t\t\ts = new Socket(\"127.0.0.1\", 8081);\n\t\t} catch (ConnectException e) {\n\t\t\ttxa.setText(txa.getText() + \"\\n\" + \"\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25...\");\n\t\t\treturn;\n\t\t\t\/\/ e.printStackTrace();\n\t\t} catch (UnknownHostException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t}\n \n\t\ttry {\n\t\t\tdos = new DataOutputStream(s.getOutputStream());\n\t\t} catch (IOException e1) {\n\t\t\te1.printStackTrace();\n\t\t} catch (NullPointerException e1) {\n\t\t\t\/\/ e1.printStackTrace();\n\t\t}\n \n\t\ttry {\n\t\t\tdis = new DataInputStream(s.getInputStream());\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t}catch (NullPointerException e) {\n\t\t\t\/\/e.printStackTrace();\n\t\t}\n\t}\n\t\n\tprivate void sendToServer(String str){\n\t\tSimpleDateFormat f = new SimpleDateFormat(\"HH:mm:ss\");\n\t\tString send = f.format(new Date()) + \"\\n\" + name + \" : \"\n\t\t\t\t+ str;\n\t\ttry {\n\t\t\tdos.writeUTF(send);\n\t\t\tdos.flush();\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (NullPointerException e) {\n\t\t\ttxa.setText(txa.getText() + \"\\n\" + send + \"  \u53d1\u9001\u5931\u8d25...\");\n\t\t\ttxf.setText(\"\");\n\t\t\t\/\/e.printStackTrace();\n\t\t}\n\t}\n \n \n\tclass RecvThread implements Runnable {\n\t\tpublic void run() {\n\t\t\ttry {\n\t\t\t\twhile (true) {\n\t\t\t\t\tString recv = dis.readUTF();\n\t\t\t\t\ttxa.setText(txa.getText() + \"\\n\" + recv);\n\t\t\t\t}\n\t\t\t} catch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t} catch (NullPointerException e) {\n\t\t\t\t\/\/e.printStackTrace();\n\t\t\t}\n\t\t}\n\t}\n\t\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>server\u7aef client\u7aef<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-378","post","type-post","status-publish","format-standard","hentry","category-06-03-play-ground"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=378"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}