快速业务通道

三步学会Java Socket编程 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-07-04
= new FileOutputStream("d:/connect.log", true); public Server() throws FileNotFoundException, IOException { super(SERVER_PORT); new Broadcast(); //append connection log Calendar now = Calendar.getInstance(); String str = "[" + now.getTime().toString() + "] Accepted a connection1512"; byte[] tmp = str.getBytes(); LOG_FILE.write(tmp); try { while (true) { Socket socket = accept(); new CreateServerThread(socket); } } finally { close(); } } public static void main(String[] args) throws IOException { new Server(); } //--- Broadcast class Broadcast extends Thread { public Broadcast() { start(); } public void run() { while (true) { if (!isClear) { String tmp = (String)Message_Array.getFirst(); for (int i = 0; i < Threader.size(); i++) { CreateServerThread client = (CreateServerThread)Threader.get(i); client.sendMessage(tmp); } Message_Array.removeFirst(); isClear = Message_Array.size() > 0 ? false : true; } } } } //--- CreateServerThread class CreateServerThread extends Thread { private Socket client; private BufferedReader in; private PrintWriter out; private String Username; public CreateServerThread(Socket s) throws IOException { client = s; in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(), true); out.println("--- Welcome to this chatroom ---"); out.println("Input your nickname:"); start(); } public void sendMessage(String msg) { out.println(msg); } public void run() { try { int flag = 0; Thread_Counter++; String line = in.readLine(); while (!line.equals("bye")) { if (line.equals("l")) { out.println(listOnlineUsers()); line = in.readLine(); continue; } if (flag++ == 0) { Username = line; User_List.add(Username); out.println(listOnlineUsers()); Threader.add(this); pushMessage("[< " + Username + " come on in >]"); } else { pushMessage("<" + Username + ">" + line); } line = in.readLine(); } out.println("--- See you, bye! ---"); client.close(); } catch (IOException e) {} finally { try { client.close(); } catch (IOException e) {} Thread_Counter--; Threader.remove(this); User_List.remove(Username); pushMessage("[< " + Username + " left>]"); } } private String listOnlineUsers() { String s ="-+- Online list -+-1512"; for (int i = 0; i < User_List.size(); i++) { s += "[" + User_List.get(i) + "]1512"; } s += "-+---------------------+-"; return s; } private void pushMessage(String msg) { Message_Array.addLast(msg); isClear = false; } } }

三步学会Java Socket编程 - 编程入门网

这就是程序运行后,多用户登陆并且输入信息后的屏幕。实现了信息的实时广播。用户输入"l"就可以列出在线人员表。

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号