File: test_sock.c

package info (click to toggle)
libstrophe 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,356 kB
  • sloc: ansic: 16,695; makefile: 301; sh: 61
file content (70 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
/* test_sock.c
** libstrophe XMPP client library -- test routines for the socket abstraction
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
**  This software is provided AS-IS with no warranty, either express
**  or implied.
**
** This program is dual licensed under the MIT or GPLv3 licenses.
*/

#include <stdio.h>
#include <string.h>

#ifndef _WIN32
#include <sys/select.h>
#endif

#include "sock.h"

int wait_for_connect(sock_t sock)
{
    fd_set wfds, efds;
    int ret;

    FD_ZERO(&wfds);
    FD_SET(sock, &wfds);
    FD_ZERO(&efds);
    FD_SET(sock, &efds);

    ret = select(sock + 1, NULL, &wfds, &efds, NULL);
    if (ret <= 0)
        return -1;

    if (FD_ISSET(sock, &efds))
        return 0;
    if (FD_ISSET(sock, &wfds))
        return 1;

    return -1;
}

int main()
{
    sock_t sock;
    int err;

    sock_initialize();

    sock = sock_connect("www.google.com", 80);

    if (sock < 0) {
        sock_shutdown();
        return 1;
    }

    err = wait_for_connect(sock);
    if (err < 0) {
        sock_close(sock);
        sock_shutdown();
        return 1;
    }

    sock_close(sock);

    sock_shutdown();

    return 0;
}