如何实施网络回叫?

时间:2011-07-05 01:00:38

标签: java networking

我创建了一个像这样的界面

public interface NetworkInterface {

    public void onReceive();
}

我有两个实现此接口的类,如此..

public class NetworkManager implements NetworkInterface {

    private NetworkQueue    mNetworkQueue;
    private TCPConnection   mTCPConnection;
    private Worker          mWorker;

    public NetworkManager(){
        mNetworkQueue = new NetworkQueue();
        mTCPConnection = new TCPConnection();
        mWorker = new Worker();
    }

    @Override
    public void onReceive() {
        // TODO Auto-generated method stub

    }

}


public class TCPConnection implements NetworkInterface{

    private final static String IP = "1.1.1.1"; 
    private final static String PORT = "12001";

    private Socket mSocket; 
    private DataOutputStream mOut;
    private InputStream mIn;

    public TCPConnection() throws NumberFormatException, UnknownHostException, IOException{
        mSocket = new Socket(InetAddress.getByName(IP),Integer.valueOf(PORT));
        mOut = new DataOutputStream(mSocket.getOutputStream());
        mIn = mSocket.getInputStream();
    }

    @Override
    public void onReceive() {
        // TODO Auto-generated method stub

    }

}

因此TCPConnection将导入NetworkManager,并且在TCPConnection的onReceive函数中,它将调用NetworkManager的onReceive函数。在NetworkManager的onReceive函数中,将作为参数传递的任何内容添加到工作队列中。

我想知道这是否是接近回叫功能的正确方法。

提前致谢。

1 个答案:

答案 0 :(得分:1)

在开始为其设计框架之前,您需要了解TCP网络的实际工作方式。我建议你阅读Java Tutorial的自定义网络路径。