terminado
[MHWsCURYlr.git] / tcp / null / server.c
blobd8b8cf407507ef10794c6f534aeb13f2714e46c1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <string.h>
5 #include <sys/socket.h>
6 #include <arpa/inet.h>
7 #include <unistd.h>
8 #include <errno.h>
10 int main(int argc, char **argv)
12 int mySocket, clientSocket, clientLenght;
13 struct sockaddr_in server, client;
14 char byte = 'B';
16 if(argc < 2) {
17 printf("Usage: %s <port>\n", argv[0]);
18 exit(1);
21 mySocket = socket(AF_INET, SOCK_STREAM, 0);
22 assert(mySocket != -1);
24 memset(&server, 0, sizeof(server));
25 server.sin_family = AF_INET;
26 server.sin_addr.s_addr = htonl(INADDR_ANY);
27 server.sin_port = htons(atoi(argv[1]));
29 assert(bind(mySocket, (struct sockaddr *) &server, sizeof(server)) == 0);
30 assert(listen(mySocket, 1) == 0);
32 clientLenght = sizeof(client);
33 //for(i = 0; i < TESTS_TOTAL; i++) {
34 while(1) {
35 #ifdef DEBUG
36 printf("server: %i\n", i);
37 #endif
38 clientSocket = accept(mySocket, (struct sockaddr *) &client, (unsigned int *) &clientLenght);
39 //printf("errno: %i\n", errno);
40 assert(clientSocket != -1);
42 assert(recv(clientSocket, &byte, 1, 0) == 1);
43 assert(send(clientSocket, &byte, 1, 0) == 1);
44 //assert(recv(clientSocket, NULL, 0, 0) == 0);
45 //assert(send(clientSocket, NULL, 0, 0) == 0);
47 close(clientSocket);
50 //close(clientSocket);
51 close(mySocket);
53 return 0;