| Description | The socket function creates a communication end point called a socket. The argument family specifies the address family. Currently the only acceptable value is AF_INET. The argument type specifies the communication semantics. The following are the currently supported types: | Type | Description |
|---|
| SOCK_STREAM | Provides a reliable connection based data stream that is full-duplex. | | SOCK_DGRAM | Provides connectionless communication that is unreliable. |
The argument protocol specifies the protocol that must be used with socket type: | Protocol | Description |
|---|
| IPPROTO_TCP | Must be used with SOCK_STREAM socket type. | | IPPROTO_UDP | Must be used with SOCK_DGRAM socket type. | | 0 | The system selects a matching protocol for the socket type. |
The socket function is in the RL-TCPnet library. The prototype is defined in rtl.h. note - You must call the socket function before any other function calls to the BSD socket.
- If a negative number is returned, it represents an error code.
|
| Example |
#include <rtl.h>
__task void server (void *argv) {
/* Server task runs in 2 instances. */
SOCKADDR_IN addr;
int sock, sd, res;
int type = (int)argv;
char dbuf[4];
while (1) {
sock = socket (AF_INET, type, 0);
addr.sin_port = htons(1001);
addr.sin_family = PF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
bind (sock, (SOCKADDR *)&addr, sizeof(addr));
if (type == SOCK_STREAM) {
listen (sock, 1);
sd = accept (sock, NULL, NULL);
closesocket (sock);
sock = sd;
}
while (1) {
res = recv (sock, dbuf, sizeof (dbuf), 0);
if (res <= 0) {
break;
}
procrec ((U8 *)dbuf);
}
closesocket (sock);
}
}
|