栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java使用Socket 客户端与服务端 通信

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用Socket 客户端与服务端 通信

Socket 客户端与服务端 通信 接收发送消息

服务端接收发送消息:

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


public class SocketServerTest {

    public static void main(String[] args) throws IOException {
        //9999为server端端口号
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        byte[] bytes = new byte[1024];
        InputStream inputStream = socket.getInputStream();
        int line = 0;
        while ((line = inputStream.read(bytes))!= -1){
            System.out.println("服务端收到消息: "+new String(bytes,0,line));
        }
        //关闭输入流,但socket仍然是连接状态,相当于给流中加入一个结束标记
        socket.shutdownInput();
        OutputStream outputStream = socket.getOutputStream();
        //发送消息
        outputStream.write("hello,client".getBytes());
        socket.shutdownOutput();
        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }

}

客户端发送接收消息:

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


public class SocketClientTest {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello,server".getBytes());
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = 0;
        while ((read = inputStream.read(bytes))!=-1){
            System.out.println("客户端收到消息: "+new String(bytes,0,read));
        }
        inputStream.close();
        outputStream.close();
        socket.close();
    }

}

注意:启动时,需要先启动服务端,在启动客户端

接收发送文件

服务端接收文件并保存:

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


public class SocketServerTest1 {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        //接收的文件路径
        String path = "D:/1234.csv";
        File file = new File(path);
        if(!file.exists()){
            file.createNewFile();
        }
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        while ((len = inputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,len);
        }
        System.out.println("文件已保存。。。");
        bufferedOutputStream.close();
        inputStream.close();
        socket.close();

    }

}

客户端发送文件:

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


public class SocketClientTest1 {

    public static void main(String[] args) throws IOException {
        //要发送的文件路径
        String path = "D:/tmp/202108041124.csv";
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
        File file = new File(path);

        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = new byte[1024];
        int len = 0;
        OutputStream outputStream = socket.getOutputStream();
        while ((len = bufferedInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
        }
        System.out.println("发送完成。。。");
        outputStream.close();
        bufferedInputStream.close();
        socket.close();
    }

}

注意:启动时,需要先启动服务端,在启动客户端

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号