ipa not sfd
[progcomp2012.git] / judge / manager / network_controller.cpp
1 #include "network_controller.h"
2
3 NetworkController::NetworkController(const Piece::Colour & newColour, const char * newName) : Controller(newColour, newName), sfd(-1)
4 {
5         struct protoent * tcp = getprotobyname("tcp");
6  
7         sfd = socket(PF_INET, SOCK_STREAM, tcp->p_proto);
8         if (sfd < 0)
9         {
10         //      fprintf(stderr, "NetworkController::NetworkController - couldn't create a TCP socket!");
11                 perror("NetworkController::NetworkController - creating TCP socket... ");
12                 return;
13         }
14 }
15
16 NetworkController::~NetworkController()
17 {
18         if (Valid())
19         {
20                 if (shutdown(sfd, SHUT_RDWR) == -1)
21                 {
22                 //      fprintf(stderr, "NetworkController::~NetworkController - Can't shutdown socket %d!", sfd);
23                         perror("NetworkController::~NetworkController - shutting down socket... ");
24                         close(sfd);
25                         sfd = -1;
26                 }
27         }
28         close(sfd);
29 }
30
31 Server::Server(const Piece::Colour & newColour, const char * newName) : NetworkController(newColour, newName)
32 {
33         struct sockaddr_in ipa;
34         ipa.sin_family = AF_INET;
35         ipa.sin_port = htons(NetworkController::port);  
36         ipa.sin_addr.s_addr = INADDR_ANY;
37         memset(&ipa,0, sizeof ipa);
38
39         if (bind(sfd, (struct sockaddr*)&ipa, sizeof (ipa)) == -1) //dERP DERP DERP
40         {
41         //      fprintf(stderr, "Server::Server - Couldn't bind to socket! Abort\n");
42                 perror("Server::Server - binding to socket... ");
43                 close(sfd);
44                 sfd = -1;
45                 return;
46         }
47
48         //Listen for at most 1 connection
49         if (listen(sfd, 1) == -1)
50         {
51                 fprintf(stderr, "Server::Server - listening failed.\n");
52                 close(sfd);
53                 sfd = -1;
54                 return;
55         }       
56
57         //Accept the connection
58         sfd = accept(sfd, NULL, NULL);
59         if (sfd < 0)
60         {
61                 fprintf(stderr, "Server::Server - couldn't accept connection.\n");
62                 close(sfd);
63                 sfd = -1;
64                 return;
65         }
66
67         //We are now ready to play!
68 }
69
70
71 Client::Client(const Piece::Colour & newColour, const char * newName, const char * server) : NetworkController(newColour, newName)
72 {
73         struct sockaddr_in ipa;
74         ipa.sin_family = AF_INET;
75         ipa.sin_port = htons(NetworkController::port);  
76         int Res = inet_pton(AF_INET, server, &ipa.sin_addr);
77
78         if (Res < 0)
79         {
80                 fprintf(stderr, "Client::Client - First parameter is not a valid address family!\n");
81                 close(sfd);
82                 sfd = -1;
83                 return;
84         }
85         else if (Res == 0)
86         {
87                 fprintf(stderr, "Client::Client - Second parameter does not contain a valid IP Address!\n");
88                 close(sfd);
89                 sfd = -1;
90                 return;
91         }
92         
93         if (connect(sfd, (struct sockaddr*)&sfd, sizeof sfd) == -1)
94         {
95                 fprintf(stderr, "Client::Client - Connection to server at \"%s\" failed.\n", server);
96                 close(sfd);
97                 sfd = -1;
98                 return;
99         }
100
101         //We are now ready to play!
102 }
103 //EOF
104
105

UCC git Repository :: git.ucc.asn.au