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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [tests/] [server_test.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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