Exemple #1
0
int main(){
    int conn1;
    char recbuf[PKTSIZE];
    int size,j,i;
	
    sequence_init();
    tcp_init();
    conn1=tcp_add_connection("134.130.62.98" ,2500, "134.130.62.94",2500, CONN_SERVER);
    tcp_add_ip(conn1,"134.130.62.98" ,2501, "134.130.62.94",2501);
    tcp_add_ip(conn1,"134.130.62.98" ,2502, "134.130.62.94",2502);
    tcp_add_ip(conn1,"134.130.62.98" ,2503, "134.130.62.94",2503);
    if (tcp_establish_connection(conn1) < 0) {
	printf("kann Verbindung nicht aufbauen!\n");
	exit(-1);
    }

    i = 0;
    while(1) {
	printf("testing for data...\n");
	while (!tcp_select(conn1, 0)) {  
	    printf("."); 
	    fflush(stdout);
	    sleep(1); 
	}
	printf("\n");
	printf("message available!\n");

	size = tcp_receive_message(recbuf, PKTSIZE, conn1);
	if (size != i) {
	    fprintf(stderr, "expected %d Bytes, got %d Bytes\n", i, size); fflush(stderr);
	}
	    
	fprintf(stderr, "got %d Bytes, first Byte: %d\n", size, (int)((char*)recbuf)[1]); fflush(stderr);
	    
	if (*((char*)recbuf) == FERTIG) {
	    printf("Dienstschluss!\n");
	    break;
	}

	for ( j = 1; j < size; j++) 
	    if (((char*)recbuf)[j] != sequence()) {
		fprintf(stderr,"error in data: size= %d, pos = %d!\n",size,j);fflush(stderr);
		exit(-1);
		    
	    }
	i += 128;
    }
    tcp_close_connections(conn1);	
    return 1;
}
Exemple #2
0
static void tcp_socket_activity (SOCKET fd, short revents, void *arg)
{
	IP_CX		*cxp = (IP_CX *) arg;
	int		err, r;
	socklen_t	sz;
	int		handle;

	ctrc_begind (TCPS_ID, TCPS_SK_ACT, &fd, sizeof (fd));
	ctrc_contd (&revents, sizeof (revents));
	ctrc_endd ();

	trace_poll_events (fd, revents, arg);
# if 0
	if (!cxp->fd_owner)
		log_printf (RTPS_ID, 0, "Socket is not the fd_owner [%d] cxp:%p(%d) paired:%p(%d) \r\n",
				fd,
				(void *) cxp, cxp->fd_owner,
				(void *) cxp->paired, cxp->paired ? cxp->paired->fd_owner : 99999);
# endif
	if ((revents & (POLLERR | POLLNVAL)) != 0) {
		sz = sizeof (err);
		r = getsockopt (cxp->fd, SOL_SOCKET, SO_ERROR, &err, &sz);
		if ((r == -1) || err)  {
			log_printf (RTPS_ID, 0, "POLLERR | POLLNVAL [%d]: %d %s\r\n", cxp->fd, err, strerror (err));
			tcp_cleanup_ctx (cxp);
			return;
		}
	}
	if ((revents & POLLHUP) != 0) {
		tcp_cleanup_ctx (cxp);
		return;
	}
	if ((revents & POLLOUT) != 0) {
		handle = cxp->handle;
		tcp_write_message_fragment (cxp);
		/* It is possible that the above call ended up in cxp being cleaned up. We can verify this by varifying
		   on the handle of that context. If the handle is still valid, we're in good shape and it is safe
		   to continue. */
		if (!rtps_ip_from_handle (handle)) {
			log_printf (RTPS_ID, 0, "POLLOUT [%d]: cxp %p h:%d was cleaned up\r\n", fd, (void *) cxp, handle);
			return;
		}
	}
	if ((revents & POLLIN) != 0) {
		tcp_receive_message (cxp);
		return;
	}
}