File: socketTest.cpp

package info (click to toggle)
between 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 3,524 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 102; perl: 67
file content (103 lines) | stat: -rw-r--r-- 2,226 bytes parent folder | download | duplicates (18)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Modification History
 *
 * 2001-January-9		Jason Rohrer
 * Created.
 *
 * 2001-January-28		Jason Rohrer
 * Added more output.
 * Added better error checking.
 *
 * 2001-December-14		Jason Rohrer
 * Added a test of HostAddress::getLocalAddress().
 *
 * 2002-July-30		Jason Rohrer
 * Added a test of streams.
 *
 * 2002-July-31   Jason Rohrer
 * Changed to test multiple blocks.
 *
 * 2002-February-4   Jason Rohrer
 * Added test of getting local address from socket.
 */


#include "Socket.h"
#include "SocketStream.h"
#include "SocketServer.h"
#include "SocketClient.h" 
#include "HostAddress.h"

#include <unistd.h>


int main() {

	HostAddress *address = HostAddress::getLocalAddress();

	printf( "Local address: " );
	address->print();
	printf( "\n" );

	int port = 5158;
    
	SocketServer *server = new SocketServer( port, 100 );
	
	printf( "Waiting for a connection on port %d\n", port );
	Socket *receiveSocket = server->acceptConnection();
	
	if( receiveSocket == NULL ) {
		return 1;
		}
	
	printf( "Connection received\n" );

    HostAddress *localAddress = receiveSocket->getLocalHostAddress();

    if( localAddress != NULL ) {
        printf( "Our local address (fetched from socket) is  " );
        localAddress->print();
        printf( "\n" );
        delete localAddress;
        }
    
    
    SocketStream *receiveStream = new SocketStream( receiveSocket );
    receiveStream->setReadTimeout( 10000 );
    
	int numBytes = 4000;

	int checksum = 0;
    
	unsigned char *buffer = new unsigned char[numBytes];
    /*    
    for( int i=0; i<numBytes; i++ ) {
        int numRec = receiveStream->read( buffer, 1 );
        checksum += buffer[ 0 ];

        if( numRec != 1) {
            printf( "Faiedl to read.\n" );
            return 1;
            }
        //sleep( 1 );
        }
    */
    int count = 0;
    while( true ) {
        int numRec = receiveStream->read( buffer, numBytes );
        printf( "Received %d successfully,\tcount = %d\n", numBytes, count );
        count++;
        }
    /*
	for( int i=0; i<numBytes; i++ ) {
		checksum += buffer[ i ];
		}
    */
	printf( "Checksum: %d\n", checksum );

    delete receiveStream;
	delete receiveSocket;
	delete server;
	
	return 0;
	}