Thread: Making simple client-server program

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    Making simple client-server program

    Hello people,
    I'm making simple client server program. I have done this before.
    I have two computers in home LAN and want to test it.
    Computer A on which server program is located has 192.168.0.2 IP address
    Computer B on which is client program has IP: 192.168.0.1.
    Aplication is simple and basically it just send/receive strings.
    Everything works OK if I on client side do something like this:
    Code:
    he = gethostbyname ("192.168.0.2");	
    server_adresa.sin_family = AF_INET;     
    server_adresa.sin_port = htons (PORT_NUM); 
    server_adresa.sin_addr = *( (struct in_addr * ) he->h_addr );
    But this solution require me to know server address in advance and to hard coded it.
    Is there any way, and API function (Windows) which can help me to do this dinamically.
    I know that is hard.
    Basically I want this:
    On Computer A I want to open port and listen, and on computer B when I start client program to try to connect to server over home LAN.
    I don't think there is a simple way to do this, but just want your advice.
    Thanks
    Last edited by Micko; 07-05-2007 at 02:24 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Send a packet to 255.255.255.255 (the broadcast address) from the client, so the server can respond to tell the client where it is.
    Last edited by Doodle77; 07-05-2007 at 10:10 AM.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why can't you have the user enter the server IP manually at runtime?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks, I made it just like that. I introduced a special command using which user must enter server's IP and it works OK.
    But I'm curious, how this is done in "real world". For example mIRC. When I install mirc on my computer, that is client program.
    In order to chat I need to connect to server. When I enter server's name for example: chat.mirc.server what is happening?
    Does program has built in routine that translate this to some static server's IP or what?
    I really want to know how real mIRC works? How mIRC client connects to server when user types server's name?
    Thank you very much

    P.S. Doodle77, I like your idea, but don't know how to implement it. When I have time I'll try to make that to see if it works
    Last edited by Micko; 07-09-2007 at 01:40 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when program has a server name it has to resolve the server IP
    Which is done in several steps...
    If I remember right
    1. It searches the local Hosts file for the name given
    2. The request is sent to the Computer browser of the LAN - to check if the server is a member of the LAN network
    2. If not found - the special request is sent to the dns server (You can configure your dns server address manually in the TCP_IP settings or receive its address from the DHCP server if the dynamic IP is configured for your comp)
    DNS server looks in its database and returns the address of the computer, if not found - the request is forwarded according to the domain name of the desired computer...

    You can search net for the detailed and more accurate description of the name resolution process
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    when program has a server name it has to resolve the server IP
    Which is done in several steps...
    If I remember right
    1. It searches the local Hosts file for the name given
    2. The request is sent to the Computer browser of the LAN - to check if the server is a member of the LAN network
    2. If not found - the special request is sent to the dns server (You can configure your dns server address manually in the TCP_IP settings or receive its address from the DHCP server if the dynamic IP is configured for your comp)
    DNS server looks in its database and returns the address of the computer, if not found - the request is forwarded according to the domain name of the desired computer...
    You don't have to do ANY of that yourself. Simply calling gethostbyname() does it all for you.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    You don't have to do ANY of that yourself. Simply calling gethostbyname() does it all for you.
    I do not mean you implement it - I mean - what is going on under the cover... You should undestand it is your gethostbyname failed to solve the network configuration problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Commerical MMORPG Developer Opp
    By Th3Guy in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 01-22-2007, 11:28 AM
  5. TCP/IP client & echo server
    By Jordban in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 06:39 PM