Java互连网编制程序

上节回想:在攻读了Socket建立客户端和服务器双向沟通后,大家发现它的问答情势不够灵活。

本节课大家上课怎样在服务器和客户端之间达成聊天室~

亟需思虑的难点:

劳动器端:2个线程专门发送消息,三个线程专门接收音讯。

客户端:三个线程专门发送音讯,贰个线程专门接收音讯。

除此之外消化这段代码外,还索要思想,怎么落到实处多少个聊天室!(难度较大!!)

【示例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();

           }

       }

    }

}

 

【示例二】聊天室客户端

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笔记」是一部能帮大家从零到10%长为全栈Java工程师体系笔记。我江湖人队称
Mr.
G,10年Java研究开发经验,曾在神州数码、航天院某所研究开发宗旨从事软件设计及研究开发工作,从小白渐渐做到工程师、高工、架构师。通晓Java平台软件开发,了解JAVAEE,熟谙各样流行开发框架。

 笔记包罗从浅入深的六大学一年级些:

 A-Java入门阶段

 B-数据库从入门到通晓

 C-手刃移动前端和Web前端

 D-J贰EE从询问到实战

 E-Java高级框架精解

 F-Linux和Hadoop 

发表评论

电子邮件地址不会被公开。 必填项已用*标注