3 ** Copyright (C) 2008 Cockos Inc
4 ** Copyright (C) 2000-2001 Nullsoft, Inc.
5 ** Author: Justin Frankel
6 ** File: listen.cpp - JNL TCP listen implementation
7 ** License: see jnetlib.h
14 JNL_Listen::JNL_Listen(short port
, unsigned int which_interface
)
17 m_socket
= ::socket(AF_INET
,SOCK_STREAM
,0);
18 if (m_socket
== INVALID_SOCKET
)
23 struct sockaddr_in sin
;
24 SET_SOCK_BLOCK(m_socket
,0);
27 setsockopt(m_socket
, SOL_SOCKET
, SO_REUSEADDR
, &bflag
, sizeof(bflag
));
29 memset((char *) &sin
, 0,sizeof(sin
));
30 sin
.sin_family
= AF_INET
;
31 sin
.sin_port
= htons( (short) port
);
32 sin
.sin_addr
.s_addr
= which_interface
?which_interface
:INADDR_ANY
;
33 if (::bind(m_socket
,(struct sockaddr
*)&sin
,sizeof(sin
)))
35 closesocket(m_socket
);
36 m_socket
=INVALID_SOCKET
;
40 if (::listen(m_socket
,8)==-1)
42 closesocket(m_socket
);
43 m_socket
=INVALID_SOCKET
;
49 JNL_Listen::~JNL_Listen()
51 if (m_socket
!=INVALID_SOCKET
)
53 closesocket(m_socket
);
57 JNL_IConnection
*JNL_Listen::get_connect(int sendbufsize
, int recvbufsize
)
59 if (m_socket
== INVALID_SOCKET
)
63 struct sockaddr_in saddr
;
64 socklen_t length
= sizeof(struct sockaddr_in
);
65 SOCKET s
= accept(m_socket
, (struct sockaddr
*) &saddr
, &length
);
66 if (s
!= INVALID_SOCKET
)
68 JNL_IConnection
*c
=new JNL_Connection(NULL
,sendbufsize
, recvbufsize
);