How to Ask The Socket to Wait for More Data to Come

How to ask the socket to wait for more data to come

According to the docs:

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer.

So you are never guranteed to get all the data asked for in the receive call. You need to check how many bytes were actually returned by the Receive, then issue another receive call for the remaining bytes. Continue that loop until you get all the bytes you were looking for.

using select to waiting for data on a client socket

The first parameter to select should be the maximum socket id plus 1. So in your case, it should be

socket_desc+1

Can you try with that and see if it works?

The reason it only gets when you press a key on the keyboard is because stdin is 0, which would be within 0 - (3 - 1) range, which is what is checked. If you set the first parameter to socket_desc+1, then 0 - (socket_desc) range should be checked for ready sockets

How to wait for any socket to have data?

You can use select.select for your problem:

sockets = [sock1, sock2, sock3]
while sockets:
rlist, _, _ = select.select(sockets, [], [])
for sock in rlist:
do_stuff(sock)
sockets.remove(sock)

how to make client socket wait for data from server socket

The problem is because client is missing a empty line while reading LIST output from server. Because of this after LIST command, Server and Client becoming out of sync.

In client code add these two lines end of do while loop.
do {

if (datakb.equals("LIST"))
reader.readLine();
}while( ..)

It should have read the missed line and the problem should go away.

How can i wait for data until it get received in Node.js from a TCP server while GET request is sent by the user?

If I understood the question correctly, you want to send the HTTP response as soon as you've received a response from your downstream socket server.

First of all, the most important thing to consider: Socket connections -- like almost everything -- in Node.JS are asynchronous.

This has some very important consequences:

  1. The call to tcp_client.on('data', ...) will not block your program execution until you've received some data. It will simply register a callback function that will (may) be called at some later point in time and then continue your program.

    This means that if you register a callback in your TCP client and then send the HTTP reponse, the HTTP response will be sent first, and your callback will be called at some later point in time.

  2. There is no need to use a while loop for polling on your socket; simply use the tcp_client.on('data', ...) method once to register a callback function that is called as soon data is read from the socket.

    As tcp_client.on(...) is asynchronous, it will return immediately; if you do so in a loop, you'll basically spin endlessly and constantly register new event listeners. So, lose the while(close_connection_flag == false) loop!

  3. If you want to wait with your HTTP response until you've received data on your TCP socket, simply put your res.end(...) call inside the tcp_client callback.

All in all, I'd suggest something like this:

app.get('/', function(req, res) {

tcp_client.on('connect', function(){
console.log('[+] Connected.');
tcp_client.write('HelloServerSideFrom:Client-Server');
});

tcp_client.on('data', function(data) {
var flag_raised_by_server;

switch(data){

case 'HelloClientServerFrom:Server':
close_connection_flag = true;
break;
case 'data-success':
close_connection_flag = true;
flag_raised_by_server = 'ds';
break;
case 'data-failure':
close_connection_flag = true;
flag_raised_by_server = 'df';
break
default:
// do nothing.
break;

}

if (flag_raised_by_server) {
res.end('flag raised by server is: ' + flag_raised_by_server);
tcp_client.destroy();
}
});
});

Sockets: How to send data to the client without 'waiting' on them as they receive/parse it

If you set your socket to non-blocking, then writes should fail if they would otherwise block. You can then queue up the data however you like, and arrange for another attempt to be made later to write it. I don't know how to set socket options in the boost socket API, but that's what you're looking for.

But this is probably more trouble than it's worth. You'd need to select a socket that's ready for writing, presumably from several open simultaneously, shove more data into it until it's full, and repeat. I don't know if the boost sockets API has an equivalent of select, so that you can wait on multiple sockets at once until any of them is ready to write.

The reason that servers typically start a thread (or spawn a process) per client connection is precisely so that they can get on with serving other clients while they're waiting on I/O, while avoiding implementing their own queues. The simplest way to "arrange for another attempt later" is just to do blocking I/O in a dedicated thread.

What you can't do, unless boost has done something unusual in its sockets API, is require the OS or the sockets library to queue up arbitrary amounts of data for you without blocking. There may be an async API which will call you back when the data is written.

Python: fixed wait time for receiving socket data

The link with settimeout() was right. It raises a Exception when timeout.

Set a timeout on blocking socket operations. The value argument can be
a nonnegative floating point number expressing seconds, or None. If a
non-zero value is given, subsequent socket operations will raise a
timeout exception if the timeout period value has elapsed before the
operation has completed. If zero is given, the socket is put in
non-blocking mode. If None is given, the socket is put in blocking
mode.

You need to put your code in a try block, so that the Exception doesn't abort your program.

import socket.timeout as TimeoutException
# set timeout 5 second
clientsocket.settimeout(5)
for i in range(0,10):
sequence_number = i
start = time.time()
clientSocket.sendto("Ping " + str(i) + " " + str(start), server)
# Receive the client packet along with the address it is coming from
try:
message, address = clientSocket.recvfrom(1024)
except TimeoutException:
print("Timeout!!! Try again...")
continue
end = time.time()
if message != '':
print message
rtt = end - start
print "RTT = " + str(rtt)


Related Topics



Leave a reply



Submit