Java socket回顾

好久没有接触socket网路编程了,查快要把最基础的都忘记了,今天,写个Server,Client回顾一下。

Server端代码:
import java.io.BufferedReader;   
import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.InputStreamReader;   
import java.net.ServerSocket;   
import java.net.Socket;   
  
public class SecServer {   

    public static final int PORT =10000;   
  
    public static void main(String[] args) {   
        System.out.println("服务器:\n");   
        SecServer server = new SecServer();   
        server.service();   
    }   
  
    public void service() {   
        try {   
            ServerSocket server = new ServerSocket(PORT);   
           while (true) {   
                // 等待接收客户端连接
                Socket client = server.accept();   
                new ThreadServer(client);   
            }   
        } catch (Exception e) {   
            e.printStackTrace();
        }   
    }   
  
    private class ThreadServer implements Runnable {   
  
        private Socket socket;   
  
        public ThreadServer(Socket sock) {   
            socket = sock;   
            new Thread(this).start();   
        }   
  
        public void run() {   
            try {   
 
                DataInputStream input = new DataInputStream(socket.getInputStream());   

                DataOutputStream out = new DataOutputStream(socket.getOutputStream());   
                  
                // 读取客户端数据   
                System.out.println("客户端发过来的内容: " + input.readUTF());   
  
                System.out.print("请输入: \t");   
                // 接收键盘输入
                String s = new BufferedReader(new InputStreamReader(System.in)).readLine();   
                out.writeUTF(s);   
                input.close();   
               out.close();   
            } catch (Exception e) {   
                e.printStackTrace();
            }   
        }   
  
    }   
}  


客户端代码:
import java.io.BufferedReader;   
import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.InputStreamReader;   
import java.net.Socket;   
  
public class SecClient {   
       
    public static final int PORT = 10000;   
       
    public static final String IP = "localhost";   
       
    public static void main(String[] args) {   
        System.out.println("客户端:\n");   
        Socket socket = null;   
        try {   
            System.out.println("当客户端接收到\"exit\" 的时候,将与服务器断开\n");   
            while (true) {   
                // 将socket连接到指定主机上的指定端口号   
                socket = new Socket(IP, PORT);
                   
                DataInputStream input = new DataInputStream(socket.getInputStream());  
 
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());

                System.out.print("请输入: \t");
                String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
                out.writeUTF(str);
   
                System.out.println("服务器端返回过来的是: " + ret);   
                // 如接收到 "exit" 则客户端将于服务器端断开
                if ("exit".equals(input.readUTF())) {   
                    System.out.println("客户端退出");   
                    //Thread.sleep(500);   
                    break;   
                }   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
       } finally {   
            if (socket != null) {   
                try {   
                    socket.close();   
                } catch (Exception e) {   
                    socket = null;   
                    e.printStackTrace();   
                }   
            }   
        }   
    }   
  
}  


先启动服务端,在启动客户端,可实现客户、服务对话。

你可能感兴趣的:(java,thread,编程,.net,socket)