LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2009, 05:43 AM   #1
FaresH
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
UDP/TCP combination 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[3]));

    
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 ?
I know that a tcp and udp can be on the same port, since Ip header makes it clear what protocol is being used.
But I am running the client and server on the same pc. Maybe thats the problem that we have two udp and two tcp on the same port. Can that be it ? If so how do I solve this ?

Thanks.
 
Old 02-26-2009, 07:00 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
FaresH,

I understand you require help with your application, however posting your query on multiple forums (the other being the Ubuntu Programming Forum) just adds clutter to the WWW.

Anyhow, should anyone else come across this thread, click on this link to see my response to FaresH's question.
 
Old 02-26-2009, 09:08 AM   #3
FaresH
LQ Newbie
 
Registered: May 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Whats wrong with multi-feedbacks ?

Anyhow I solved my problem a few minutes ago, I just came to say that I solved it.
The error was not in the design, it was in the recvfrom.

recvfrom requires that the last parameter must be a pointer. Unlike in sendto. I am assuming because recvfrom updates/changes something in the struct sockaddr_in client. Maybe the size changes.
And I was giving it sizeof(client). So it couldn't update it.
So the second time recvfrom was called, it gave me a bad address.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
About TCP / UDP shipon_97 Linux - Newbie 5 06-24-2012 06:22 PM
TCP and UDP aatwell Programming 4 11-07-2007 08:47 AM
tcp/udp and c++ Kroenecker Programming 1 05-10-2005 11:56 AM
UDP over TCP The_Nerd Programming 7 07-21-2004 09:45 PM
TCP vs. UDP mikeshn Linux - Networking 5 05-17-2003 04:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration