您好,欢迎访问三七文档
回顾:线程的高级应用:同步代码块(同步方法)如果过多的同步就会造成死锁生产者和消费者(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();//侦听一客户端socketSystem.out.println(client+上线了!);//启动线程newThread(newServerReader(client)).start();}}}classServerReaderimplementsRunnable{Sockets;publicServerReader(Sockets){this.s=s;}@Overridepublicvoidrun(){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;SetSocketset;publicServer()throwsException{server=newServerSocket(8888);set=newHashSetSocket();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;privateMapString,SocketonlineUsers;//保存登录的用户名和socketpublicChatRoomServer()throwsException{//完成对象的初始化server=newServerSocket(8888);onlineUsers=newHashMapString,Socket();}publicvoidstartServices()throwsException{//完成一个接收客户端的动作Sockets=null;while(true){s=server.accept();newChatRoomServerThread(s).start();}}publicstaticvoidmain(String[]args)throwsException{newChatRoomServer().startServices();}//读线程:内部类privateclassChatRoo
本文标题:java网络编程二
链接地址:https://www.777doc.com/doc-4922888 .html