基于Java完成的一个hello/hi的简单的网络聊天程序

服务器端代码

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(10086);
            Socket socket = null;
            int count = 0;
            System.out.println("服务器启动");
            while (true) {
                socket = serverSocket.accept();
                InetAddress inetAddress=socket.getInetAddress();
                ServerThread thread=new ServerThread(socket,inetAddress);
                thread.start();
                count++;
                System.out.println("客户端数量:" + count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class ServerThread extends Thread{
    Socket socket = null;
    InetAddress inetAddress= null;

    public ServerThread(Socket socket, InetAddress inetAddress) {
        this.socket = socket;
        this.inetAddress = inetAddress;
    }

    @Override
    public void run() {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        OutputStream outputStream = null;
        OutputStreamWriter writer = null;
        try{
            inputStream = socket.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String info = null;
            while ((info = bufferedReader.readLine()) != null) {
                System.out.println("服务器端接收:"+"{'from_client':'"+socket.getInetAddress().getHostAddress()+"','data':'"+info+"'}");
            }
            socket.shutdownInput();
            outputStream = socket.getOutputStream();
            writer = new OutputStreamWriter(outputStream, "UTF-8");
            writer.write("hello");
            writer.flush();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            try {
                if (writer != null) {
                    writer.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

客户端代码

import java.io.*;
import java.net.Socket;


public class client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 10086);
            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter writer=new OutputStreamWriter(outputStream,"UTF-8");
            writer.write("hi");
            writer.flush();
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String info = null;
            System.out.println("客户端IP地址:"+socket.getInetAddress().getHostAddress());
            while ((info = bufferedReader.readLine()) != null) {
                System.out.println("客户端接收:" + info);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            writer.close();
            outputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果图:

服务器端:

客户端:

分析:

一、Java基于Socket类和ServerSocket类实现网络编程

服务器端通信流程:

  • new一个ServerSocket,用于接收用户请求
  • 调用ServerSocket的accept方法获得用户请求的Socket
  • 创建输入流读取客户端发送的数据
  • 创建输出流向客户端发送数据消息

客户端通信流程:

  • new一个Socket,传入参数为服务器的IP和Port
  • 创建输出流向服务器发送数据消息
  • 创建输入流读取服务器发送来的数据消息 

基于Java完成的一个hello/hi的简单的网络聊天程序_第1张图片

 二、Java Socket API与Linux Socket API

// 创建一个套接字
int socket(itn domain, int type, int protocol);
// 将套接字绑定到地址上
int bind(int sockfd, const struct sockaddr *addr, socklen_t addelen);
// 监听连接
int listen(int sockfd, int backlog);
// 接收一个连接请求
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

 Java的Socket类和ServerSocket类其实是对Linux Socket API的封装,是更高层次的抽象,所以使用Java完成网络编程会更简单。

你可能感兴趣的:(基于Java完成的一个hello/hi的简单的网络聊天程序)