java socket Tcp协议的应用

本文展示了使用Java的Socket编程实现服务端和客户端之间的图像数据传输。服务端通过ServerSocket监听特定端口,接收客户端连接并处理接收到的图像文件,客户端则发送指定文件到服务端,并接收响应消息。
摘要由CSDN通过智能技术生成
1,服务端
public class Server {

    public static void main(String[] args) {
        try {
            final ServerSocket server = new ServerSocket(10000);
            ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Long start=System.currentTimeMillis();
                            System.out.println("start...");
                            Socket socket = server.accept();
                            System.out.println("连接耗时:t="+(System.currentTimeMillis()-start));
                            cachedThreadPool.execute(new Runnable() {
                                @Override
                                public void run() {
                                    String tmpName=receiveImg(socket);
                                    Long end=System.currentTimeMillis();
                                    System.out.println(new Date()+",tmpName="+tmpName+",耗时:t="+(end-start));
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static String receiveImg(Socket socket) {
        byte[] inputByte = null;
        Integer length = 0;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        OutputStream out =null;
        String tmpName="image/"+ UUID.randomUUID()+".jpg";
        try {
            System.out.println("start receive...");
            InputStream inputStream=socket.getInputStream();
            dis = new DataInputStream(inputStream);
            fos = new FileOutputStream(new File(tmpName));
            inputByte = new byte[1];
            while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
                String str=new String(inputByte,0,length);
                if (str.equals("6")){
                    break;
                }
            }
            System.out.println("img receive end");
            //获取输出流,准备给客户端发送消息
            out = socket.getOutputStream();
            out.write("success".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return tmpName;
    }
}

2,客户端

public class Client {
    public static void main(String[] args) {
        int length = 0;
        byte[] sendBytes = null;
        Socket socket = null;
        DataOutputStream dos = null;
        FileInputStream fis = null;
        try {
            try {
                //123.57.63.199
                for (int i=0;i<2;i++){
                    Long start=System.currentTimeMillis();
                    socket = new Socket();
                    socket.connect(new InetSocketAddress("127.0.0.1", 10000),
                            10 * 1000);
                    System.out.println("start...");
                    dos = new DataOutputStream(socket.getOutputStream());
                    Long end=System.currentTimeMillis();
                    System.out.println(new Date()+",耗时:t="+(end-start));
                    File file = new File("D:\\我的文档\\Pictures\\3.jpg");
                    fis = new FileInputStream(file);
                    sendBytes = new byte[1024];
                    while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
                        dos.write(sendBytes, 0, length);
                    }
                    dos.write("\r\n".getBytes());
                    dos.flush();
                    //通知服务端,数据发送完毕
                    socket.shutdownOutput();
                    //3.获取输出流,接受服务器传送过来的消息
                    InputStream in = socket.getInputStream();
                    byte[] bufIn = new byte[1024];
                    int num = in.read(bufIn);
                    String readStr=new String(bufIn,0,num);
                    System.out.println(readStr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值