#include <network.h>
Public Methods | |
Network () | |
The Constructor. More... | |
~Network () | |
The Deconstructor. More... | |
int | waitForConnection () |
Waits for Connections. More... | |
Private Attributes | |
int | sock |
The own server socket. More... | |
struct sockaddr_in | address |
The address. More... |
Definition at line 17 of file network.h.
00017 { 00018 public: 00020 Network (); 00022 ~Network (); 00023 00025 00028 int waitForConnection (); 00029 00030 private: 00031 int sock; 00032 struct sockaddr_in address; 00033 }
|
The Constructor.
Definition at line 34 of file network.cpp.
00034 { 00035 extern FrontEnd* frontEnd; 00036 00037 string msg; 00038 00039 frontEnd->displayDebugMessage ("Opening TCP/IP socket"); 00040 00041 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0) { 00042 frontEnd->displayDebugMessage ("Error: Can't open TCP/IP socket !"); 00043 exit (1); 00044 }; 00045 00046 int i = 1; 00047 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i)); 00048 00049 address.sin_family = AF_INET; 00050 address.sin_port = htons (DEFAULT_PORT); 00051 00052 memset (&address.sin_addr, 0, sizeof (address.sin_addr)); 00053 00054 msg = "Binding TCP/IP socket to port "; 00055 msg += intToChar (DEFAULT_PORT); 00056 frontEnd->displayDebugMessage (msg.c_str ()); 00057 00058 if (bind (sock, (struct sockaddr*) &address, sizeof (address))) { 00059 frontEnd->displayDebugMessage ("Error: Can't bind TCP/IP socket !"); 00060 exit (1); 00061 }; 00062 00063 frontEnd->displayDebugMessage ("Listening for connections"); 00064 00065 if (listen (sock, 5)) { 00066 frontEnd->displayDebugMessage ("Error: Can't listen to socket !"); 00067 exit (1); 00068 }; 00069 }
|
|
Waits for Connections.
Definition at line 96 of file network.cpp.
00096 { 00097 extern FrontEnd* frontEnd; 00098 00099 fd_set checkIt; 00100 int clientSock = 0; 00101 00102 FD_ZERO (&checkIt); 00103 FD_SET (sock, &checkIt); 00104 00105 if (select (70, &checkIt, 0, 0, NULL) < 0) { 00106 frontEnd->displayDebugMessage ("Error in Network::waitForConnection: select !"); 00107 exit (1); 00108 }; 00109 00110 if (FD_ISSET (sock, &checkIt)) { 00111 if ((clientSock = accept (sock, (struct sockaddr*) &address, (socklen_t*) sizeof (address))) < 0) { 00112 frontEnd->displayDebugMessage ("Error in Network::waitForConnection: accept !"); 00113 exit (1); 00114 }; 00115 } else { 00116 frontEnd->displayDebugMessage ("Error in Network::waitForConnection: FD_ISSET !"); 00117 exit (1); 00118 }; 00119 00120 return clientSock; 00121 }
|
|