首页 > 基础资料 博客日记

Java网络编程

2023-08-08 18:13:46基础资料围观191

这篇文章介绍了Java网络编程,分享给大家做个参考,收藏Java资料网收获更多编程知识

案例1:
public
class API2 { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9999); //连接上后,生成Socket,通过socket.getOutputStream()得到和socket对象关联的输出流对象 OutputStream outputStream = socket.getOutputStream(); //通过输出流,写入数据到数据通道 outputStream.write("diwhfoiwv".getBytes()); //关闭对象和socket必须关闭 outputStream.close(); socket.close(); System.out.println("客户端退出..."); } }
public class API {
    public static void main(String[] args) throws IOException {
        //创建客户端服务器,在本机的9999端口,等待连接。当没有
        //客户端连接9999端口时,程序会阻塞,等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        System.out.println(socket+"连接成功");
        InputStream inputStream = socket.getInputStream();
        //IO流读取
        byte[] buf = new byte[10];
        int read=0;
        while ((read = inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,read));
        }
        inputStream.close();
        socket.close();
    }
}

 案例二:

1.编写一个服务器和客户端

2.服务器端在9999端口监听

3.客户端连接到服务器,发送“hello server”,并接收服务器端回发的“hello client”

4.服务器端接收并发送

服务器
public class API {
    public static void main(String[] args) throws IOException {
        //创建客户端服务器,在本机的9999端口,等待连接。当没有
        //客户端连接9999端口时,程序会阻塞,等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        System.out.println("客户端连接成功");
        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int read=0;
        while ((read=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,read));
        }
        socket.shutdownInput();
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("你好".getBytes());
        socket.shutdownOutput();
        inputStream.close();
        outputStream.close();
    }
}
客户端
public class API2 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        System.out.println("服务器连接成功");
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("你好".getBytes());
        socket.shutdownOutput();

        InputStream inputStream = socket.getInputStream();
        int read = 0;
        byte[] buf= new byte[1024];
        while ((read=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,read));
        }
        socket.shutdownInput();
        inputStream.close();
        outputStream.close();
    }
}

使用字符流传送信息:

 

 

 

服务器
public class API {
    public static void main(String[] args) throws IOException {
        //创建客户端服务器,在本机的9999端口,等待连接。当没有
        //客户端连接9999端口时,程序会阻塞,等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        System.out.println("客户端连接成功");
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("你好 字符流");
        bufferedWriter.newLine();
        bufferedWriter.flush();
        bufferedReader.close();
        bufferedWriter.close();
    }
}

客户端
public class API2 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        System.out.println("服务器连接成功");
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("你好 字符流");
        //表示写入内容结束
        bufferedWriter.newLine();
        bufferedWriter.flush();


        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);
        bufferedReader.close();
        bufferedWriter.close();
} }
socket.close();

文章来源:https://www.cnblogs.com/maqingsong/p/16543819.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云