⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendrequest.java

📁 JAVA实现的SOCKET通讯源码,里面有双方制定的报文标示!
💻 JAVA
字号:
package testDzjs.sbClient.SendRequest;


import common.DateTime;
import common.ConstPar;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
import java.math.BigInteger;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2005-4-14
 * Time: 17:22:53
 * To change this template use File | Settings | File Templates.
 */
public class SendRequest extends ConstPar{

    private Socket socket;

    static String serIP=QYDZJSSERIP;

    static int serPort=QYDZJSSERPORT;

    static int  socketTimeOut = QYDZJSSERTIMEOUT;


    /**
     * 发送数据
     * @param message
     * @return
     * @throws Exception
     */
    public String sendMessage(byte[] message,Socket socke) throws Exception {
        socket = socke;
//        buileConn();
        BufferedInputStream in;
        BufferedOutputStream out;
        String feedback = "";
        try {
            
            // 获得 socket 输入流
            in = new BufferedInputStream(socket.getInputStream());
            // 获得 socket 输出流
            out = new BufferedOutputStream(socket.getOutputStream());
            //发送数据
            send(message, out);
            //接收数据
            feedback = receive(in);
            in.close();
            out.close();



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return feedback;
    }




    /**
     * 建立socket连接
     * @throws Exception
     */
    private void buileConn() throws Exception {
        try {
            // 创建 socket                                         .

            socket = new Socket(InetAddress.getByName(serIP),serPort);

            socket.setSoTimeout(socketTimeOut);

        } catch (Exception e) {
            if(socket != null){
                socket.close();
                System.out.println("socke close");
            }
            System.out.println("创建SOCKET出错:" + DateTime.getCurrentDateTime() + e.getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 发送数据
     * @param message
     * @param out
     */
    private void send(byte[] message, BufferedOutputStream out){
        try {
            //每次发送1024字节
            int num = (message.length % 1024 == 0) ? message.length / 1024
                    : message.length / 1024 + 1;
            for (int i = 0; i < num; i++) {
                int lenght = (message.length % 1024 != 0 && i == num - 1) ? message.length % 1024
                        : 1024;
                byte[] buffer = new byte[lenght];
                System.arraycopy(message, i * 1024, buffer, 0, lenght);
                out.write(buffer);

            }
            out.flush();
        } catch (Exception e) {
            try {
                this.socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 读取数据
     * @param in
     * @return
     */
    public String receive(BufferedInputStream in){
        try {
            //读取协议头部分
            byte[] head = new byte[16];
            in.read(head);
            //读取有效数据长度
            byte[] length = new byte[4];
            System.arraycopy(head, 4, length, 0, 4);
            int span = new BigInteger(length).intValue();
            byte[] body = new byte[span];
            in.read(body);
            return new String(body);
        } catch (Exception e) {
            try {
                this.socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return "";
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -