File: socket_ipv4_tcp.c

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (31 lines) | stat: -rw-r--r-- 691 bytes parent folder | download | duplicates (7)
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
#include <sys/socket.h>
#include <unistd.h>       /* close() */
#include <stdio.h>        /* perror() */
#include <netinet/in.h>   /* struct sockaddr_in */

int main()
{
    int fd;
    int val;
    int ret;
    struct sockaddr_in addr;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd < 0)
        perror("socket");

    val = 1;
    (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    addr.sin_addr.s_addr = 0x7f000001;
    ret = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
    if (ret < 0)
        perror("connect");

    ret = close(fd);
    if (ret)
        perror("close");
    return 0;
}