JDK6.0学习笔记(九)tcp多线程服务器

    技术2022-05-12  13

    /** * tcp开发,有连接的Socket操作采用TCP协议,效率低,传输可靠性高 * TCP下的Socket必须在发送数据之前与目的地的Socket取得一个连接 * 一旦建立了连接,Socket就可以使用数据流借口,按照打开-读-写-关闭 的顺序来操作数据流 * 多线程服务器 * 主程序在9080端口侦听到新的客户端的连接请求时,启动一个独立的子线程为其服务  * */import java.net.*;public class StartupEchoServer {    public static void main(String[] args) {        try {            ServerSocket serverSocket = new ServerSocket(9080);//主线程负责接受客户端连接            System.out.println("EchoServer在9080端口上侦听......");            while (true) {                Socket socket = serverSocket.accept();                System.out.println("建立一个连接.../n");                new EchoServer(socket).start();            }        } catch (Exception e) {            e.printStackTrace();        }    }}   /** * tcp开发,多线程服务器客户端  * */import java.io.*;import java.net.*;public class EchoClient {    public static void main(String[] args) {        Socket socket = null;        BufferedReader in = null;        PrintWriter out = null;        try {            socket = new Socket("localhost"9080);// 创建与服务器的连接,获取IP地址            in = new BufferedReader(new InputStreamReader(socket                    .getInputStream()));            out = new PrintWriter(socket.getOutputStream(), true);            out.println("DOW");            System.out.println("今天是:" + in.readLine());            out.println("DOY");            System.out.println("今天是一年的:" + in.readLine());            byte[] b = new byte[2048];            String msg = new String(b, 0, System.in.read(b));            out.println("FREE:" + msg);            System.out.println("收到答复:" + in.readLine());        } catch (Exception e) {        } finally {            try {                if (in != null)                    in.close();                if (out != null)                    out.close();                if (socket != null)                    socket.close();// Socket.close() 客户端主动断开连接            } catch (Exception e1) {            }        }    }} /** * tcp开发,多线程服务器 * 子线程负责处理客户端的请求 * */import java.io.*;import java.net.*;import java.util.*;public class EchoServer extends Thread {    private Socket socket = null;//主程序传递来的Socket对象    public EchoServer(Socket s) {        this.socket = s;    }    public void run() {        BufferedReader in = null;        PrintWriter out = null;        Calendar c = Calendar.getInstance();//子线程负责处理客户端的请求        try {            in = new BufferedReader(new InputStreamReader(this.socket                    .getInputStream()));//取得输入流            out = new PrintWriter(this.socket.getOutputStream(), true);//取得输出流            do {                String msg = in.readLine();                if (msg == null)                    break;                msg = msg.toUpperCase();                System.out.println("收到指令:" + msg);                if (msg.equals("DOW"))//返回今天是星期几                    switch (c.get(Calendar.DAY_OF_WEEK)) {                    case Calendar.SUNDAY:                        out.println("SUNDAY");                        break;                    case Calendar.MONDAY:                        out.println("MONDAY");                        break;                    case Calendar.TUESDAY:                        out.println("TUESDAY");                        break;                    case Calendar.WEDNESDAY:                        out.println("WEDNESDAY");                        break;                    case Calendar.THURSDAY:                        out.println("THURSDAY");                        break;                    case Calendar.FRIDAY:                        out.println("FRIDAY");                        break;                    case Calendar.SATURDAY:                        out.println("SATURDAY");                    }                if (msg.equals("DOY"))//今天是一年的第几天                    out.println("" + c.get(Calendar.DAY_OF_YEAR));                if (msg.startsWith("FREE"))//客户端的信息以FEEE开头,返回信息原文                    out.println(msg.substring(5));            } while (true);        } catch (Exception e) {        } finally {            try {                System.out.println("关闭一个连接.../n");                if (in != null)                    in.close();                if (out != null)                    out.close();                if (this.socket != null)                    this.socket.close();//ServerSocket.close() 服务器主动断开连接            } catch (Exception e3) {            }        }    }}

    最新回复(0)