Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
int main(){
int sockfd, newsockfd;
struct sockaddr_in my_addr, client_addr;
char buffer[100];
//Step 1: Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Step 2: Bind to a port number
memset(&my_addr, 0, sizeof(struct sockaddr_in));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(9090);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_in));
//Step 3: Listen for conections
//the goal of task 1 is to flood this listen with a bunch of SYN requests unitl it cannot take any more
listen(sockfd, 5);
socklen_t client_len = sizeof(client_addr);
while(1){
newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len);
if (fork() == 0) { //Child process. Remember 3100?
close(sockfd);
//Read Data
memset(buffer, 0, sizeof(buffer));
int len = read(newsockfd, buffer, 100);
printf("Recieved %d bytes.\n%s\n", len, buffer);
close(newsockfd);
return 0;
} else { //Parent process
close(newsockfd);
}//ENDELSE
}//ENDWHILE
}//ENDMAIN