nonblocking

Enable non-blocking socket

三世轮回 提交于 2019-12-01 13:27:24
问题 I have a server written in C/C++. I set the wrapper for the connection as follow: //START WRAPPER void Server::init_address(int port) { memset(&(this->serv_addr), 0, sizeof(this->serv_addr)); this->serv_addr.sin_family = AF_INET; this->serv_addr.sin_port = htons(port); this->serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); } int Server::w_socket() { int retv; retv = socket(PF_INET, SOCK_STREAM, 0); //FIXME //fcntl(retv, F_SETFL, O_NONBLOCK); if(retv == -1) { std::string err_msg(strerror(errno))

Advice on implementing “presence” for a web site?

我的未来我决定 提交于 2019-12-01 11:09:45
Ideally, I'd like to find simple, lightweight code that allows all the web clients connected to my site to maintain real-time status of who else is currently online. I know ejabberd does this, but it also does a lot of other things, and I'd prefer a small code footprint so I can customize and understand its performance characteristics. I like the non-blocking aspect of node.js, and was wondering if there's an open source project that does all this logic. I'd also like to see a JavaScript implementation of maintaining this model on the client side. For real time status, use socket.io . Every

Advice on implementing “presence” for a web site?

女生的网名这么多〃 提交于 2019-12-01 08:33:27
问题 Ideally, I'd like to find simple, lightweight code that allows all the web clients connected to my site to maintain real-time status of who else is currently online. I know ejabberd does this, but it also does a lot of other things, and I'd prefer a small code footprint so I can customize and understand its performance characteristics. I like the non-blocking aspect of node.js, and was wondering if there's an open source project that does all this logic. I'd also like to see a JavaScript

BSD Sockets - How to use non-blocking sockets?

…衆ロ難τιáo~ 提交于 2019-12-01 07:13:52
问题 I am trying to use non-blocking TCP sockets. The problem is that they are still blocking. The code is below - server code - struct sockaddr name; char buf[80]; void set_nonblock(int socket) { int flags; flags = fcntl(socket,F_GETFL,0); assert(flags != -1); fcntl(socket, F_SETFL, flags | O_NONBLOCK); } int main(int agrc, char** argv) { int sock, new_sd, adrlen; //sock is this socket, new_sd is connection socket name.sa_family = AF_UNIX; strcpy(name.sa_data, "127.0.0.1"); adrlen = strlen(name

How to make a delayed non-blocking function call

别来无恙 提交于 2019-12-01 05:50:12
I want to call the add function of an HashSet with some delay, but without blocking the current thread. Is there an easy solution to achieve something like this: Utils.sleep(1000, myHashSet.add(foo)); //added after 1 second //code here runs immediately without delay ... You can use ScheduledThreadPoolExecutor.schedule : ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); exec.schedule(new Runnable() { public void run() { myHashSet.add(foo); } }, 1, TimeUnit.SECONDS); It will execute your code after 1 second on a separate thread. Be careful about concurrent modifications of

How to make a delayed non-blocking function call

心已入冬 提交于 2019-12-01 03:18:53
问题 I want to call the add function of an HashSet with some delay, but without blocking the current thread. Is there an easy solution to achieve something like this: Utils.sleep(1000, myHashSet.add(foo)); //added after 1 second //code here runs immediately without delay ... 回答1: You can use ScheduledThreadPoolExecutor.schedule: ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1); exec.schedule(new Runnable() { public void run() { myHashSet.add(foo); } }, 1, TimeUnit.SECONDS); It

Node.js - single thread, non-blocking?

我怕爱的太早我们不能终老 提交于 2019-12-01 03:04:16
I am learning Node.js and I have read that Node.js is single threaded and non-blocking. I have a good background in JavaScript and I do understand the callbacks, but what I don't really understand is how Node.js can be single threaded and run code in the background. Isn't that contradictory? Because if Node.js is single threaded it can still only perform one task at the time. So if it runs something in the background it has to stop the current task to process something in the background, right? How does that work practically? What "in the background" really means in terms of NodeJS is that

C select() timeout STDIN single char (no ENTER)

假如想象 提交于 2019-11-30 22:52:19
I want to be able to use select() to work with entering a single char (no ENTER) from STDIN. So, when a user press a single key, select() should return immediately, not waiting for the user to hit ENTER. int main(void) { fd_set rfds; struct timeval tv; int retval; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(0, &rfds); /* Wait up to 2 seconds. */ tv.tv_sec = 2; tv.tv_usec = 0; retval = select(1, &rfds, NULL, NULL, &tv); if (retval == -1) perror("select()"); else if (retval) printf("Data is available now.\n"); else printf("No data within five seconds.\n"); exit(EXIT

Nested requests are blocking

瘦欲@ 提交于 2019-11-30 22:48:20
I am relatively new to nodejs. I've been recently pooling all of the collective knowledge i've gathered through the past couple of months into an project. I believe I've ran into my first "blocking" issue in nodejs. I have a page that loads two request() calls they are async and nested accordingly. The innermost one uses data from the innermost to redirect the user. request(parameters,function(error, response, data){ //the first request passes a token request(newParamters,function(error, response, data){ //the second request passes a url res.redirect(data.info.url); }); }); The error is that

Node.js - single thread, non-blocking?

怎甘沉沦 提交于 2019-11-30 22:19:25
问题 I am learning Node.js and I have read that Node.js is single threaded and non-blocking. I have a good background in JavaScript and I do understand the callbacks, but what I don't really understand is how Node.js can be single threaded and run code in the background. Isn't that contradictory? Because if Node.js is single threaded it can still only perform one task at the time. So if it runs something in the background it has to stop the current task to process something in the background,