簡單的tcp/ip 協議java代碼

//服務端
public class Server{
public static void main(String[] args) {
        //建立端口3500的服務器socket
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(3500);
        } catch (Exception e) {
            e.printStackTrace();
        }
 //防止客戶端斷開以後服務端程序掛掉
        while (ss != null) {
            try {
                //在返回客戶端socket以前,accept將會一直阻塞
                Socket s = ss.accept();//接收客戶端的鏈接
                // 將請求指定一個線程去執行
                Thread start = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                            int i = 0;
                            logger.debug("客戶端已連接");
                            while (true) {
                                //發送信息時,須要加上換行符,不然客戶端的readline()會阻塞
                                wr.write("服務器返回消息成功[" + i++ + "]\n");
                                //使用flush方法能夠當即清空buffer,讓消息立刻發出去,不然在buffer滿以前消息都不會發出去
                                wr.flush();
                                String line = br.readLine();
                                logger.debug("來自客戶端的數據:==" +line);

                            }
                        } catch (Exception e) {
//                            e.printStackTrace();
                            logger.debug("客戶端已斷開");
                        }
                    }
                });
                start.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
//客戶端


public class Client {

    public static void main(String[] args) {
        try {
             Socket socket = new Socket("127.0.0.1", 3500);
//            Socket socket = new Socket("120.24.220.136", 3500);
            socket.setKeepAlive(true);
//            socket.setSoTimeout(10);
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            int i = 0;
            boolean isOnline = true;
            while (isOnline) {
                //發送信息時,須要加上換行符,不然服務器端的readline()會阻塞
                wr.write("你好,您收到客戶端的新年祝福[" + i++ + "]\n");
                //使用flush方法能夠當即清空buffer,讓消息立刻發出去,不然在buffer滿以前消息都不會發出去
                wr.flush();
//                 socket.sendUrgentData(0xFF); // 發送心跳包  
                System.out.println("目前處於連接狀態!");

                System.out.println("------------我是分割線------------");
                String line = br.readLine();
                System.out.println("來自服務器的數據:" + line);
                Thread.sleep(1000);
            }
        } catch (Exception ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
相關文章
相關標籤/搜索