• Java实例开发0501 简单的多线程服务器


    知识点:
    1.Socket编程:ServerSocket类   Socket类
    2.Java多线程:编程原则(能够独立在一个线程中完成的操作就应该开辟一个新的线程)
    3.telnet命令
     
    [注]win7是有telnet功能的,只是默认没有开启,开启方法:
    打开“控制面板”,找到程序和功能,然后点击左上方的“打开或关闭windows功能”,会弹出windows功能的对话框,这个对话框里面有许多windows的小功能,再找到TelentClient,打上勾,确认即可.
     
    源代码:
    /**
    * @Author:胡家威  
    * @CreateTime:2011-9-20 下午08:30:28
    * @Description:
    */


    package com.yinger.chap51;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Date;

    public class ReceiveServer {

      final int PORT=9090;

      public static void main(String[] args) {
        new ReceiveServer();
      }

      public ReceiveServer(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        ServerThread serverThread = null;
        try {
          serverSocket = new ServerSocket(PORT);
          System.out.println("Server is start!\t"+new Date());
          System.out.println("port:"+serverSocket.getLocalPort());
          System.out.println("timeout:"+serverSocket.getSoTimeout());
          while(true){
            socket = serverSocket.accept();
            serverThread = new ServerThread(socket);
            serverThread.start();
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

    }

    class ServerThread extends Thread{
      Socket socket;
      BufferedReader br;
      PrintWriter pw;

      public ServerThread(Socket socket) {
        this.socket = socket;
        try {
          // 对socket的输入输出流进行封装
          br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);//true
          pw.println("Welcom to server!\t"+new Date());
          pw.println("LocalPort:"+socket.getLocalPort());
          pw.println("Port:"+socket.getPort());
          pw.println("what can I do for you?");
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      @Override
      public void run() {
        String command;
        while(true){
          try {
            command= br.readLine();
            if(command==null || command.trim().equalsIgnoreCase("exit")){
              break;
            }else if(command.trim().equalsIgnoreCase("help")){
              pw.println("HELP\tQUERY\tEXIT");
            }else if(command.trim().equalsIgnoreCase("query")){
              pw.println("I don't know!");
            }else{
              pw.println("command is not found!Please see HELP!");
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        try {
          socket.close();//关闭连接!
        } catch (IOException e) {
          e.printStackTrace();
        }
      }


    }
     
     

    效果展示:

    服务器启动:

    建立连接:

    测试:





  • 相关阅读:
    memcached源码剖析5:并发模型
    memcached源码剖析4:并发模型
    深入剖析php执行原理(4):函数的调用
    深入剖析php执行原理(2):函数的编译
    剖析php脚本的超时机制
    strerror的坑
    深入理解php中的ini配置(2)
    深入理解php中的ini配置(1)
    一个“日期”字符串进行比较的case
    用valgrind检测php扩展内存泄露
  • 原文地址:https://www.cnblogs.com/yinger/p/2182882.html
Copyright © 2020-2023  润新知