[IPLUG/AU] add IParam::mIsMeta and related setter/getter in order to get params that...
[wdl/wdl-ol.git] / WDL / jnetlib / listen.cpp
blob91569cf14421f9692ee545d7fe0e866eee94f8f9
1 /*
2 ** JNetLib
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
8 */
10 #include "netinc.h"
11 #include "util.h"
12 #include "listen.h"
14 JNL_Listen::JNL_Listen(short port, unsigned int which_interface)
16 m_port=port;
17 m_socket = ::socket(AF_INET,SOCK_STREAM,0);
18 if (m_socket == INVALID_SOCKET)
21 else
23 struct sockaddr_in sin;
24 SET_SOCK_BLOCK(m_socket,0);
25 #ifndef _WIN32
26 int bflag = 1;
27 setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &bflag, sizeof(bflag));
28 #endif
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;
38 else
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)
61 return NULL;
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);
69 c->connect(s,&saddr);
70 return c;
72 return NULL;