开发者

Something wrong with my socket program

I wrote a program communicated with sockets.But I don't know why they don't work.

Server Code:

this.serverSocket = new ServerSocket(ServerConnector.port);
        this.socketListener = this.serverSocket.accept();
        System.out.println(this.socketListener.getPort());

        this.objIn = new ObjectInputStream(this.socketListener.getInputStream());
        System.out.println("1");

        this.objOut = new ObjectOutputStream(this.socketListener.getOutputStream());
        System.out.println("1");

        this.objOut.writeInt(19999);
        System.out.println("1");

        this.objOut.writeObject(new Date());
        System.out.println("1");

Client Code:

this.clientSocket = new Socket(ClientConnector.host, ClientConnector.port);

        System.out.println(this.clientSocket.getPort());
        this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
        System.out.println("1");
        this.objOut = new ObjectOutputStream(this.clientSocket.getOutputStream());
        System.out.println("1");

        int i = (Integer) this.objIn.readInt();
        System.out.println(i);
        Date date = (Date) this.objIn.readObject();

The truth is, they don't show any information I suggested to pass through(19999 and date), they even can't print a line of "1"(I added for testing). It means even the line below can't work normally. I really conf开发者_StackOverflow中文版used by these, who can figure the error out?

this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());


You are most likely experiencing the effect of Nagle's Algorithm. which tries to optimize packet sending in TCP. If you want to send your data immediately you need to disable it using the setTcpNoDelay method on the socket interface.

P.S. no idea why the question is tag'ed as osgi, as it has no relevance to OSGi at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜