上节回顾:在学习了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入门阶段
B-数据库从入门到精通
C-手刃移动前端和Web前端
D-J2EE从了解到实战
E-Java高级框架精解
F-Linux和Hadoop