socketchannel

How to get data from TCP socket into a ByteBuffer

回眸只為那壹抹淺笑 提交于 2019-12-04 19:21:09
I need to get incoming data from a socket into a ByteBuffer and I do not know how to do it. I am new to this field and therefore not sure of the best way to start. I found the following but that is not what I want as it gets the data in line but I need to have all of my data in bytebuffer for other purposes. ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort)); while (true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new

PubNub best practice: How to manage private rooms?

淺唱寂寞╮ 提交于 2019-12-04 14:31:00
问题 I'm learning pubnub and I read their documentation but I just can't find how to manage a multi room chat box. By default, a channel can be listened by anyone. Subscribing to it and publishing on it is easy. What I want is to have a main public room (so far so good) but anyone should also be able to talk privately to anyone else without the risk of being read by other users. These dynamic rooms would be tabbed and the user should be able to go from one to another. Another requirement would be

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

点点圈 提交于 2019-12-03 16:45:55
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.InputStreamReader; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels

PubNub best practice: How to manage private rooms?

故事扮演 提交于 2019-12-03 09:00:00
I'm learning pubnub and I read their documentation but I just can't find how to manage a multi room chat box. By default, a channel can be listened by anyone. Subscribing to it and publishing on it is easy. What I want is to have a main public room (so far so good) but anyone should also be able to talk privately to anyone else without the risk of being read by other users. These dynamic rooms would be tabbed and the user should be able to go from one to another. Another requirement would be that talking privately to someone doesn't kick you out of the other rooms you subscribed to (you can

SSL and SocketChannel

最后都变了- 提交于 2019-12-03 06:02:54
问题 Ideally, I only need a simple SSLSocketChannel . I already have a component that reads and writes message over ordinary SocketChannel , but for some of these connections, I have to use SSL over the wire; the operations over these connections, however, are the same. Does anyone knows a free SSLSocketChannel implementation (with the appropriate selector) or something similar? I've found this, but the selector doesn't accept it since its vendor isn't SUN. I'm decoupling the reading_from/writing

Any issues with replacing new Socket() with SocketChannel.open().socket()?

こ雲淡風輕ζ 提交于 2019-12-02 10:25:09
问题 What can go wrong if I simply replace socket = new Socket() with socket = SocketChannel.open().socket()? Background: I have some legacy code using new Socket() , and I wanted to be able to interrupt the socket.connect() call. I don't want to rewrite the code to use NIO. I learned that Thread.interrupt() does not interrupt socket.connect() , but that socket.close() on another thread is supposed to interrupt the connection. Oddly, that worked with Java 7 but not Java 6. I somehow got it into my

Sending multiple messages through only one SocketChannel

喜你入骨 提交于 2019-12-02 09:28:15
问题 After reading this tutorial: http://rox-xmlrpc.sourceforge.net/niotut/ (it's about writing non-blocking server and client, and I read NIO part, skiped SSL part), now I'm trying to rewrite my own client, but I'm stuck in a problem when trying to edit the client code. Firstly, I want to let you see the client code of the tutorial, it's include 2 files: RspHandler.java : http://rox-xmlrpc.sourceforge.net/niotut/src/RspHandler.java NIOClient.java : http://rox-xmlrpc.sourceforge.net/niotut/src

Sending multiple messages through only one SocketChannel

倖福魔咒の 提交于 2019-12-02 04:45:33
After reading this tutorial: http://rox-xmlrpc.sourceforge.net/niotut/ (it's about writing non-blocking server and client, and I read NIO part, skiped SSL part), now I'm trying to rewrite my own client, but I'm stuck in a problem when trying to edit the client code. Firstly, I want to let you see the client code of the tutorial, it's include 2 files: RspHandler.java : http://rox-xmlrpc.sourceforge.net/niotut/src/RspHandler.java NIOClient.java : http://rox-xmlrpc.sourceforge.net/niotut/src/NioClient.java But I edited NIOClient.java a bit in main function for explanation my problem as below:

Socket vs SocketChannel

你。 提交于 2019-11-30 10:11:38
问题 I am trying to understand SocketChannels, and NIO in general. I know how to work with regular sockets and how to make a simple thread-per-client server (using the regular blocking sockets). So my questions: What is a SocketChannel? What is the extra I get when working with a SocketChannel instead of a Socket. What is the relationship between a channel and a buffer? What is a selector? The first sentance in the documentation is A selectable channel for stream-oriented connecting sockets. .

Java ServerSocketChannel SocketChannel (Callback)

爱⌒轻易说出口 提交于 2019-11-30 07:33:37
I am trying to learn Java. I would like to implement a simple networked connect 4 game as well as a chat feature. I want my network logic to be non blocking so after much study I found that SocketChannel is what I am after regrading my needs. What has not made sense still is the lack of CallBack functions in SocketChannels.. Like one finds in C#. My query for this time is: How do I deliver the data received to the Chat or Game form (JFrame)? Some guidance is most welcome. You need to use a Selector. First create a Selector to receive the events: Selector selector = Selector.open() Then you