Getting Ip from socket.

This is a discussion on Getting Ip from socket. within the Networking/Device Communication forums, part of the General Programming Boards category; ok. I have clients that connect to a server, i need the server to some how get the clients ip ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    Getting Ip from socket.

    ok.
    I have clients that connect to a server, i need the server to some how get the clients ip address so that i can pass them to other clients so i can allow direct connections of clients.
    For example, clientA connects to the server and clientB also connects to the server. clientA tells the server that he wants to send a file to clientB. So the server needs to do something like GetIpFromSocket(clientAsocket) and then send that to clientB so that both clients can achieve a direct connection. Any help on this matter will be apreciated.

    thanks in advance.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    accept() will return the ip address. Alternatively, getpeername() can retrieve the remote ip address of a connected socket.

    You need to be aware that many/most clients will be behing firewalls and you will not be able to connect to them without configuration by the user (which they will be very reluctant to perform).

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Sounds like P2P app. What the guy above said is true, so you may need to do some work with your Applications protocol, to determine if some one can connect directly, you may have too do some odd ball stuff to get the connection, like having a request system in your protocol that asks the which one can accept connections, and then tell the other one to connect to the one that can. Sorry that may sound confusing.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    hmm, i never thought of that, it could and will be quite a problem.
    I also don't see anyway around it without the user configuring his/her firewall.
    Do either of you know how a direct connection between clients could be achieved without disturbing the firewall?
    programs like msn messenger do it.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Do either of you know how a direct connection between clients could be achieved without disturbing the firewall?
    Nope. Any firewall that allowed another computer to connect to it without any sort of configuration would be pretty worthless, wouldnt it?

    programs like msn messenger do it.
    No, msn messenger doesnt do it. What it does is have client A try to connect to client B. If it cant, then it will have client B try to connect to client A. If they are both behind a firewall, then it cant send the file without one of the clients doing some configuration.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If they are both behind a firewall, then it cant send the file without one of the clients doing some configuration.
    Are you sure about that? I am able to (in most cases) send files to friends, even when both of us have firewalls - albeit very slowly. I came up with a theory that if neither can connect, then MSN just sends the file through the chat server somehow (packet header with a 'file transfer' flag or something).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    If both clients have a firewall up, then they cannot connect to each other without one of them opening a port in the firewall. There is no way around that.

    MSN messenger may have changed to allow people to send files through their server, but I could picture that killing their bandwidth.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    MSN messenger may have changed to allow people to send files through their server, but I could picture that killing their bandwidth.
    I suppose so. But I did notice that after one certain upgrade to MSN, I was suddenly able to upload files to others.. and there's an "If you're experiencing slow transfer rates, click here to know why" popup, which proceeds to explain that you probably have a firewall. And it DOES give you a horrible transfer rate (about 1-2 kb/s).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    by firewall configuration do you just mean that the firewall will ask the user if they want to allow an incomming connection from xx.xx.xx.xx?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Well, fancy new firewalls will ask the user if they want to allow an incomming connection, but many do not. For instance, my firewall (a linux box running iptables) requires me to manually open up ports that I want other computers to be able to connect to.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    ok, this program is a bit like a chat program the server is constantly looping with a pause of 10 miliseconds to check for new data in one of the sockets. If i had to send the file through the server, would it be wise to make a new thread for that file transfer?
    Because it would probably hog the other loop. Also, if the clients were able to direct connect would they have to use another port from the one they were using to connect to the server?

    thanks so far

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    If i had to send the file through the server, would it be wise to make a new thread for that file transfer?
    Yes, it would be very wise.


    Also, if the clients were able to direct connect would they have to use another port from the one they were using to connect to the server?
    Not only would you need to use another port, but you would need to create a new socket descriptor with a call to socket().

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Not only would you need to use another port
    Why does the direct-connect'ing client have to listen on a different port than the server? They're not on the same machine, so you shouldn't run into a port conflict...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    Yes, it would be very wise.
    I take it this new thread would have to use another port?

    also, i tried to use getpeername() but it takes a sockaddr struct as the second parameter, how do i get the Ip address from this?
    sa_data? or sa_family?

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>also, i tried to use getpeername() but it takes a sockaddr struct as the second parameter, how do i get the Ip address from this?
    Look into inet_ntoa(), and use a SOCKADDR_IN instead of SOCKADDR, then pass (the sockaddr_in).S_addr.s_addr I think.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Extracting WAN IP out of a SOCKET
    By apsync in forum Windows Programming
    Replies: 13
    Last Post: 10-04-2008, 03:38 PM
  3. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 06:49 AM
  4. IP without connecting to external socket
    By Hunter2 in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-26-2003, 08:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21