/* The port number is passed as an argument */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } // create a socket // socket(int domain, int type, int protocol) sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); // clear address structure bzero((char *) &serv;_addr, sizeof(serv_addr)); portno = atoi(argv[1]); /* setup the host_addr structure for use in bind call */ // server byte order serv_addr.sin_family = AF_INET; // automatically be filled with current host's IP address serv_addr.sin_addr.s_addr = INADDR_ANY; // convert short integer value for port must be converted into network byte order serv_addr.sin_port = htons(portno); // bind(int fd, struct sockaddr *local_addr, socklen_t addr_length) // bind() passes file descriptor, the address structure, // and the length of the address structure // This bind() call will bind the socket to the current IP address on port, portno if (bind(sockfd, (struct sockaddr *) &serv;_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); // This listen() call tells the socket to listen to the incoming connections. // The listen() function places all incoming connection into a backlog queue // until accept() call accepts the connection. // Here, we set the maximum size for the backlog queue to 5. listen(sockfd,5); // The accept() call actually accepts an incoming connection clilen = sizeof(cli_addr); // This accept() function will write the connecting client's address info // into the the address structure and the size of that structure is clilen. // The accept() returns a new socket file descriptor for the accepted connection. // So, the original socket file descriptor can continue to be used // for accepting new connections while the new socker file descriptor is used for // communicating with the connected client. newsockfd = accept(sockfd, (struct sockaddr *) &cli;_addr, &clilen;); if (newsockfd < 0) error("ERROR on accept"); printf("server: got connection from %s port %d\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port)); // This send() function sends the 13 bytes of the string to the new socket send(newsockfd, "Hello, world!\n", 13, 0); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); close(newsockfd); close(sockfd); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}