simpleserver.java

来自「网络+多线程+输入输出流的结合运用」· Java 代码 · 共 41 行

JAVA
41
字号
package socket;
import java.net.*;
    import java.io.*;

public class SimpleServer {
    public static void main(String args[]) {
        ServerSocket s = null;
        Socket s1;
        String sendString = "Hello Net World!!";
        OutputStream s1out;
        DataOutputStream dos;

        // Register your service on port 5432
        try {
                    s = new ServerSocket(5432);
        } catch (IOException e) { }

        int count=1;
        // Run the listen/accept loop forever
        while (true) {
        try {
        // Wait here and listen for a connection
        s1=s.accept();
        System.out.println("accept a new client"+count++);
        // Get a communication stream for socket
        s1out = s1.getOutputStream();
        dos = new DataOutputStream (s1out);

        // Send your string! (UTF provides machine-independent format)
        dos.writeUTF(sendString);

        // Close the connection, but not the server socket
        dos.close();
        s1.close();
        } catch (IOException e) { }
        } //end of while loop
    } //end of method main()
    } //end of class SimpleServer


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?