1 #include "transport_socket.h"
5 int main( char* argc, char** argv ) {
7 transport_socket sock_obj;
9 sock_obj.server = "10.0.0.4";
10 sock_obj.data_received_callback = &print_stuff;
12 printf("connecting...\n");
13 if( (tcp_connect( &sock_obj )) < 0 ) {
14 printf( "error connecting" );
17 printf("sending...\n");
18 if( tcp_send( &sock_obj, "<stream>\n" ) < 0 ) {
19 printf( "error sending" );
22 printf("waiting...\n");
23 if( tcp_wait( &sock_obj, 15 ) < 0 ) {
24 printf( "error receiving" );
27 printf("disconnecting...\n");
28 tcp_disconnect( &sock_obj );
34 // returns the socket fd, -1 on error
35 int tcp_connect( transport_socket* sock_obj ){
38 if( sock_obj == NULL ) {
39 fatal_handler( "connect(): null sock_obj" );
43 struct sockaddr_in remoteAddr, localAddr;
47 // ------------------------------------------------------------------
49 // ------------------------------------------------------------------
50 if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
51 fatal_handler( "tcp_connect(): Cannot create socket" );
55 // ------------------------------------------------------------------
57 // ------------------------------------------------------------------
58 if( (hptr = gethostbyname( sock_obj->server ) ) == NULL ) {
59 fatal_handler( "tcp_connect(): Unknown Host => %s", sock_obj->server );
63 // ------------------------------------------------------------------
64 // Construct server info struct
65 // ------------------------------------------------------------------
66 memset( &remoteAddr, 0, sizeof(remoteAddr));
67 remoteAddr.sin_family = AF_INET;
68 remoteAddr.sin_port = htons( sock_obj->port );
69 memcpy( (char*) &remoteAddr.sin_addr.s_addr,
70 hptr->h_addr_list[0], hptr->h_length );
72 // ------------------------------------------------------------------
73 // Construct local info struct
74 // ------------------------------------------------------------------
75 memset( &localAddr, 0, sizeof( localAddr ) );
76 localAddr.sin_family = AF_INET;
77 localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
78 localAddr.sin_port = htons(0);
80 // ------------------------------------------------------------------
81 // Bind to a local port
82 // ------------------------------------------------------------------
83 if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) {
84 fatal_handler( "tcp_connect(): Cannot bind to local port" );
88 // ------------------------------------------------------------------
90 // ------------------------------------------------------------------
91 if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) {
92 fatal_handler( "tcp_connect(): Cannot connect to server %s", sock_obj->server );
96 sock_obj->sock_fd = sock_fd;
97 sock_obj->connected = 1;
103 int tcp_send( transport_socket* sock_obj, const char* data ){
105 if( sock_obj == NULL ) {
106 fatal_handler( "tcp_send(): null sock_obj" );
110 //fprintf( stderr, "TCP Sending: \n%s\n", data );
112 // ------------------------------------------------------------------
113 // Send the data down the TCP pipe
114 // ------------------------------------------------------------------
115 if( send( sock_obj->sock_fd, data, strlen(data), 0 ) < 0 ) {
116 fatal_handler( "tcp_send(): Error sending data" );
120 debug_handler( "Sent Data %s At %f Seconds", data, get_timestamp_millis() );
126 int tcp_disconnect( transport_socket* sock_obj ){
128 if( sock_obj == NULL ) {
129 fatal_handler( "tcp_disconnect(): null sock_obj" );
133 if( close( sock_obj->sock_fd ) == -1 ) {
135 // ------------------------------------------------------------------
136 // Not really worth throwing an exception for... should be logged.
137 // ------------------------------------------------------------------
138 warning_handler( "tcp_disconnect(): Error closing socket" );
145 // ------------------------------------------------------------------
146 // And now for the gory C socket code.
147 // Returns 0 on failure, 1 otherwise
148 // ------------------------------------------------------------------
149 int tcp_wait( transport_socket* sock_obj, int timeout ){
151 if( sock_obj == NULL ) {
152 fatal_handler( "tcp_wait(): null sock_obj" );
160 int sock_fd = sock_obj->sock_fd;
164 FD_ZERO( &read_set );
165 FD_SET( sock_fd, &read_set );
167 // ------------------------------------------------------------------
168 // Build the timeval struct
169 // ------------------------------------------------------------------
174 if( timeout == -1 ) {
176 // ------------------------------------------------------------------
177 // If timeout is -1, there is no timeout passed to the call to select
178 // ------------------------------------------------------------------
179 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
180 warning_handler( "Call to select interrupted" );
184 } else if( timeout != 0 ) { /* timeout of 0 means don't block */
186 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
187 warning_handler( "Call to select interrupted" );
192 memset( &buf, 0, BUFSIZE );
194 if( set_fl( sock_fd, O_NONBLOCK ) < 0 )
197 #ifdef _ROUTER // just read one buffer full of data
199 n = recv(sock_fd, buf, BUFSIZE-1, 0);
200 sock_obj->data_received_callback( sock_obj->user_data, buf );
204 #else // read everything we can
206 debug_handler( "Leaving Socket Select At %f Seconds", get_timestamp_millis() );
207 while( (n = recv(sock_fd, buf, BUFSIZE-1, 0) ) > 0 ) {
208 debug_handler("SOCKET Read: \n%s\n", buf);
209 sock_obj->data_received_callback( sock_obj->user_data, buf );
210 memset( &buf, 0, BUFSIZE );
215 if( clr_fl( sock_fd, O_NONBLOCK ) < 0 ) {
216 warning_handler("Unable to clear O_NONBLOCK flag on socket");
222 if( errno != EAGAIN ) {
223 warning_handler( " * Error reading socket with errno %d", errno );
238 int tcp_connected( transport_socket* obj ) {
241 if( ! obj->sock_fd ) { return 0; }
243 ret = read( obj->sock_fd , NULL,0 );