38 Network-related functions (vi)--live555 source Reading (iv) network
- 38 Network-related functions (vi)--live555 source Reading (iv) network
- Brief introduction
- Makesocketnonblocking and Makesocketblocking set interface blocking property settings
- Setupstreamsocket set the flow-socket interface
This article by Mob Lym fabricated, welcome reprintblog.cnblogs.net/oloroso
This article by Mob Lym fabricated, welcome reprintmy.oschina.net/oloroso
Brief introduction
Network-related functions are a series of functions for manipulating network data. There are definitions of related functions in multiple files. There are also some functions socket API
that are system-related functions that are not mentioned.
Most of the functions of this series have a characteristic and require a usageenvironmet& type parameter.
Most of these methods are live555sourcecontrol\groupsock\include\GroupsockHelper.hh
declared in.
Makesocketnonblocking and Makesocketblocking set interface blocking property settings
makeSocketNonBlocking
The function is used to sock
add O_NONBLOCK
non-blocking properties for the set of interfaces represented by the parameter.
// 设置sock为非阻塞模式Boolean makeSocketNonBlocking(int sock) {#if defined(__WIN32__) || defined(_WIN32) unsigned long arg = 1; return ioctlsocket(sock, FIONBIO, &arg) == 0;#elif defined(VXWORKS) int arg = 1; return ioctl(sock, FIONBIO, (int)&arg) == 0;#else int curFlags = fcntl(sock, F_GETFL, 0); return fcntl(sock, F_SETFL, curFlags|O_NONBLOCK) >= 0;#endif}
makeSocketBlocking
The function is used to sock
remove the O_NONBLOCK
non-blocking attribute for the set of interfaces represented by the parameter.
// 设置sock为阻塞模式Boolean makeSocketBlocking(int sock) {#if defined(__WIN32__) || defined(_WIN32) unsigned long arg = 0; return ioctlsocket(sock, FIONBIO, &arg) == 0;#elif defined(VXWORKS) int arg = 0; return ioctl(sock, FIONBIO, (int)&arg) == 0;#else int curFlags = fcntl(sock, F_GETFL, 0); return fcntl(sock, F_SETFL, curFlags&(~O_NONBLOCK)) >= 0;#endif}
Setupstreamsocket set the flow-socket interface
setupStreamSocket
and setupDatagramSocket
the function is very similar, the difference is that the return is a streaming socket interface.
makeNonBlocking
Parameters are used to control whether the set of interfaces created is blocked.
Set the streaming socket int Setupstreamsocket (usageenvironment& env, Port port, Boolean makenonblocking) {if (!initializewins Ockifnecessary ()) {Socketerr (env, "Failed to initialize ' Winsock ':"); return-1; } int newsocket = Createsocket (sock_stream); if (Newsocket < 0) {Socketerr (env, "unable to create stream socket:"); return newsocket; } int reuseflag = Groupsockpriv (env)->reuseflag; Reclaimgroupsockpriv (env); if (setsockopt (Newsocket, Sol_socket, SO_REUSEADDR, (const char*) &reuseflag, sizeof Reuseflag) < 0) { Socketerr (env, "setsockopt (SO_REUSEADDR) Error:"); Closesocket (Newsocket); return-1; }//So_reuseport doesn ' t really make sense for TCP sockets, so we//normally don ' t set them. However, if you really want to does this//#define REUSE_FOR_TCP#IFDEF reuse_for_tcp#if defined (__win32__) | | Defined (_WIN32)//Windoze doesn ' t properly handle so_reuseport#else#ifdef SO_REUSEPORT if (setsockopt (Newsocket, Sol_socket, So_reuseport, (const char*) &reuseflag, sizeof Reuseflag) < 0) { Socketerr (env, "setsockopt (so_reuseport) Error:"); Closesocket (Newsocket); return-1; } #endif #endif#endif//Note:windoze requires binding, even if the port number is 0#if defined (__win32__) | | Defined (_WIN32) #else if (Port.num ()! = 0 | | Receivinginterfaceaddr! = inaddr_any) {#endif make_sockaddr_in (name, RECEIVINGINTERFACEADDR, Port.num ()); if (Bind (Newsocket, (struct sockaddr*) &name, sizeof name)! = 0) {char tmpbuffer[100]; sprintf (Tmpbuffer, "bind () Error (Port number:%d):", Ntohs (Port.num ())); Socketerr (env, tmpbuffer); Closesocket (Newsocket); return-1; } #if defined (__win32__) | | Defined (_WIN32) #else} #endif//If the parameter is set to non-blocking if (makenonblocking) {if (!makesocketnonblocking (Newsocket)) {Socketerr (env, "FAIled to make non-blocking: "); Closesocket (Newsocket); return-1; }} return newsocket;}
38 Network-related functions (vi)--LIVE555 source read (iv) network