上节回顾:在学习了Socket建立客户端和服务器双向交流后,我们发现它的问答形式不够灵活。
本节课我们讲解如何在服务器和客户端之间实现聊天室~
需要思考的问题:
除了消化这段代码外,还需要思考,怎么实现一个聊天室!(难度较大!!)
【示例1】聊天室服务器端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.socket;
public class ChatServer {
public static void main(String[] args) { ServerSocket server=null; Socket socket=null; BufferedReader in=null; try { server = new ServerSocket(8888); socket=server.accept(); new ServerThread(socket).start();
//main线程负责读取客户端发来的信息 in=new BufferedReader(new InputStreamReader(socket.getInputStream())); while(true){ String str=in.readLine(); System.out.println("客户端说:"+str); } } catch (IOException e) { e.printstacktrace(); }finally{ try { in.close(); } catch (IOException e) { e.printstacktrace(); } try { socket.close(); } catch (IOException e) { e.printstacktrace(); }
}
} }
/** * 专门向客户端发送消息的线程 * @author Administrator * */ class ServerThread extends Thread { Socket ss; BufferedWriter out; BufferedReader br; public ServerThread(Socket ss){ this.ss = ss; try { out=new BufferedWriter(new OutputStreamWriter(ss.getoutputStream())); br = new BufferedReader(new InputStreamReader(System.in)); } catch (IOException e) { e.printstacktrace(); } } public void run(){ try { while(true){ String str2 = br.readLine(); out.write(str2+"\n"); out.flush(); } } catch (IOException e) { e.printstacktrace(); }finally{ try { out.close(); } catch (IOException e) { e.printstacktrace(); } try { br.close(); } catch (IOException e) { e.printstacktrace(); } }
} }
|
【示例2】聊天室客户端
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.socket; import java.net.UnkNownHostException;
public class ChatClient { public static void main(String[] args) { Socket socket = null; BufferedReader in = null; try { socket=new Socket(InetAddress.getByName("192.168.1.32"),8888); new ClientThread(socket).start(); in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//main线程负责接收服务器发来的信息 while(true){ System.out.println("服务器说:"+in.readLine()); }
} catch (UnkNownHostException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); }finally{ try { socket.close(); } catch (IOException e) { e.printstacktrace(); }
try { in.close(); } catch (IOException e) { e.printstacktrace(); } }
} }
/** * 用于向服务器发送消息 * @author Administrator * */ class ClientThread extends Thread{ Socket s; BufferedWriter out; BufferedReader wt; public ClientThread(Socket s){ this.s = s; try { out=new BufferedWriter(new OutputStreamWriter(s.getoutputStream())); wt=new BufferedReader(new InputStreamReader(System.in)); } catch (IOException e) { e.printstacktrace(); }
} public void run(){ try { while(true){ String str = wt.readLine(); out.write(str+"\n" ); out.flush(); } } catch (IOException e) { e.printstacktrace(); }finally{ try { wt.close(); } catch (IOException e) { e.printstacktrace(); } try { out.close(); } catch (IOException e) { e.printstacktrace(); } } } } |
「全栈Java笔记」是一部能帮大家从零到一成长为全栈Java工程师系列笔记。笔者江湖人称 Mr. G,10年Java研发经验,曾在神州数码、航天院某所研发中心从事软件设计及研发工作,从小白逐渐做到工程师、高级工程师、架构师。精通Java平台软件开发,精通JAVAEE,熟悉各种流行开发框架。
笔记包含从浅入深的六大部分:
A-Java入门阶段
C-手刃移动前端和Web前端
D-J2EE从了解到实战
E-Java高级框架精解
F-Linux和Hadoop
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。