Last active
November 28, 2019 17:30
-
-
Save roy4801/95cf5ae2ded83a2af259048e621a59c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <cstring> | |
| #include <unistd.h> | |
| #include <sys/socket.h> | |
| #include <netinet/in.h> | |
| #include <arpa/inet.h> | |
| char buffer[1024]; | |
| const char *hello = "Hello from server"; | |
| #define PORT 8080 | |
| int main(int argc, char const *argv[]) | |
| { | |
| int server_fd, new_socket, valread; | |
| struct sockaddr_in address; | |
| int addrlen = sizeof(address); | |
| // Creating socket file descriptor | |
| if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) | |
| { | |
| perror("socket failed"); | |
| exit(EXIT_FAILURE); | |
| } | |
| // Set address | |
| address.sin_family = AF_INET; | |
| if(inet_pton(AF_INET, "127.0.0.1", &address.sin_addr) <= 0) | |
| { | |
| perror("Failed to convert address"); | |
| exit(EXIT_FAILURE); | |
| } | |
| // address.sin_addr.s_addr = INADDR_ANY; // 0.0.0.0 | |
| address.sin_port = htons( PORT ); | |
| // Forcefully attaching socket to the port 8080 | |
| if (bind(server_fd, (struct sockaddr *)&address, | |
| sizeof(address))<0) | |
| { | |
| perror("bind failed"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (listen(server_fd, 3) < 0) | |
| { | |
| perror("listen"); | |
| exit(EXIT_FAILURE); | |
| } | |
| if ((new_socket = accept(server_fd, (struct sockaddr *)&address, | |
| (socklen_t*)&addrlen))<0) | |
| { | |
| perror("accept"); | |
| exit(EXIT_FAILURE); | |
| } | |
| while(true) | |
| { | |
| valread = read( new_socket , buffer, 1024); | |
| printf("%s\n", buffer); | |
| send(new_socket, hello, strlen(hello), 0); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment