Matlab和java间socket通信

来源:互联网 发布:2016加入淘宝客的条件 编辑:程序博客网 时间:2024/05/18 03:45

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

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

function initglobal data;global beginglobal 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=[];1conn= 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;endend

这是socket通信主要部分

function mainglobal data;global beginglobal 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;      endend

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]);        }    }}
0 0
原创粉丝点击