Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #define PORT 3452
- #define BUFLEN 1024
- void main()
- {
- int i;
- int rc;
- int s;
- int cs;
- char buf[BUFLEN+1];
- struct sockaddr_in sa;
- struct sockaddr_in csa;
- int size_csa;
- fd_set rfd;
- fd_set c_rfd;
- int dsize;
- memset(&sa, 0, sizeof(sa));
- sa.sin_family = AF_INET;
- sa.sin_port = htons(PORT);
- sa.sin_addr.s_addr = INADDR_ANY;
- s = socket(AF_INET, SOCK_STREAM, 0);
- if (s < 0) {
- perror("socket: allocation failed");
- exit(EXIT_FAILURE);
- }
- int option = 1;
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0)
- {
- perror("Setsockopt");
- exit(EXIT_FAILURE);
- }
- rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));
- if (rc) {
- perror("bind");
- exit(EXIT_FAILURE);
- }
- rc = listen(s, 5);
- if (rc) {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- size_csa = sizeof(csa);
- dsize = getdtablesize();
- FD_ZERO(&rfd);
- FD_SET(s, &rfd);
- while (1){
- c_rfd = rfd;
- rc = select(dsize, &c_rfd, NULL, NULL, (struct timeval *)NULL);
- if (FD_ISSET(s, &c_rfd)) {
- cs = accept(s, (struct sockaddr *)&csa, &size_csa);
- if (cs < 0){
- continue;
- }
- FD_SET(cs, &rfd);
- continue;
- }
- for (i = 0; i < dsize; i++){
- if (i != s && FD_ISSET(i, &c_rfd)) {
- rc = read(i, buf, BUFLEN);
- printf("%s", buf);
- if (rc == 0) {
- close(i);
- FD_CLR(i, &rfd);
- }
- else {
- printf("%s", buf);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement