root/juggler/branches/1.0/Environment/vjSocket.cpp

Revision 11237, 6.1 kB (checked in by patrickh, 6 years ago)

Deal with IRIX 6.5.17 where socklen_t is an unsigned integer but
accept(2) wants a signed int.

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