java和c#之间的通信与套接字网络

时间:2013-09-22 11:59:53

标签: c# java .net sockets networking

我正在尝试在Unity3D应用程序和Java(Jason)应用程序之间建立套接字网络(使用" BufferedOutputStream"在Java部分和#34; NetworkStream"在C#部分)。这两个模块应该相互通信。这是代码的java部分:

public static void SendExecuteAction(String ag, Structure structure, Object tempObject, ClassEnv classEnv)
    {   
        if(!agentThreadList.containsKey(ag))
        {
            AgentServiceThread ast = new AgentServiceThread(s, agentThreadList.size() - 1, ag, structure, tempObject, classEnv);
            agentThreadList.put(ag, ast);
            if(ast.getState() != Thread.State.BLOCKED)
                ast.start();
            try {
                ast.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else
        {
            agentThreadList.get(ag).start();
        }
    }
}

class AgentServiceThread extends Thread
{
    Socket agentSocket;
    int agentID = -1;
    String agent;
    Structure structure;
    Object blockObject;
    byte[] ba;
    ClassEnv classEnv;

    InputStream is;
    BufferedOutputStream bos;
    ToByteConvertHelper toBytes;
    FromByteConvertHelper fromBytes;


    AgentServiceThread(Socket s, int i, String ag, Structure structure, Object object, ClassEnv classEnv)
    {
        agentSocket = s;
        agentID = i;
        agent = ag;
        this.structure = structure;
        blockObject = object;
        this.classEnv = classEnv;

        try 
        {
            toBytes = new ToByteConvertHelper();
            fromBytes = new FromByteConvertHelper();
            is = agentSocket.getInputStream();
            bos = new BufferedOutputStream(agentSocket.getOutputStream());
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

    public void run()
    {
        VCActionRequest vcar;

        List<String> termList = new ArrayList<String>();

        List<Term> structureTermList = structure.getTerms();

        for(int i = 0; i < structureTermList.size(); i++)
        {
            termList.add(structureTermList.get(i).toString());
        }

        vcar = new VCActionRequest(agent, structure.getFunctor(), (ArrayList<String>)termList);
        vcar.requestEnum = RequestEnum.ExecuteActions.index();

        System.out.println("agent: " + agent + " action: " + structure.getFunctor());

        ba = toBytes.toByte(vcar);

        try 
        {
            bos.write(ba);
            bos.flush();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }

        synchronized(blockObject)
        {
            try 
            {
                ba = new byte[1000];
                is.read(ba, 0, 1000);

                @SuppressWarnings("unchecked")
                ArrayList<VCPerception> result = (ArrayList<VCPerception>)fromBytes.fromByte(ba);

                classEnv.UpdatePercepts(result);
            } 
            catch (IOException e1) 
            {
                System.out.println(e1.toString());
                e1.printStackTrace();
            } 
        }


    }   
}

&#34; SendExecuteAction&#34;方法由另一个方法调用,该方法由Jason架构在其间隔中自动调用。问题是,当我尝试从Unity3D应用程序中获取软件包时,似乎我错过了一些软件包。实际上我从Unity获得了第一个包,但没有获得其他包。我应该使用一些不同的沟通方式吗?我需要读取每条消息的异步。以下是用于从网络流中读取数据的C#代码。

    private IEnumerator CommunicateWithMAS()
    {

        while (!_ns.DataAvailable)
        {
            yield return null;
        }

        byte[] ba = new byte[1000];

        AgentServiceThread ast = new AgentServiceThread(_tc, -1, this, ba);
        Thread thread = new Thread(new ThreadStart(ast.Communicate));

        thread.Start();

        StartCoroutine(CommunicateWithMAS());
    }


public class AgentServiceThread
{
    VCSocketCommScript socketCommScript;
    TcpClient client;
    int id;
    string agentName;

    NetworkStream ns;
    StreamWriter sw;
    StreamReader sr;
    string informationString;
    string responseString;
    FromByteConvertHelper fromBytes;
    byte[] ba;

    int byteArrayLength = 1000;

    public AgentServiceThread(TcpClient client, int id, VCSocketCommScript vcscs, byte[] ba)
    {
        this.client = client;
        this.id = id;
        socketCommScript = vcscs;
        //this.ba = ba;

        fromBytes = new FromByteConvertHelper();

        ns = client.GetStream();
    }

    public void Communicate()
    {
        byte[] ba = new byte[byteArrayLength];

        Debug.Log(ns.DataAvailable);
        ns.Read(ba, 0, byteArrayLength);

        //Debug.Log(ba.Length);

        VCActionRequest action = (VCActionRequest)fromBytes.fromByte(ba, 0, typeof(VCActionRequest));

        Debug.Log(action.agentName + " " + action.functor);

        socketCommScript.AddAction(action);

        ns.Flush();
    }
}

0 个答案:

没有答案