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;
50 if( WSAStartup(MAKEWORD(1,1), &data) ) {
51 fatal_handler( "somethin's broke with windows socket startup" );
57 // ------------------------------------------------------------------
59 // ------------------------------------------------------------------
60 if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
61 fatal_handler( "tcp_connect(): Cannot create socket" );
65 // ------------------------------------------------------------------
67 // ------------------------------------------------------------------
68 if( (hptr = gethostbyname( sock_obj->server ) ) == NULL ) {
69 fatal_handler( "tcp_connect(): Unknown Host" );
73 // ------------------------------------------------------------------
74 // Construct server info struct
75 // ------------------------------------------------------------------
76 memset( &remoteAddr, 0, sizeof(remoteAddr));
77 remoteAddr.sin_family = AF_INET;
78 remoteAddr.sin_port = htons( sock_obj->port );
79 memcpy( (char*) &remoteAddr.sin_addr.s_addr,
80 hptr->h_addr_list[0], hptr->h_length );
82 // ------------------------------------------------------------------
83 // Construct local info struct
84 // ------------------------------------------------------------------
85 memset( &localAddr, 0, sizeof( localAddr ) );
86 localAddr.sin_family = AF_INET;
87 localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
88 localAddr.sin_port = htons(0);
90 // ------------------------------------------------------------------
91 // Bind to a local port
92 // ------------------------------------------------------------------
93 if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) {
94 fatal_handler( "tcp_connect(): Cannot bind to local port" );
98 // ------------------------------------------------------------------
100 // ------------------------------------------------------------------
101 if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) {
102 fatal_handler( "tcp_connect(): Cannot connect to server %s", sock_obj->server );
106 sock_obj->sock_fd = sock_fd;
107 sock_obj->connected = 1;
113 int tcp_send( transport_socket* sock_obj, const char* data ){
115 if( sock_obj == NULL ) {
116 fatal_handler( "tcp_send(): null sock_obj" );
120 //fprintf( stderr, "TCP Sending: \n%s\n", data );
122 // ------------------------------------------------------------------
123 // Send the data down the TCP pipe
124 // ------------------------------------------------------------------
125 if( send( sock_obj->sock_fd, data, strlen(data), 0 ) < 0 ) {
126 fatal_handler( "tcp_send(): Error sending data" );
130 debug_handler( "Sent Data %s At %f Seconds", data, get_timestamp_millis() );
136 int tcp_disconnect( transport_socket* sock_obj ){
138 if( sock_obj == NULL ) {
139 fatal_handler( "tcp_disconnect(): null sock_obj" );
143 if( close( sock_obj->sock_fd ) == -1 ) {
145 // ------------------------------------------------------------------
146 // Not really worth throwing an exception for... should be logged.
147 // ------------------------------------------------------------------
148 warning_handler( "tcp_disconnect(): Error closing socket" );
155 // ------------------------------------------------------------------
156 // And now for the gory C socket code.
157 // Returns 0 on failure, 1 otherwise
158 // ------------------------------------------------------------------
159 int tcp_wait( transport_socket* sock_obj, int timeout ){
161 if( sock_obj == NULL ) {
162 fatal_handler( "tcp_wait(): null sock_obj" );
170 int sock_fd = sock_obj->sock_fd;
174 FD_ZERO( &read_set );
175 FD_SET( sock_fd, &read_set );
177 // ------------------------------------------------------------------
178 // Build the timeval struct
179 // ------------------------------------------------------------------
184 if( timeout == -1 ) {
186 // ------------------------------------------------------------------
187 // If timeout is -1, there is no timeout passed to the call to select
188 // ------------------------------------------------------------------
189 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, NULL)) == -1 ) {
190 warning_handler( "Call to select interrupted" );
194 } else if( timeout != 0 ) { /* timeout of 0 means don't block */
196 if( (retval = select( sock_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) {
197 warning_handler( "Call to select interrupted" );
202 memset( &buf, 0, BUFSIZE );
204 if( set_fl( sock_fd, O_NONBLOCK ) < 0 )
207 #ifdef _ROUTER // just read one buffer full of data
209 n = recv(sock_fd, buf, BUFSIZE-1, 0);
210 sock_obj->data_received_callback( sock_obj->user_data, buf );
214 #else // read everything we can
216 debug_handler( "Leaving Socket Select At %f Seconds", get_timestamp_millis() );
217 while( (n = recv(sock_fd, buf, BUFSIZE-1, 0) ) > 0 ) {
218 debug_handler("SOCKET Read: \n%s\n", buf);
219 sock_obj->data_received_callback( sock_obj->user_data, buf );
220 memset( &buf, 0, BUFSIZE );
225 if( clr_fl( sock_fd, O_NONBLOCK ) < 0 ) {
226 warning_handler("Unable to clear O_NONBLOCK flag on socket");
232 if( errno != EAGAIN ) {
233 warning_handler( " * Error reading socket with errno %d", errno );
248 int tcp_connected( transport_socket* obj ) {
251 if( ! obj->sock_fd ) { return 0; }
253 ret = read( obj->sock_fd , NULL,0 );