Java Socket编程

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TestNetCode {
    public static void main(String[] args) throws IOException {
        String host = "www.horstmann.com";
        InetAddress[] addresses = InetAddress.getAllByName(host);
        for (InetAddress a : addresses) {
            System.out.println(a);
        }

        InetAddress localAddr = InetAddress.getLocalHost();
        System.out.println(localAddr);
        ////建立 Server Socket
        //try (ServerSocket s = new ServerSocket(9200)) {
        //    //等待client连接
        //    try (Socket in = s.accept()) {
        //        System.out.println("111111111111111111111");
        //        InputStream inStream = in.getInputStream();
        //        OutputStream outStream = in.getOutputStream();
        //        try(Scanner inScanner = new Scanner(inStream)) {
        //            PrintWriter out = new PrintWriter(outStream, true /*autoFlush*/);
        //            out.println("Hello! Enter BYE to exit.");
        //
        //            //echo client input
        //            boolean done = false;
        //            while (!done && inScanner.hasNextLine()) {
        //                String line = inScanner.nextLine();
        //                out.println("Echo: " + line);
        //                if (line.trim().equals("BYE")) {
        //                    done=true;
        //                }
        //            }
        //
        //        }
        //
        //    }
        //}
        ServerSocket s1 = new ServerSocket(9200);
        while(true) {
            Socket incoming = s1.accept();
            Runnable r = new ThreadedEchoHandler(incoming);
            Thread t = new Thread(r);
            t.start();
        }
    }
}

class ThreadedEchoHandler implements Runnable {
    private Socket in;
    ThreadedEchoHandler(Socket incoming) {
        this.in = incoming;
    }
    @Override
    public void run(){
        try {
            InputStream inStream = in.getInputStream();
            OutputStream outStream = in.getOutputStream();
            try(Scanner inScanner = new Scanner(inStream)) {
                PrintWriter out = new PrintWriter(outStream, true /*autoFlush*/);
                out.println("Hello! Enter BYE to exit.");

                //echo client input
                boolean done = false;
                while (!done && inScanner.hasNextLine()) {
                    String line = inScanner.nextLine();
                    out.println("Echo: " + line);
                    if (line.trim().equals("BYE")) {
                        done=true;
                    }
                }

            }
            finally {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

可以创建单次连接,

ServerSocket s1 = new ServerSocket(9200);建立Socker Server对象,
Socket in = s.accept()等待client连接。

用线程建立一对多服务。这种方法不能满足高性能服务的需求。为了使服务器实现更高的吞吐量,可以使用java.nio包中的特性。

参考:https://www.ibm.com/developerworks/java/library/j-javaio/

java.nio 建立不阻塞的IO操作是围绕selectors和channals。

Java Socket编程_第1张图片

nio的简单实现:参考https://blog.csdn.net/qq_36666651/article/details/80955398

 

你可能感兴趣的:(Java,进阶知识,Socket,Java,网络编程,nio)