summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/tcp_server.py
blob: bc454d7d0be2190f660930f4dc201e16d1fc0c4c (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python2
#
# SPDX-License-Identifier: GPL-2.0
#

import sys, os, os.path, getopt
import socket, time
import subprocess
import select

def read(sock, n):
    buf = ''
    while len(buf) < n:
        rem = n - len(buf)
        try: s = sock.recv(rem)
        except (socket.error), e: return ''
        buf += s
    return buf

def send(sock, s):
    total = len(s)
    count = 0
    while count < total:
        try: n = sock.send(s)
        except (socket.error), e: n = 0
        if n == 0:
            return count;
        count += n
    return count


SERVER_PORT = 12877
MAX_PORTS = 2

serverPort = SERVER_PORT
serverSocket = None

HostName = socket.gethostname()

# create passive socket
serverSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
host = socket.gethostname()

try: serverSocket.bind((host, 0))
except socket.error as msg:
    print 'bind fails: ', msg

sn = serverSocket.getsockname()
serverPort = sn[1]

cmdStr = ("./tcp_client.py %d &") % (serverPort)
os.system(cmdStr)

buf = ''
n = 0
while n < 500:
    buf += '.'
    n += 1

serverSocket.listen(MAX_PORTS)
readList = [serverSocket]

while True:
    readyRead, readyWrite, inError = \
        select.select(readList, [], [], 2)

    if len(readyRead) > 0:
        waitCount = 0
        for sock in readyRead:
            if sock == serverSocket:
                (clientSocket, address) = serverSocket.accept()
                address = str(address[0])
                readList.append(clientSocket)
            else:
                sock.settimeout(1);
                s = read(sock, 1000)
                n = send(sock, buf)
                sock.close()
                serverSocket.close()
                sys.exit(0)
    else:
        print 'Select timeout!'
        sys.exit(1)
OpenPOWER on IntegriCloud