java网络编程二.docx
- 文档编号:24137710
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:60
- 大小:33.08KB
java网络编程二.docx
《java网络编程二.docx》由会员分享,可在线阅读,更多相关《java网络编程二.docx(60页珍藏版)》请在冰豆网上搜索。
java网络编程二
回顾:
线程的高级应用:
同步代码块(同步方法)如果过多的同步就会造成死锁生产者和消费者(Object:
wait()notify()notyfyAll())
今天:
网络编程
基于Socket编程:
客户端只能和服务器端进行信息的交互,日常所见,客户端和客户端可以进行信息的交互?
?
?
所有客户端把信息发给服务器,服务器解析并回发.
1.一个服务器端连接多个客户端
publicclassServer{
//连接多个客户端
publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{
ServerSocketserver=newServerSocket(8888);
Socketclient=null;
//不停的监听,连接多个客户端
while(true){
Thread.sleep(1000);
client=server.accept();
//侦听一客户端socket
System.out.println(client+"上线了!
");
//启动线程
newThread(newServerReader(client)).start();
}
}
}
classServerReaderimplementsRunnable{
Sockets;
publicServerReader(Sockets){
this.s=s;
}
@Override
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader(newInputStreamReader(
s.getInputStream()));
PrintWriterpw=newPrintWriter(s.getOutputStream());
while(true){
//从客户读
Stringinfo=br.readLine();
System.out.println("客户端:
"+s+"发送的信息:
"+info);
//回写
pw.println("你好,客户端");
pw.flush();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
publicclassClient{
privateSockets;
publicClient()throwsUnknownHostException,IOException{
s=newSocket("127.0.0.1",8888);
System.out.println(s+"客户端创建了!
");//
//先写
Scannersc=newScanner(System.in);
PrintWriterpw=newPrintWriter(s.getOutputStream(),true);
//读...
newClientReader(s).start();
while(true){
System.out.println("请输入:
");
pw.println(sc.nextLine());
}
}
publicstaticvoidmain(String[]args)throwsUnknownHostException,IOException{
newClient();
}
}
classClientReaderextendsThread{
privateSockets;
publicClientReader(Sockets){
this.s=s;
}
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader
(newInputStreamReader(s.getInputStream()));
while(true){
Stringinfo=br.readLine();
System.out.println("服务器发来的消息:
"+info);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
2.如果一个只被实例化一次,可将此类定义成内部类
publicclassServer{
ServerSocketserver;
Sockets;
publicServer()throwsException{
server=newServerSocket(8888);
while(true){
s=server.accept();
System.out.println(s+"上线了!
");
newServerReader().start();
}
}
publicstaticvoidmain(String[]args)throwsException{
newServer();
}
//内部类:
可以直接调用外部类的属性
classServerReaderextendsThread{
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader(
newInputStreamReader(s.getInputStream()));
PrintWriterpw=newPrintWriter(s.getOutputStream(),true);
while(true){
Stringinfo=br.readLine();
System.out.println("客户端"+s+"发送的信息:
"+info);
//回发:
pw.println("你好"+s+"客户端");
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
publicclassClient{
Sockets;
publicClient()throwsException{
s=newSocket("127.0.0.1",8888);
System.out.println(s+"创建了!
");
PrintWriterpw=newPrintWriter(s.getOutputStream(),true);
Scannersc=newScanner(System.in);
newClientReader().start();
while(true){
pw.println(sc.nextLine());
}
}
publicstaticvoidmain(String[]args)throwsException{
newClient();
}
privateclassClientReaderextendsThread{
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader
(newInputStreamReader(s.getInputStream()));
while(true){
Stringinfo=br.readLine();
System.out.println("服务器发来的信息:
"+info);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
3.连接多个客户端,如果想实现信息交互,怎么存储多个客户端,
publicclassServer{
//ListSet:
存储一个单值
ServerSocketserver;
Sockets;
Set
publicServer()throwsException{
server=newServerSocket(8888);
set=newHashSet
while(true){
Thread.sleep(2000);
s=server.accept();
System.out.println(s+"已启动");
set.add(s);
newServerReader(s).start();
}
}
publicstaticvoidmain(String[]args)throwsException{
newServer();
}
classServerReaderextendsThread{
privateSockets;
publicServerReader(Sockets){
this.s=s;
}
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader(
newInputStreamReader(s.getInputStream()));
while(true){
Stringinfo=br.readLine();
System.out.println(s+"发来的信息:
"+info);
//发送给每一个客户端
sendMessage(info);
}
}catch(IOExceptione){
e.printStackTrace();
}
}
//发关信息到每一个客户端
publicvoidsendMessage(Stringinfo){
for(Socketsl:
set){
try{
PrintWriterpw1=newPrintWriter(sl.getOutputStream(),true);
pw1.println(sl+"发来的信息:
"+info);
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
publicclassClient{
Sockets;
publicClient()throwsException{
s=newSocket("192.168.3.233",8888);
System.out.println(s+"创建了");
System.out.println("请输入信息:
");
Scannersc=newScanner(System.in);
PrintWriterpw=newPrintWriter(s.getOutputStream(),true);
newClientReader().start();
while(true){
pw.println(sc.nextLine());
}
}
publicstaticvoidmain(String[]args)throwsException{
newClient();
}
classClientReaderextendsThread{
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader
(newInputStreamReader(s.getInputStream()));
while(true){
System.out.println(br.readLine());
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
3.聊天室
业务:
登录:
用户名+socket服务器读到客户端的用户名和Socket,存储
如果用户名已存在,提示回写给客户端
客户端:
将用户名和Socket发送给服务器,让服务器解析
显示好友列表
群聊
私聊
网络:
发送一个头文件+databody+foot
头:
%%Login%%:
message[Login]:
%%Chat%%message
头文件找信息主体:
头文件一定要唯一
(1)用户名+socket服务器读到客户端的用户名和Socket,存储
如果用户名已存在,提示回写给客户端
publicclassChatRoomServer{
privateServerSocketserver;
privateMap
publicChatRoomServer()throwsException{//完成对象的初始化
server=newServerSocket(8888);
onlineUsers=newHashMap
}
publicvoidstartServices()throwsException{//完成一个接收客户端的动作
Sockets=null;
while(true){
s=server.accept();
newChatRoomServerThread(s).start();
}
}
publicstaticvoidmain(String[]args)throwsException{
newChatRoomServer().startServices();
}
//读线程:
内部类
privateclassChatRoomServerThreadextendsThread{
privateSockets;
publicChatRoomServerThread(Sockets){
this.s=s;
}
publicvoidrun(){
try{
BufferedReaderbr=newBufferedReader(
newInputStreamReader(s.getInputStream()));
PrintWriterpw=newPrintWriter(s.getOutputStream(),true);
while(true){
Stringcomm=null;
comm=br.readLine();
System.out.println(comm);
if(comm.split(":
")[0].equals("%LOGIN%")){
Stringusername=comm.split(":
")[1];
if(onlineUsers.containsKey(username)){
//REFUSE:
用户名已存在
pw.println("%LOGIN%:
REFUSE");
}
else{//OK:
登录成功
pw.println("%LOGIN%:
OK");
onlineUsers.put(username,s);
//群聊和私聊
}
}
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
publicclassChatRoomClient{
privateSockets;
privatePrintWriterpw;
privateBufferedReaderbr;
publicChatRoomClient()throwsException{
//连接服务器
createSocket();
//登录:
name+socket
login();
//进入聊天室
}
//连接服务器方法
publicvoidcreateSocket()throwsException{
s=newSocket("127.0.0.1",8888);
}
//登录方法
publicvoidlogin()throwsException{
do{
Stringstr=JOptionPane.showInputDialog("请输入用户名");
//写给服务器
pw=newPrintWriter(s.getOutputStream(),true);
//登录:
头+用户名
pw.println("%LOGIN%:
"+str);
try{
br=newBufferedReader
(newInputStreamReader(s.getInputStream()));
Stringcomm=br.readLine();
if(comm.split(":
")[0].equals("%LOGIN%")){
Stringresult=comm.split(":
")[1];
if(result.equals("OK")){
System.out.println("登录成功!
");
break;
}
else{
System.out.println("用户名已存在请重新输入");
}
//
}
}catch(IOExceptione){
e.printStackTrace();
}
}
while(true);
}
publicstaticvoidmain(String[]args)throwsException{
newChatRoomClient();
}
privateclassChatRommClientThreadextendsThread{
BufferedReaderbr;
publicvoidrun(){
}
}
//
}
2.得到好友列表
publicclassChatRoomServer{
privateServerSocketserver;
privateMap
publicChatRoomServer()throwsException{//完成对象的初始化
server=newServerSocket(8888);
onlineUsers=newHashMap
}
publicvoidstartServices()throwsException{//完成一个接收客户端的动作
Sockets=null;
while(true){
s=server.accept();
newChatRoomServerThread(s).start();
}
}
publicstaticvoidmain(String[]args)throwsException{
newChatRoomServer().startServices();
}
//读线程:
内部类
privateclassChatRoomServerThreadextendsThread{
privateSockets;
privateBufferedReaderbr;//输入缓冲流
privatePrintWriterpw;//输出流
publicChatRoomServerThread(Sockets){
this.s=s;
//brpw一定要是同一个socket
try{
br=newBufferedReader(newInputStreamReader(s
.getInputStream()));
pw=newPrintWriter(s.getOutputStream(),true);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicvoidrun(){
//pw=newPrintWiter();//只在run()方法中有效
//br=
try{
while(true){
Stringcomm=null;
comm=br.readLine();
System.out.println(comm);
if(comm.split(":
")[0].equals("%LOGIN%")){
Stringusername=comm.split(":
")[1];
if(onlineUsers.containsKey(username)){
//REFUSE:
用户名已存在
pw.println("%LOGIN%:
REFUSE");
}
else{//OK:
登录成功
pw.println("%LOGIN%:
OK");
onlineUsers.put(username,s);
//发送好友列表
sendOnlineUserList();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 网络 编程