File: socketClientTest.cpp

package info (click to toggle)
between 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 97; perl: 67
file content (125 lines) | stat: -rw-r--r-- 2,947 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Modification History
 *
 * 2001-January-9		Jason Rohrer
 * Created.
 *
 * 2001-January-28		Jason Rohrer
 * Added more output.
 * Changed so that it doesn't sleep in the middle of a block
 * since the requirements of the socket interface have changed
 * and implementations no longer have to support this behavior.
 *
 * 2001-December-12		Jason Rohrer
 * Changed to use new HostAddress constructor.
 *
 * 2002-July-30		Jason Rohrer
 * Fixed a bug in localhost address.  Added a test of streams.
 *
 * 2002-July-31   Jason Rohrer
 * Changed to test multiple blocks.
 *
 * 2002-December-2   Jason Rohrer
 * Changed to test socket creation failure.
 * Added counter to output.
 *
 * 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() {

    char *name = new char[99];
	sprintf( name, "63.249.65.249" );
	
    
	//char *name = new char[99];
	//sprintf( name, "192.168.1.2" );
	
	//char *name = "192.168.1.1";
	//int addressLength = 11;
	
	//char *name = "monolith.2y.net";
	//int addressLength = 15;
	
	int port = 5158;

	HostAddress *address = new HostAddress( name, port );

	printf( "Trying to connect to server: " );
	address->print();
	printf( "\n" );

    Socket *sock;
    int numConnections = 0;
    while( true ) {
        sock = SocketClient::connectToServer( address );
	
        if( sock == NULL ) {
            printf( "%d Connecting to server failed\n", numConnections );
            //return 1;
            }
        else {
            printf( "%d Connection established\n", numConnections );
            HostAddress *localAddress = sock->getLocalHostAddress();

            if( localAddress != NULL ) {
                printf( "Our local address (fetched from socket) is  " );
                localAddress->print();
                printf( "\n" );
                delete localAddress;
                }

            delete sock;
            }
        //usleep( 1000 );
        numConnections++;
        }
    
	int numBytes = 4000;

	unsigned char *buffer = new unsigned char[numBytes];
	for( int i=0; i<numBytes; i++ ) {
		buffer[i] = i;
		}

    SocketStream *stream = new SocketStream( sock );
    
	//printf( "sleeping\n" );
	//sleep( 10 );
    
    int count = 0;
    while( true ) {
        printf( "sending %d bytes\n", numBytes );
        int numSent = stream->write( buffer, numBytes );
	
        printf( "Sent %d successfully,\tcount = %d\n", numSent, count );
        count++;
        }
    
    int checksum = 0;
	for( int i=0; i<numBytes; i++ ) {
		checksum += buffer[ i ];
		}
	printf( "Checksum: %d\n", checksum );

    
    printf( "Deleting stream\n" );
	delete stream;
    printf( "Deleting socket\n" );
	delete sock;
    printf( "Deleting address\n" );
	delete address;

    printf( "Returning\n" );
	return 0;
	}