Főoldal | Névtérlista | Osztályhierarchia | Betűrendes lista | Adatszerkezetek | Könyvtárak | Fájllista | Névtértagok | Adatmezők | Globális elemek

main.cpp

Ugrás a fájl dokumentációjához.
00001 /*
00002     mySocket
00003     Copyright (C) 2006  Kornel Csernai <csko@csko.hu>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 
00020     This is a socket connect example. The first ConnectSocket(test) show how to
00021     connect by giving a sockaddr_in as an argument. The second one shows how to
00022     connect by giving a port and an address. After connecting, the SocketHandler
00023     class writes to and reads from the sockets. The MyServer classes do almost
00024     the same, but what they do is that they listen on a port instead of
00025     connecting. The __On*() functions are called every time an event occures
00026     (defined in other/* files).
00027 
00028 */
00029 #include <main.h>
00030 #include <StringLib.h>
00031 #include <RealSocket.h>
00032 
00033 using namespace std;
00034 using namespace StringLib;
00035 
00036 int main(){
00037 
00038   try {
00039 
00040     SocketHandler::newInstance();
00041 
00042     MyConnectSocket* test = new MyConnectSocket();
00043     MyConnectSocket* test2 = new MyConnectSocket();
00044     MyConnectSocket* test3 = new MyConnectSocket();
00045 
00046 #ifdef HAVE_SSL
00047 
00048     test -> SetSSL(true);
00049     test -> Connect("gmail.com", 443);
00050     test << (string) "GET / HTTP/1.0\n\n";
00051 
00052 #endif
00053 
00054 
00055     test2 -> Connect("www.ubuntu.com", 80);
00056     test2 << (string) "GET / HTTP/1.0\n\n";
00057 
00058     sockaddr_in saddr;
00059 #ifndef _WIN32 // still problems :\
00060 
00061     memset(&saddr, 0, sizeof(saddr));
00062     saddr.sin_family = AF_INET;
00063     saddr.sin_port = htons(80);
00064     saddr.sin_addr.s_addr = inet_addr("66.35.250.203"); // sourceforge.net
00065 
00066     test3 -> Connect(saddr, TYPE_TCP, false);
00067     test3 -> Write((string) "GET / HTTP/1.0\r\n\r\n");
00068 
00069 #endif
00070 
00071     MyServer* test4 = new MyServer();
00072     MyServer* test5 = new MyServer();
00073 
00074     test4 -> Start(8013, "127.0.0.1");
00075 
00076     memset(&saddr, 0, sizeof(saddr));
00077     saddr.sin_family = AF_INET;
00078     saddr.sin_port = htons(8014);
00079     saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
00080     test5 -> Start((struct sockaddr_in) saddr, TYPE_TCP);
00081 
00082     SocketHandler* handler = SocketHandler::getInstance();
00083     if(!handler) return 0;
00084 
00085     while(handler and handler -> HaveSockets()){
00086 
00087       try {
00088 
00089         handler -> Handle();
00090 
00091         }
00092 
00093       catch(SocketException a){
00094         
00095         out("Error: " + a());
00096     
00097         }
00098     
00099       }
00100 
00101     }
00102 
00103   catch(SocketException a){
00104   
00105     out("Error: " + a());
00106   
00107     }
00108 
00109   return 0;
00110 
00111   }

SourceForge.netLogo