Matlab和java间socket通信

这是老师给我安排的小任务,服务器和Matlab间socket通信,为了保证数据的完整,在socket的基础上给每个包加上头部和尾部,并且要求有断点续传功能,该功能尚未完善,为了区开分头部尾部,但凡在数据中出现头部和尾部的标识都要加上注释。
为了上位机编程方便头部用’<’,尾部用’>’,转义符用’@’。

这是Matlab初始化代码,有连接信息,解包相关参数的初始化。

function init
global data;
global begin
global endbit;
global esc;
global isBegin;
global isEsc;
global isEnd;
global conn;

begin = 60;
endbit= 62;
%92是反斜扛
esc=64;

isBegin=0;
isEsc=0;
isEnd=0;
data=[];

1
conn= tcpip('192.168.1.149',4014);
try
    2
    logfunction('Server connection success');
    fopen(conn);
    logfunction('Open data stream');
catch
     3
    logfunction('Failed to connect to the server');
    pause(1)
    init;
end

end

这是socket通信主要部分

function main

global data;
global begin
global endbit;
global esc;
global isBegin;
global isEsc;
global isEnd;

global conn;
 echotcpip('on',2000);
init;

 while 1

      try
%         fwrite(conn,'h');
        A= fread(conn,1);

      catch
          4
          logfunction('Server connection disconnected'); 
          pause(1)
          init;
          continue;
      end
      try
        if(isEsc == 1 && A == esc )
             data(length(data)+1)=A;
             isEsc = 0;
             continue
        end


         if(isEsc == 1 && A ~= esc && A ~= begin && A ~= endbit )

            isBegin=0;
            isEsc=0;
            isEnd=0;
            data=[];
         end

        if (A==begin&&isBegin==0)
            isBegin=1;
            continue;
        end

        if (isBegin==1&&A==esc)
            isEsc=1;
            continue;
        end




        if (isEnd ==1&&isEsc==1)
            data(length(data)+1)=A;

            isEsc = 0;
            continue;
        end


        if (A== endbit&&isEsc==0&&isBegin==1)
            isBegin=0;
            isEsc=0;
            isEnd=0;
            %此处将data持久化
            logfunction(data);
            char(data)
            data=[];


            continue;
        end

        if (isEsc==1&&A==begin&&isBegin == 1)
            data(length(data)+1)=A;
            isEsc = 0;
            continue;
        end

        if (isEsc==1&&A==endbit)
            data(length(data)+1)=A;
            isEsc = 0;
            continue;
        end
        if (isBegin==0)
            continue;
        end


        data(length(data)+1)=A;
      catch
          5
          logfunction('The specified amount of data was not returned within the Timeout period. ');
          pause(0.5)
          init;
          continue;
      end

end

Java代码
这是服务住程序
每次连接开启新的线程,为了程序的健壮使用了线程池

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketServer extends Thread

{

    // 设置端口号

    public static int portNo = 4014;

    ServerSocket server = null;
    Socket sk = null;
    ExecutorService threadPool = Executors.newScheduledThreadPool(20);
    Map<ClientId, CutPoit> clientIDMap = new HashMap<ClientId, CutPoit>();

    public SocketServer() {
        try {
            server = new ServerSocket(portNo);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (true) {
            System.out.println("Listenning...");
            try {
                // 每个请求交给一个线程去处理
                sk = server.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            ServerThread st = new ServerThread(sk);
            threadPool.execute(st);

        }
    }

    public static void main(String[] args) throws IOException {
        SocketServer s = new SocketServer();
        s.run();
    }
}

每个线程执行的任务

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

public class ServerThread implements Runnable {

    Socket sk = null;
    String clientCode = null;

    public ServerThread(Socket sk) {
        this.sk = sk;
    }

    public void run() {
        OutputStream out = null;
        InputStream in = null;
        try {
            out = sk.getOutputStream();
            in = sk.getInputStream();
            String name = Thread.currentThread().getName();
            System.out.println("执行当前任务的线程为:" + name);

            while (true) {
                out.write(util.SocketUtil.createFrame(test.DataSource.getData()));
                byte[] buff1 = { '<', 'M', '>', '<', 'M', '>'};
                byte[] buff2={'<', 'M', 'M', 'M',
                        'M', 'M', 'M', 'M', 'M', 'M', 'M' };
                byte[] buff3={'M', 'M', 'M',
                        'M', 'M', 'M', 'M', 'M', 'M', '>' };
                out.write(buff1);
                out.write(buff2);
                out.write(buff3);
                Thread.sleep(1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                if (sk != null) {
                    sk.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

工具类,对元数据进行编码


import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import test.DataSource;

public class SocketUtil {
    private static final byte head = '<';
    private static final byte end = '>';
    private static final byte esc = '@';

    public static byte[] createFrame(byte[] b) {
        List<Byte> buffer = new ArrayList<Byte>();
        byte[] frame = null;
        buffer.add(head);
        for (int i = 0; i < b.length; i++) {
            if (b[i] == head || b[i] == end || b[i] == esc) {
                buffer.add(esc);
                buffer.add(b[i]);
            } else {
                buffer.add(b[i]);
            }
        }
        buffer.add(end);
        Byte[] buff = (Byte[]) buffer.toArray(new Byte[buffer.size()]);
        frame = new byte[buff.length];
        for (int i = 0; i < buff.length; i++) {
            frame[i] = buff[i].byteValue();
        }
        return frame;

    }

    @Test
    public void test() {
        byte[] b = createFrame(DataSource.getData());
        for (byte bb : b) {
            System.out.print((char) bb);
        }
    }
}

测试类
随机产生数据


import java.util.Random;

import org.junit.Test;

public class DataSource {

    public static byte[] getData() {
        Random random = new Random();
        int key = random.nextInt(5);
        switch (key) {
        case 0:

            return "hi".getBytes();
        case 1:
            return "@11>".getBytes();

        case 2:

            return "<22@@2222".getBytes();
        case 3:

            return "><3<3@<@@@>333".getBytes();
        case 4:

            return ">44@4444<".getBytes();

        default:
            return "1111".getBytes();

        }

    }

    @Test
    public void test1() {
        byte[] data = getData();
        for (int i = 0; i < data.length; i++) {
            System.out.print((char)data[i]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值