Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I want to send ten integers from 0-9 from a server to a client. Both sides have a tcp and udp sockets. The even numbers, 0 included, will be ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    8

    UDP TCP port problem

    Hi,

    I want to send ten integers from 0-9 from a server to a client.
    Both sides have a tcp and udp sockets.
    The even numbers, 0 included, will be sent thru the tcp.
    The odd numbers will be sent thru the udp.

    This is what I wrote but it gives me a bad address error by the recvfrom on the client side.

    Client side

    PHP Code:

               struct sockaddr_in server 
    ;

        
    server.sin_family AF_INET;
            
    inet_aton(argv[1],&(server.sin_addr));
            
    server.sin_port htons(atoi(argv[2]));

        
    printf("UDP: Server Socket\n");
        if( (
    udp_socket socket (AF_INET,SOCK_DGRAM,0)) < ){
            
    somethingIsWrong("UDP"SOCKERR);
        }

        
    printf("UDP: Connect\n");
        if ((
    connect(udp_socket, (struct sockaddr*)&serversizeof(server))) < ){
            
    somethingIsWrong("UDP"CONCTERR);
        }

        
    printf("TCP: Connect\n"); 
        if( (
    tcp_socket tcp_connect(argv[1], atoi(argv[2]))) < ){ 
            
    somethingIsWrong("TCP"SOCKERR); 
        }

        for( 
    i=10 i+=){
            if( (
    cur_receivedtcp_receive(tcp_socketbufBUFFSIZE)) < ){ 
                
    somethingIsWrong("TCP"READERR); 
            }
            
    printf("TCP- %i "atoi(&buf[0]));
            
    i++;
            if( (
    cur_received recvfrom(udp_socket, &bufsizeof(buf),0
                    (
    struct sockaddr *)&server, (socklen_t *) sizeof(server))) < ){
                
    somethingIsWrong("UDP"READERR); 
            }
            
    printf("UDP- %i "atoi(&buf[0]));
        }

            
    printf("\n"); 
    Server
    PHP Code:

            struct sockaddr_in server 
    ;
        
    struct sockaddr_in client ;

        
    server.sin_family AF_INET;
        
    server.sin_addr.s_addr INADDR_ANY;
        
    server.sin_port htons(atoi(argv[2]));

        
    printf("Udp: Socket\n");

        if( (
    server_udp socket (AF_INET,SOCK_DGRAM,0)) < 0){
            
    somethingIsWrong("UDP"SOCKERR);
        }

        
    printf("Bind UDP\n");
        if(
    bind (server_udp, (struct sockaddr*)&serversizeof(server))==-1){
            
    somethingIsWrong("UDP"BINDERR);
        }

        
    printf("Establish tcp server\n");
            
    // tcp_establish creates socket, bind it to port=argv[1] and listens
        
    if( (server_tcp tcp_establish(atoi(argv[1]), BACKLOG )) < ){
            
    somethingIsWrong("TCP"SOCKERR);
        }

        
    printf("New Client\n");
            
    // accepts new clients
        
    if( (client_tcp tcp_get_connection(server_tcpNULL )) < ){
            
    somethingIsWrong("TCP"ACCEPTERR);
        }

        for( 
    i=10 i++ ){
            
    printf("%i "i);
            
    memcpy(buf, &isizeof(u_int));
            if( 
    tcp_send(client_tcpbufsizeof(int), 0) < ){ 
                
    somethingIsWrong("TCP"SENDERR); 
            }
            
    i++;
            
    memcpy(buf, &isizeof(u_int));
            
    printf("%i "i);
            if( 
    sendto(server_udpbufBUFFSIZE0, (struct sockaddr *) &client,  sizeof(client) ) < ){ 
                
    somethingIsWrong("UDP"SENDERR); 
            }
        }    

        
    printf("\n"); 

    Any idea how to solve this problem ?
    Thanks.

  2. #2
    Just Joined!
    Join Date
    Feb 2009
    Posts
    45
    The problem was obviously posted at least thrice and is already solved:

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    8
    I will try and make admins delete them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...