Reputation: 1
Below is the server program(C) using socket functionality provided by libC
# include <unistd.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <string.h>
#include <netinet/in.h>
main(){
int listfd,connfd,retval;
//pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listfd = socket(AF_INET, SOCK_STREAM, 0);
printf("listfd = %d ", listfd);
if (listfd < 0){
perror("sock:");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8004);
retval = bind(listfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if(retval < 0){
perror("bind:");
exit(2);
}
listen(listfd, 5);
while(1){
clilen = sizeof(cliaddr);
connfd = accept(listfd, (struct sockaddr *) &cliaddr, &clilen);
printf(" connfd= %d",connfd);
printf(" client connected \n");
printf(" client's port no = %d\n",htons(cliaddr.sin_port));
}
}
Is Node JS a similar socket library that helps JS users to communicate over sockets?
Note: I would say, node
is a framework but not library, because JS code is consumed by node
unlike above C program invoking socket library functions
Upvotes: 0
Views: 1222
Reputation: 707158
node.js is a Javascript runtime (capable of running Javascript programs) that includes many library functions. One such library function it includes is a socket library for making socket connections and communicating over those sockets. That library in node.js is referred to as the "net" library which you can see documented here.
This is just one thing that node.js does and only one type of feature that it offers. A node.js program does not have to use the net library if it does not need to. Other libraries such as the http library are built on top of the net library to offer higher level networking functionality such as http servers and http requests.
node.js is conceptually similar to a Python implementation in the scope of what it covers. Both include a language interpreter, a bunch of built-in libraries for core functions (networking, file I/O, etc...), the ability to run programs written in the supported language and the ability to include other libraries written for the environment or to create your own libraries.
Upvotes: 2