socketchannel

ServerSocketChannel stops Accepting after a while on Linux

梦想的初衷 提交于 2021-02-11 14:23:29
问题 I noticed that, when I run my application on a Linux OS, after a while, the server just stops accepting clients. This is a screenshot of wireshark when trying to connect to the Server from my host, after it stopped accepting. As you can see the first request is [FIN,ACK] and it seems like that my server can't handle that. Starting up the server, opens the selector, binds, blocking mode is set to false and the server channel registers for OP_ACCEPT. (the usual standard stuff) And this is the

Using resources of try-with-resources outside try

时光怂恿深爱的人放手 提交于 2021-01-28 03:50:40
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

Using resources of try-with-resources outside try

好久不见. 提交于 2021-01-28 02:44:07
问题 I'm using SocketChannel to send message between a server and a client. Once a client connects with a server, the server opens the InputStreams and OutputStream in a try-with-resources try, to receive messages from the client and send messages to the client, like so: try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {} I want to access out outside of the try. The try contains a while loop that

java: Single socket on read write operation. Full duplex

前提是你 提交于 2020-01-05 12:11:24
问题 I have to implement sending data with specific source port and in the same time listen to that port. Full duplex. Does anybody know how to implement it on java. I tried to create separate thread for listening on socket input stream but it doesnt work. I cannot bind ServerSocket and client socket to the same source port and the the same with netty. It there any solution for dull duplex? init(){ socket = new Socket(InetAddress.getByName(Target.getHost()), Target.getPort(), InetAddress.getByName

Connecting to websocket using SocketChannel Android

大憨熊 提交于 2020-01-04 13:59:12
问题 I have written android app which connects to a websocket server, server app and android app implemented with Autobahn websocket library. I can connect and exchange the messages with server successfully. but after some time(after 20 - 30 mins) android app stop communicating with server(it cannot send messages to server). On logcat shows app still connect to websocket even though it cannot send messages to server. I have went through the source code of Autobahn library and found that it using

Java NIO read() End Of Stream

二次信任 提交于 2020-01-02 09:28:16
问题 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

Java ServerSocketChannel SocketChannel (Callback)

浪子不回头ぞ 提交于 2019-12-30 02:27:30
问题 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. 回答1: You need to use

SocketChannel.read() blocks indefinitely

坚强是说给别人听的谎言 提交于 2019-12-24 15:22:35
问题 I'm having a hard time figuring this one out. I have the following code: if (selector.select(1000) <= 0) { return; } Set<SelectionKey> selectionKeys = selector.selectedKeys(); for (SelectionKey key : selectionKeys) { try { SocketEventHandler handler = (SocketEventHandler) key.attachment(); if (key.isValid() && key.isAcceptable()) { handler.becomesAcceptable(key); } if (key.isValid() && key.isReadable()) { handler.becomesReadable(key); } if (key.isValid() && key.isWritable()) { handler

Java NIO Server/Client Chat App - sending data only by closing the socket

浪子不回头ぞ 提交于 2019-12-24 02:17:18
问题 friends! I'm new to Java NIO and I'm currently trying to make a non-blocking chat app. The client connects to the server without problem. The client writes a message or few messages to the server but the server starts reading the messages only when the Socket connection is closed from the client code, so a SocketChannel (or only Socket) must be created and closed in the client code for every message - this doesn't seems to me right. I've tried the client side with simple Java I/O and also

Android - How to keep connection with server for a long time

天涯浪子 提交于 2019-12-24 02:15:02
问题 I wrote a chat application for Android using SocketChannel. It connects successfully with the server and all features work. But after a long time since I logged in (about 2-3 hours), I try to send a chat message again and it fails. In log file, SocketChannel, selector still open and connect to server, message already write successful. What's the problem? What am I missing? Thanks in advance for your help. 回答1: if you want to create a Chat for android or something else with push from a server,