socketchannel

Java Selector returns SelectionKey with OP_READ without data in infinity loop after writing to channel

拜拜、爱过 提交于 2019-12-21 05:20:37
问题 I've trouble with my code: i've written simple SocketChannel client with Selector, after starting it successfully reads messages from server (server sends events). But after writing to socket (see main method) selector starts returning readable socket in infinyty loop, handleKey returns that -1 bytes readed, so selector all time returns OP_READ SelectionKey without data for reading. Sorry for my English. Thanks. import java.io.BufferedReader; import java.io.IOException; import java.io

Java NIO Socket Channel mismatch in size of sent and received bytes

ぃ、小莉子 提交于 2019-12-13 08:01:06
问题 I have the server and the client. Server sends data using this: private int writeMessage(AgentHandler agentHandler, Msg message) { SocketChannel sc = agentHandler.getSocketChannel(); byte[] encodedMessage = Encoder.INSTANCE.encode(message); ByteBuffer writeBuffer = ByteBuffer.wrap(encodedMessage); int count = 0; while (writeBuffer.hasRemaining()) { try { int written = sc.write(writeBuffer); count += written; } catch (IOException e) { deregisterAgent(agentHandler.getAgentID()); } } System.out

HttpPost: InputDispatcher: “Channel is unrecoverably broken and will be disposed!” on Nexus 7

◇◆丶佛笑我妖孽 提交于 2019-12-11 11:41:49
问题 On Nexus 7 (4.3), and not on my older device, LG Optimus 3d (Android 2.2), when I do HttpPost, I get this E/InputDispatcher﹕ channel '4273f7b0 ... MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed! People have mentioned a possible memory leak. See **. However, this problem happens right away on startup when I try the HttpPost. Is it still likely a memory leak? Here is how I'm doing the HttpPost: public void server_addUserGetId() { String url = GS.baseUrl() + "

Manage Client Socket Pool

偶尔善良 提交于 2019-12-10 11:08:09
问题 I need to manage long running TCP socket connections to an external server from my Java application. I'm looking for a good socket pool so I will be able to re-use the sockets. There is another solution than org.apache.commons.pool2 ? 回答1: Here are some object pool implementations you may try: Vibur Object Pool fast-object-pool I suggest you to benchmark the object pool implementations in order to find the most appropriate one for your project. 来源: https://stackoverflow.com/questions/43735067

Java NIO. SocketChannel.read method all time return 0. Why?

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:57:57
问题 I try understand how works java NIO. In particular, how works SocketChannel. I wrote code below: import java.io.*; import java.net.*; import java.nio.*; import java.nio.channels.*; public class Test { public static void main(String[] args) throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress("google.com", 80)); while (!socketChannel.finishConnect()) { // wait, or do something else... }

socketchannel.write() becomes very slow when message size is large

六眼飞鱼酱① 提交于 2019-12-10 00:03:06
问题 In my program using java nio, the socketchannel.write() becomes very slow when it tries to write 10 KB messages consecutively. The measured time for writing a complete 10 KB message is between 160 ms and 200 ms. But the time for writing a complete 5 KB message is only takes 0.8 ms. In the selector, I only have Selection.OP_READ and do not handle Selection.OP_WRITE. When a large complete message is received, it is written to another receiver 4 times. Is anyone accounter same problem? There is

Java NIO read() End Of Stream

自古美人都是妖i 提交于 2019-12-06 11:56:00
I'm using Java NIO in to accept, read and write in my server. In the documentation it said that the SocketChannel.read() function will return the number of bytes read from socket, and -1 if it reach end of stream. Now - i don't really understand what does "End Of Stream" mean? is it as same as borken pipe error ? Does it mean that the connection is lost for both side and i should close the SocketChannel ? I would be really great full if someone can explain more about it - and give some example of using and behaving correctly according to it.. End Of Stream means there is nothing more to read

Manage Client Socket Pool

99封情书 提交于 2019-12-06 07:34:50
I need to manage long running TCP socket connections to an external server from my Java application. I'm looking for a good socket pool so I will be able to re-use the sockets. There is another solution than org.apache.commons.pool2 ? Here are some object pool implementations you may try: Vibur Object Pool fast-object-pool I suggest you to benchmark the object pool implementations in order to find the most appropriate one for your project. 来源: https://stackoverflow.com/questions/43735067/manage-client-socket-pool

Read timeout for an NIO SocketChannel? [duplicate]

妖精的绣舞 提交于 2019-12-06 06:33:00
问题 This question already has answers here : Timeout for SocketChannel doesn't work (3 answers) Closed last year . What is the best way to set a timeout to close a NIO SocketChannel if there is no data is received for a certain period after the connection is established? 回答1: Either: You are using a Selector , in which case you have a select timeout which you can play with, and if it goes off ( select(timeout) returns zero) you close all the registered channels, or You are using blocking mode, in

socketchannel.write() becomes very slow when message size is large

孤街浪徒 提交于 2019-12-04 19:25:17
In my program using java nio, the socketchannel.write() becomes very slow when it tries to write 10 KB messages consecutively. The measured time for writing a complete 10 KB message is between 160 ms and 200 ms. But the time for writing a complete 5 KB message is only takes 0.8 ms. In the selector, I only have Selection.OP_READ and do not handle Selection.OP_WRITE. When a large complete message is received, it is written to another receiver 4 times. Is anyone accounter same problem? There is a post about socketchannel.write() slow. My question is how to alternate change between OP_READ and OP