OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [server_test.c] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      tests/server_test.c
4
//
5
//      Simple TCP 'server' test
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    gthomas
19
// Contributors: gthomas
20
// Date:         2000-01-10
21
// Purpose:      
22
// Description:  
23
//              
24
//
25
//####DESCRIPTIONEND####
26
//
27
//==========================================================================
28
// Network server test code
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <network.h>
32
 
33
#ifndef CYGPKG_LIBC_STDIO
34
#define perror(s) diag_printf(#s ": %s\n", strerror(errno))
35
#endif
36
 
37
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
38
static char stack[STACK_SIZE];
39
static cyg_thread thread_data;
40
static cyg_handle_t thread_handle;
41
 
42
extern void
43
cyg_test_exit(void);
44
 
45
void
46
pexit(char *s)
47
{
48
    perror(s);
49
    cyg_test_exit();
50
}
51
 
52
static void
53
server_test(struct bootp *bp)
54
{
55
    int s, client;
56
    socklen_t client_len;
57
    struct sockaddr_in client_addr, local;
58
    char buf[256];
59
    int one = 1;
60
    fd_set in_fds;
61
    int num, len;
62
    struct timeval tv;
63
 
64
    s = socket(AF_INET, SOCK_STREAM, 0);
65
    if (s < 0) {
66
        pexit("stream socket");
67
    }
68
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
69
        pexit("setsockopt SO_REUSEADDR");
70
    }
71
    if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
72
        pexit("setsockopt SO_REUSEPORT");
73
    }
74
    memset(&local, 0, sizeof(local));
75
    local.sin_family = AF_INET;
76
    local.sin_len = sizeof(local);
77
    local.sin_port = htons(7734);
78
    local.sin_addr.s_addr = INADDR_ANY;
79
    if(bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
80
        pexit("bind error");
81
    }
82
    listen(s, SOMAXCONN);
83
    while (true) {
84
        client_len = sizeof(client_addr);
85
        if ((client = accept(s, (struct sockaddr *)&client_addr, &client_len)) < 0) {
86
            pexit("accept");
87
        }
88
        client_len = sizeof(client_addr);
89
        getpeername(client, (struct sockaddr *)&client_addr, &client_len);
90
        diag_printf("connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
91
 
92
        diag_sprintf(buf, "Hello %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
93
 
94
        write(client, buf, strlen(buf));
95
        tv.tv_sec = 5;
96
        tv.tv_usec = 0;
97
        FD_ZERO(&in_fds);
98
        FD_SET(client, &in_fds);
99
        num = select(client+1, &in_fds, 0, 0, &tv);
100
        if (num > 0) {
101
            len = read(client, buf, sizeof(buf)-1);
102
            buf[len] = '\0';
103
            diag_printf("buf = '%s'\n", buf);
104
        } else if (num == 0) {
105
            diag_printf("No reply - timed out\n");
106
        } else {
107
            perror("select");
108
        }
109
        close(client);
110
    }
111
    close(s);
112
}
113
 
114
void
115
net_test(cyg_addrword_t param)
116
{
117
    diag_printf("Start SERVER test\n");
118
    init_all_network_interfaces();
119
#ifdef CYGHWR_NET_DRIVER_ETH0
120
    if (eth0_up) {
121
        server_test(&eth0_bootp_data);
122
    }
123
#endif
124
    cyg_test_exit();
125
}
126
 
127
void
128
cyg_start(void)
129
{
130
    // Create a main thread, so we can run the scheduler and have time 'pass'
131
    cyg_thread_create(10,                // Priority - just a number
132
                      net_test,          // entry
133
                      0,                 // entry parameter
134
                      "Network test",    // Name
135
                      &stack[0],         // Stack
136
                      STACK_SIZE,        // Size
137
                      &thread_handle,    // Handle
138
                      &thread_data       // Thread data structure
139
            );
140
    cyg_thread_resume(thread_handle);  // Start it
141
    cyg_scheduler_start();
142
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.