SocketCom.cpp
Go to the documentation of this file.
00001 
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include "canon_vbc50i/libSock/SocketCom.h"
00013 
00014 //#define TRACE
00015 
00016 SocketCom::SocketCom(const char * _host, int _port) :
00017         Socket()
00018 {
00019         server = false;
00020         host = strdup(_host);
00021         port = _port;
00022 }
00023         
00024 SocketCom::SocketCom(int _port) :
00025         Socket()
00026 {
00027         server = true;
00028         host = NULL;
00029         port = _port;
00030 }
00031         
00032 SocketCom::~SocketCom()
00033 {
00034         if (!server) free(host);
00035 #ifdef TRACE
00036         printf("SocketCom : Destruction\n");
00037 #endif
00038 }
00039 
00040 bool SocketCom::PrepareServer()
00041 {
00042         if (!server) return false;
00043         if (accepting) return true;
00044 #ifdef TRACE
00045         printf("Opening server\n");
00046 #endif
00047         if (!Create()) return false;
00048 #ifdef TRACE
00049         printf("Create OK\n");
00050 #endif
00051         if (!Bind(port)) return false;
00052 #ifdef TRACE
00053         printf("Binding OK\n");
00054 #endif
00055         if (!Listen()) return false;
00056 #ifdef TRACE
00057         printf("Listening OK\n");
00058 #endif
00059         return true;
00060 }
00061 bool SocketCom::Open(Socket * newsocket)
00062 {
00063         if ((!server)||(!accepting)) return false;
00064         if (!Accept(newsocket)) return false;
00065 #ifdef TRACE
00066         printf("Accept OK\n");
00067 #endif
00068         return true;
00069 }
00070 
00071 bool SocketCom::Open()
00072 {
00073         if (server)
00074         {
00075 #ifdef TRACE
00076                 printf("Opening server\n");
00077 #endif
00078                 Socket listener;
00079                 if (!listener.Create()) return false;
00080 #ifdef TRACE
00081                 printf("Create OK\n");
00082 #endif
00083                 if (!listener.Bind(port)) return false;
00084 #ifdef TRACE
00085                 printf("Binding OK\n");
00086 #endif
00087                 if (!listener.Listen()) return false;
00088 #ifdef TRACE
00089                 printf("Listening OK\n");
00090 #endif
00091                 if (!listener.Accept(*this)) return false;
00092 #ifdef TRACE
00093                 printf("Accept OK\n");
00094 #endif
00095         }
00096         else
00097         {
00098 #ifdef TRACE
00099                 printf("Opening client %s:%d\n",host,port);
00100 #endif
00101                 if (!Create()) return false;
00102 #ifdef TRACE
00103         printf("Create OK\n");
00104 #endif
00105                 if (!Connect(host,port)) return false;
00106 #ifdef TRACE
00107                 printf("Connect OK\n");
00108 #endif
00109                 SetNonBlocking(true);
00110         }
00111         return true;
00112 }
00113 


canon_vbc50i
Author(s): Cedric Pradalier
autogenerated on Sun Oct 5 2014 23:47:45