Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

network.cpp

Go to the documentation of this file.
00001 /********************************************************************
00002 Description: Class for network functions
00003 part of the 3Dsia project
00004 created: StonedBones, 31.1.2000
00005 
00006 History:
00007     date,   name,       changes,                    in funtion
00008     180300  StonedBones cleaned source
00009     310100  StonedBones created source
00010 
00011 ********************************************************************/
00012 
00013 #include "network.h"
00014 
00015 #include <string>
00016 #include <unistd.h>
00017 
00018 #include "../common/options.h"
00019 #include "../common/intToChar.h"
00020 #include "frontEnd.h"
00021 
00022 /*...................................................................
00023 Description: Constructor
00024 Args: <none>
00025 Returns: <none>
00026 Created: StonedBones, 31.1.2000
00027 [ToDo:]
00028 [Comments:]
00029 - based on old 3Dsia code by Xandi
00030 Changes:
00031 - 18.03.2000 StonedBones replaced char* -> string
00032 -------------------------------------------------------------------*/
00033 
00034 Network::Network () {
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 };
00070     
00071 /*...................................................................
00072 Description: Destructor
00073 Args: <none>
00074 Returns: <none>
00075 Created: StonedBones, 31.1.2000
00076 [ToDo:]
00077 - close all open connections
00078 [Comments:]
00079 Changes:
00080 -------------------------------------------------------------------*/
00081 
00082 Network::~Network () {
00083 };
00084         
00085 /*...................................................................
00086 Description: waits until a client connects
00087 Args: <none>
00088 Returns: socket number of new client
00089 Created: StonedBones, 31.1.2000
00090 [ToDo:]
00091 [Comments:]
00092 - based on old 3Dsia code by Xandi
00093 Changes:
00094 -------------------------------------------------------------------*/
00095 
00096 int Network::waitForConnection () {
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 };

Generated at Sat May 13 13:50:20 2000 for 3Dsia by doxygen 1.1.2 written by Dimitri van Heesch, © 1997-2000