root/juggler/tags/1.0.5/Environment/vjSocket.cpp

Revision 7539, 6.0 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33 #include <vjConfig.h>
34
35 #include <Environment/vjSocket.h>
36
37 #ifndef VJ_OS_Win32
38
39 /***************************** BSD Sockets Version **************************/
40
41 //#include <Environment/sockstream.h>
42 #include <strings.h>    // For bzero()
43 #include <sys/types.h>
44 #include <netdb.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47
48 #include <Environment/vjSockStream.h>
49
50 const int vjSOCKID_INVALID = -1;
51
52
53 vjSocketPosix::vjSocketPosix() {
54     sockid = -1;
55     in = 0;
56     out = 0;
57 }
58
59
60
61 vjSocketPosix::vjSocketPosix(vjSocketIDPosix id) {
62     sockid = id;
63     out = new std::ostream (new sockstreambuf (id));
64     in = new std::istream (new sockstreambuf(id));
65 }
66
67
68
69 vjSocketPosix::~vjSocketPosix () {
70     close();
71 }
72
73
74
75 //  std::string vjSocketPosix::getName () {
76 //      std::string foo = sockid;
77 //      return foo;
78 //  }
79
80
81
82 void vjSocketPosix::close() {
83     if (in) {
84         delete in;
85         in = 0;
86     }
87     if (out) {
88         delete out;
89         out = 0;
90     }
91     if (sockid != -1) {
92         ::close (sockid);
93         sockid = -1;
94     }
95 }
96
97
98
99 bool vjSocketPosix::listen (int port) {
100     /* here, we open a socket & get ready to read connections */
101     struct sockaddr_in sockaddress;
102     bzero(&sockaddress, sizeof (struct sockaddr_in));
103     sockaddress.sin_family = PF_INET;
104     sockaddress.sin_port = htons(port);
105
106     sockid = socket (AF_INET, SOCK_STREAM, 0);
107     if (sockid == -1)
108         return false;
109
110     int err = bind ( sockid, (sockaddr*)&sockaddress,
111                      sizeof (struct sockaddr_in));
112     if (!err)
113         err = ::listen (sockid, 0);
114    
115     if (err) {
116         close();
117         return false;
118     }
119     else
120         return true;
121 }
122
123
124
125 // must be called after listen
126 vjSocketPosix* vjSocketPosix::accept () {
127     sockaddr_in servaddr;
128     int servsock;
129
130 #ifdef VJ_OS_HPUX
131     int len;
132 #else
133     socklen_t len;
134 #endif
135
136     len = sizeof (struct sockaddr_in);
137     servsock = ::accept (sockid, (sockaddr*)&servaddr, &len);
138
139     if (servsock != -1)
140         return new vjSocketPosix (servsock);
141     else
142         return NULL;
143 }
144
145
146
147 #else
148
149
150 /****************************** Winsock2 Version ***************************/
151
152 #include <winsock2.h>
153 #include <Environment/vjSockStream.h>
154
155
156 const int vjSOCKID_INVALID = -1;
157
158
159 vjSocketWin32::vjSocketWin32() {
160     sockid = -1;
161     in = 0;
162     out = 0;
163 }
164
165
166
167 vjSocketWin32::vjSocketWin32(vjSocketIDWin32& id) {
168     sockid = id;
169 //     sockstreambuf* buf = new sockstreambuf (id, new char[512], 512);
170 //     out = new ofstream (buf);
171 //     in = new ifstream (buf);
172     out = new std::ostream (new sockstreambuf (id));
173     in = new std::istream (new sockstreambuf(id));
174 }
175
176
177
178 vjSocketWin32::~vjSocketWin32 () {
179     close();
180 }
181
182
183 void vjSocketWin32::close() {
184     if (in) {
185         delete in;
186         in = 0;
187     }
188     if (out) {
189         delete out;
190         out = 0;
191     }
192     if (sockid != -1) {
193         ::closesocket (sockid);
194         sockid = -1;
195     }
196 }
197
198
199
200 bool vjSocketWin32::listen (int port) {
201     /* here, we open a socket & get ready to read connections */
202     WSADATA wsaData;
203     unsigned short wVersionRequested = (0x1<<8) | 0x1;
204     int err;
205
206     err = WSAStartup( wVersionRequested, &wsaData );
207     if(err < 0) {
208         std::cout << "-------- socket - WSAstartup failed-------------"
209                   << std::endl;
210         //return false;
211         // output the error.
212         //std::string error;
213         //socketUtil::getLastError( error );
214         //std::cout<<"socketUtil: "<<error<<"\n"<<std::flush;
215     }
216
217     u_long nInterfaceAddr = inet_addr("0.0.0.0");
218     struct sockaddr_in sockaddress;
219 //      for (int j = 0; j < sizeof (struct sockaddr_in); j++)
220 //          *((char*)&sockaddress) = 0;
221     sockaddress.sin_addr.s_addr = nInterfaceAddr;
222     sockaddress.sin_family = PF_INET;
223     sockaddress.sin_port = htons(port);
224
225     sockid = socket (AF_INET, SOCK_STREAM, 0);
226     if (sockid == -1) {
227         std::cout << "---------- socket - sockID bad----------" << std::endl;
228         return false;
229     }
230
231     err = bind ( sockid, (sockaddr*)&sockaddress,
232                  sizeof (struct sockaddr_in));
233     if (err)
234         std::cout << "--------- socket - bind error " << err << " --------"
235                   << std::endl;
236     if (!err)
237         err = ::listen (sockid, 0);
238
239     if (err) {
240         std::cout << "--------------- socket - listen fail ------------"
241                   << std::endl;
242         close();
243         return false;
244     }
245     else
246         return true;
247 }
248
249
250
251 // must be called after listen
252 vjSocketWin32* vjSocketWin32::accept () {
253     sockaddr_in servaddr;
254     unsigned int servsock;
255     int len = sizeof (struct sockaddr_in);
256     servsock = ::accept (sockid,
257                          (sockaddr*)&servaddr, &len);
258     if (servsock != -1)
259         return new vjSocketWin32 (servsock);
260     else
261         return NULL;
262 }
263
264
265
266 #endif //ifndef VJ_OS_Win32
267
Note: See TracBrowser for help on using the browser.