Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<sys/wait.h>
- #include<sys/types.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<netdb.h>
- #include<string.h>
- //#include<pthread.h>
- void childend(int signo) {
- wait(NULL);
- printf("omg!");
- }
- struct dorsz {
- int cfd;
- struct sockaddr_in caddr;
- };
- void *worker(void* arg){
- struct dorsz* c = (struct dorsz*)arg;
- printf("---> \033[90m %s\033[m:%d\n", inet_ntoa(c->caddr.sin_addr),
- c->caddr.sin_port);
- write(c->cfd, "\033[92mhello client\n", 30);
- close(c->cfd);
- free(c);
- return 0;
- }
- int main(int argc, char** argv) {
- int fd, cfd, on = 1;
- socklen_t l;
- fd = socket(PF_INET, SOCK_STREAM, 0);
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
- struct sockaddr_in saddr, caddr;
- signal(SIGCHLD, childend);
- saddr.sin_family = PF_INET;
- saddr.sin_port = htons(atoi(argv[1]));
- saddr.sin_addr.s_addr = INADDR_ANY;
- bind(fd, (struct sockaddr*)&saddr, sizeof(saddr));
- listen(fd, 10);
- while(1) {
- l = sizeof(caddr);
- struct dorsz* c = malloc(sizeof(struct dorsz));
- c->cfd = accept(fd, (struct sockaddr*)&caddr, &l);
- pthread_t thread_id;
- pthread_create(&thread_id, NULL, worker, c);
- pthread_detach(thread_id);
- printf("lecimy dalej");
- /*
- if (fork() != 0) {
- printf("SERVER");
- close(cfd);
- } else {
- close(fd);
- //for(int i = 0; i < 100; i++)
- // write(cfd, "\a", 10);
- close(cfd);
- exit(0);
- return 0;
- }*/
- }
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement