Questions tagged [socketchannel]

Issues related to socket channel for stream-oriented connecting sockets in Java

A socket channel represents a "selectable channel" for stream-oriented connecting sockets in Java. It is part of the Java NIO library.

249 questions
2
votes
3 answers

How to get data from TCP socket into a ByteBuffer

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…
user1372020
  • 91
  • 2
  • 2
  • 12
2
votes
5 answers

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

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)…
user471011
  • 7,104
  • 17
  • 69
  • 97
2
votes
1 answer

java.nio.channels.IllegalBlockingModeException while reading content from client through a channel

Am very new to NIO am building a chat application i got connection in all the client but while reading content from client i got java.nio.channels.IllegalBlockingModeException. please help me here is the code where the exception occures. while…
Amith
  • 1,907
  • 5
  • 29
  • 48
2
votes
3 answers

PubNub best practice: How to manage private rooms?

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…
Baylock
  • 1,246
  • 4
  • 25
  • 49
1
vote
1 answer

Android: Find peers on Wifi Network

I'm trying to figure out how to search for other devices logged into a wifi network that are hosting the application on a specific port. How can I detect the presence of these other devices without knowing their address or even necessarily the port…
bgroenks
  • 1,859
  • 5
  • 34
  • 63
1
vote
1 answer

Sending multiple messages through only one SocketChannel

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…
Leo
  • 1,433
  • 23
  • 40
1
vote
2 answers

java nio socketChannel read always return same data

In client side, read code: byte[] bytes = new byte[50]; //TODO should reuse buffer, for test only ByteBuffer dst = ByteBuffer.wrap(bytes); int ret = 0; int readBytes = 0; boolean fail = false; try { while ((ret = socketChannel.read(dst)) > 0) { …
Eric Yu
  • 11
  • 1
  • 4
1
vote
2 answers

SocketChannel.read() blocks indefinitely

I'm having a hard time figuring this one out. I have the following code: if (selector.select(1000) <= 0) { return; } Set selectionKeys = selector.selectedKeys(); for (SelectionKey key : selectionKeys) { …
cdecker
  • 4,515
  • 8
  • 46
  • 75
1
vote
2 answers

ReadableByteChannel hangs on read(bytebuffer)

Im working on Instant messenger using java 1.6. IM uses multithreading - main thread, receiving, and ping. For tcp/ip communication I used SocketChannel. And it seems there is a problem with receiving bigger packages from server. Server instead of…
1
vote
1 answer

how to create multiple UDP Datagram channels/streams on same local/src address

I have to receive data from diff clients on same port of server. I want to create separate channels for each client for this purpose and receive data on each of them. I am exploring all the options available to design this. Option 1 : Listen on a…
Onki
  • 1,879
  • 6
  • 38
  • 58
1
vote
1 answer

With a non-blocking SocketChannel, is the affiliated Socket blocking?

I'm developing an Android app, trying to do a non-blocking write from one thread on a socket, while doing a blocking read on another thread. I'm looking through SocketChannel docs and trying to figure out what exactly configureBlocking does.…
lacker
  • 5,470
  • 6
  • 36
  • 38
1
vote
1 answer

How to read from & write to ByteBuffer correctly?

I can't manage to get all data from byteBuffer. I have to methods as follows: Client side: public String sendMessage(String msg) { buffer = ByteBuffer.wrap(msg.getBytes()); String response = null; try { …
Alxa
  • 79
  • 1
  • 2
  • 11
1
vote
1 answer

How does Java NIO constructs - socketchannel and socketserverchannel work internally?

I have some specific questions related to how socketchannel and socketserverchannel work:- When a ServerSocketChannel accepts a connection( please refer to code below), my understanding is it somehow moves the client to a new randomly selected…
user3740951
  • 1,109
  • 2
  • 20
  • 39
1
vote
0 answers

Why can my Server not receive any data from a readable socketChannel?

I'm trying to build a server client application. The clients can connect to the server and are correctly registered. When a client writes data to the channel which connects client and server, the server cannot read any data. The selectorKey…
Knolle
  • 11
  • 2
1
vote
1 answer

Java; NIO - reading large amounts of data from a SocketChannel

I am writing a very simple networking library that I can use for my other projects in the future. Right now, I am having trouble handling reading operations. I have a class that is responsible for handling events from SocketChannels, and it also…
Martin Tuskevicius
  • 2,590
  • 4
  • 29
  • 46