Permalink
Cannot retrieve contributors at this time
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?
CSE4402-TCP-Attacks-Lab/tcp_server_improved.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (36 sloc)
1.12 KB
This file contains 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 <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 |