Socket编程 基本的聊天实现。

编程技术  /  houtizong 发布于 3年前   115
public class Server{//用来存储所有连接上来的客户private List<ServerThread> clients;public static void main(String[] args){Server s = new Server();s.startServer(9988);}public void startServer(int port){ServerSocket ss = null;Socket socket = null;try{ss = new ServerSocket(port);clients=new ArrayList<ServerThread>();while (true){System.out.println("waiting...");socket = ss.accept();ServerThread st = new ServerThread(socket);new Thread(st).start();}}catch (IOException e){e.printStackTrace();}finally{SocketUtils.closeSocketServer(ss);}}private class ServerThread implements Runnable{private Socket s = null;private BufferedReader br;private PrintWriter out;private String address;private String username;private boolean flag;public ServerThread(Socket s) throws IOException{this.s = s;br = new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));out = new PrintWriter(s.getOutputStream(),true);username=br.readLine();System.out.println("username="+username);address = s.getInetAddress().getHostAddress() + ":" + s.getPort();//System.out.println(name + "连接上来了");clients.add(this);send(username+"上线了");}private void stop(){System.out.println(username+"已经离开了");clients.remove(this);flag = false;}/** * 接收用户的信息 */private void receive(){String str = null;try{while ((str = br.readLine()) != null){if (str.equalsIgnoreCase("quit")){// 关闭stop();out.println("disconnect");break;}String sendMsg=username+":"+str;//out.println(username+":"+ str);//给所有的用户发送信息send(sendMsg);System.out.println("out");}}catch (IOException e){stop();System.out.println("用户非正常死亡->"+username);}finally{SocketUtils.closeSocket(s);}}/** * 给用户发信息 * @param msg */private void send(String msg){for(ServerThread st:clients){st.out.println(msg);}}public void run(){// if(!flag)break;receive();}}}




public class Client{private Socket s;private BufferedReader br;private PrintWriter out;private boolean flag=true;public static void main(String[] args){Client c=new Client(); //c.sendMessage();c.startup();}public void startup(){BufferedReader sbr=null;try{s=new Socket("127.0.0.1",9988);//s=new Socket("115.28.240.213",9988);out=new PrintWriter(s.getOutputStream(),true);out.println("myname");br=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));sbr=new BufferedReader(new InputStreamReader(System.in));new Thread(new ClientThread()).start();String str=null;while((flag)&&(str=sbr.readLine())!=null){if(!flag)break;String msg=new String(str.getBytes("gbk"),"utf-8");out.println(msg);}}catch (UnknownHostException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}} private void closeSocket(Socket s){if(s!=null){try{s.close();}catch (IOException e){e.printStackTrace();}}}private void receive(){try{String rs=br.readLine();if(rs.equalsIgnoreCase("disconnect")){flag=false;System.out.println("按回车退出");return ;}System.out.println(rs);}catch (IOException e){e.printStackTrace();}}private class ClientThread implements Runnable{@Overridepublic void run(){while(true){if(!flag)break;receive();}}} }

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客