Created
October 12, 2013 14:40
-
-
Save unikoid/6950684 to your computer and use it in GitHub Desktop.
Problems with this code, ab2 and sigint.
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 <sys/socket.h> | |
#include <sys/types.h> | |
#include <sys/select.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <netinet/in.h> | |
#include <signal.h> | |
const char msg[] = "lol\n"; | |
int sockfd; | |
void sigint(int signal) | |
{ | |
close(sockfd); | |
exit(0); | |
} | |
void process(int socket) | |
{ | |
size_t len = 1024; | |
ssize_t recv_len; | |
char buffer[len]; | |
printf("recv\n"); | |
if( (recv_len = recv(socket, buffer, len, 0)) < 0) | |
{ | |
perror("recv"); | |
close(socket); | |
return; | |
} | |
printf("write\n"); | |
write(socket, msg, sizeof(msg)-1); | |
printf("written\n"); | |
close(socket); | |
} | |
int main(int argc, char** argv) | |
{ | |
signal(SIGINT, sigint); | |
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
{ | |
perror("socket"); | |
}; | |
struct sockaddr_in addr; | |
memset(&addr, 0, sizeof(addr)); | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(12345); | |
if(inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr)<=0) | |
{ | |
perror("inet_pton"); | |
} | |
if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) | |
{ | |
perror("bind"); | |
} | |
if(listen(sockfd, 0) < 0) | |
{ | |
perror("listen"); | |
} | |
printf("%d\n", sockfd); | |
for(;;) | |
{ | |
printf("Accepting\n"); | |
int socket = accept(sockfd, NULL, 0); | |
printf("Accepted\n"); | |
process(socket); | |
printf("Processed\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment