Java網絡編程:TCP實現聊天

客戶端

package com.zhl.nett;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * client客戶端
 */
public class TcpClient1 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os= null;
        //1知道服務器的地址
        try {
            InetAddress severIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2創建socket鏈接
             socket = new Socket(severIP, port);
            //3發送消息io流
             os =socket.getOutputStream();
            os.write("你好,11歡迎學習網絡編程".getBytes());


        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

服務端

package com.zhl.nett;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * sever服務端
 */
public class TcpSever1 {
    public static void main(String[] args) {
        ServerSocket serverSocket1 = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //我得有1個地址
             serverSocket1 = new ServerSocket(9999);

            while (true){
                //2等待客戶端連接
                socket = serverSocket1.accept();
                //3讀取客戶端消息
                is = socket.getInputStream();
                //管道流過濾消息
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while((len=is.read(buffer))!=-1){
                    baos.write(buffer,0,len);//寫入信息
                }
                System.out.println(baos.toString());//輸出避免亂碼

            }



            //異常處理
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //關閉資源,先開後關,注意捕獲異常,標準寫法
            if(baos!=null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
                if (is!=null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                    if(socket !=null){
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                        if(serverSocket1!=null){
                            try {
                                serverSocket1.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                }
            }
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章