简单的网络通信举例

简单的网络通信举例

使用java简单实现一个小功能,有客户端和服务端,客户端发信息给客户端,客户端收到信息即为成功。

上代码

import org.junit.Test;

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

/**
 * 客户端向服务端发送信息,服务端接收信息
 */
public class SocketTest {
    
    

    /**
     * 客户端
     * @throws IOException
     */
    @Test
    public void client(){
    
    

        Socket socket = null;
        OutputStream outputStream = null;
        try {
    
    
            InetAddress inetAddress = InetAddress.getByName("192.168.33.220");
            socket = new Socket(inetAddress, 8890);

            outputStream = socket.getOutputStream();
            outputStream.write("hello,你好!".getBytes());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                outputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                socket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }



    }

    /**
     * 服务端
     * @throws IOException
     */
    @Test
    public void server() {
    
    

        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream acceptInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
    
    
            serverSocket = new ServerSocket(8890);

            accept = serverSocket.accept();
            acceptInputStream = accept.getInputStream();

            //使用ByteArrayOutputStream防止拼接时产生乱码
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bytes = new byte[2 << 10];
            int read;
            while ((read = acceptInputStream.read(bytes))!=-1){
    
    
                byteArrayOutputStream.write(bytes,0,read);
            }

            System.out.println("***收到来自于 "+accept.getInetAddress().getHostAddress()+" 的信息***");
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                byteArrayOutputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                acceptInputStream.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                accept.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
            try {
    
    
                serverSocket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

先启动服务端,再启动客户端,然后服务端收到客户端发送的消息。
在这里插入图片描述
可以看到服务端收到了信息。

猜你喜欢

转载自blog.csdn.net/liuliusix/article/details/111277329
今日推荐