I need some information concerning on a small project I've been given.
I have little experience in network programming.
"Simulate a DHCP Server/client using TCP (in java)
A client will simulate that it is connecting to a DHCP server. It will send its MAC Address and in return receive an IP Address. The server will have to maintain a list of IP Addresses allocated. IP addresses are allocated for a leased time, which can be set on the server. When the lease time is over, the server will allocate another IP Address to the client. The project will have to be implemented using multithreading
Is it really possible to write it in TCP? As far as i know, tcp doesn't support broadcast and dhcp server uses UDP normally.
Any help will be greatly appreciated.
Related
Using tcp sockets, I wrote a server, launched it on a computer. Wrote a client for android. Being in the same local network, I successfully connect from my smartphone knowing the local ip address of the computer and the server port.
The problem is that I want to run this server on multiple computers on the local network, and their local addresses can change.
Need to somehow scan and select an available server in the application.
How is it implemented? What is the special protocol for scanning? Or just try to connect by sorting through all possible addresses and ports?
Is UDP broadcast a bad solution?
Sorry for google-translator!
UPD: I am trying to make a remote PC application. This will not work on the global internet. only local network. Thanks for the answer
I solve it by use DatagramSocket to UDP broadcast to 192.168.1.255.
I am trying to make an application which consist of a Java UDP client and a PHP UDP server. I am thinking about binding the server with the client over internet, because the client may not have real/public ip address. Though the server is a web application itself which will open a listener on the public address over some port. The task of the client is to communicate (send/receive) data over UDP.
Communicating parts:
Client: Windows/Linux OS connected to Internet (Java application)
Server: Windows IIS Server, with a real ip and domain (PHP web application)
This will depend on the firewalling/NAT-ing policy at a given client site, but the usual setup is to allow UDP out to the server and then allow corresponding reply packets back in, based on source and destination addresses and port numbers.
It would be a problem if you were to try and create a server behind your router.
Your router will replace the client's local ip address with the public ip address of your router at the moment that you send something to the server. The router will internally keep a translation table. This translation table is consulted when the server sends a reply to retrieve the original IP adress where the request originated and then the reply is simply forwarded to that IP address. As such it seems as if you are using the ip# that is allocated for you usually using DHCP by your router. But this local ip# is never seen on the internet only on your local lan.
Because the translation table is built by you trying to send to a server, you cannot easily setup a server behind your router. Simply because your router will not have an entry in its translation table to route the request coming from the internet to your server. This is called NAT/PAT. Because the translation table keeps track of ip adressen and ports.
All IP addresses are real. But people talk about a public IP# and a local IP#. The public IP# is the one that is used on the internet where the local ip# is the one that you use on your local LAN.
I've tested the program locally using a program called "Packet Tester"
I downloaded UDP Receiver/Sender for my mobile. Using the java program on my computer I was able to send packets to the mobile server and it was working great. I just typed in the mobiles IP address and the port number it was listening for.
When I tried running my UDP peer to peer on my Laptop and Computer they could not chat at all.
Does anyone know if it is to do with the fact the same router is being used or what not. It should not be since my mobile is receiving messages from the laptop and computer. ]
The Java app can retrieve UDP packets locally, but not from the laptop or mobile. The mobile can retrieve udp packets from the laptop and mobile. The tests show confusing results which have lead me to a wall.
I would appreciate what your thoughts on this are.
Thank you.
The problem might be because of Network Address Translation done by the router.
I believe you are sending packets to correct IP address but the port is not correct. The port to which you might be sending the packets is the local port to the machine.
You need to send the packets to the port assigned by the router.
When the packet goes from a computer A to computer B through the router, the router maps the local port of computer to some random port.
So, if computer B needs to send a packet to computer A, then the computer B needs to send it to the IP:port assigned by the router. The router will then forward it to the local IP:port of computer A.
I suggest first understand how this mapping is created when UDP packets travel through a router within or external to the network. Read about Network Address Translation, UDP holepunching.
These source may help:
Network Address Translation
UDP Hole Punching
RFC 4787 NAT Behavioral Requirements UDP
RFC 5128 P2P across NAT
I'm developing a game that can be played with computer or versus other players. The GUI is Swing but irrelevant for the matter. My question is : how do I connect with other players in a network?
I am familiar with terms like client / server, sockets etc, and i can write a basic client/server program, but it can only be run from ONE computer. I don't know how to connect to this network from a computer, say, half way across the country.
You are probably setting behind a router which hides your local IP address from the rest of the world (look for NAT for more info on this). Basically, the world only sees your YOUR_ROUTER_IP, your router takes care of carrying all IP packets from the outside world to you and vice versa. You will need to change your router settings so that your computer/server gets the router ip address. This way you can access your server application from anywhere in the world by using simple socket operations.
I hope this helps.
I like to use Kryonet for network connections, it works very well and has really good documentation.
You indicated you know how to use client and server sockets, so I'll just throw out there that you ought to try connecting with "real" ip address instead of localhost (127.0.0.1). Take two machines on your local network, get the IP address of the "server" machine and use that address to connect from the client.
This will work all the way around the world, except for the fact that you are likely behind some Network Address Translation (NAT) firewalls and will likely need to "open" or "forward" the ports you need. If you need more information on NAT, google and serverfault will be pretty helpful.
If your client and server is located on the same machine, then you can use loopback address(ie 127.0.0.1), in LAN you can do with private ip addresses
Private ip address for LAN
CLASS A - 10.x.x.x
CLASS B - 172.16.0.0 - 172.31.255.255
CLASS C - 192.168.0.0 - 192.168.255.255
you can use this for LAN environment
For connecting someone over the internet, you will need Public ip addresses.
Address apart from the private ip in every range are public address.
Now if you have a server which is having a static ip then it wont be a problem for the client to access it anywhere from the world over internet.
But if its over a LAN , and accessing the internet from a gateway, then there will be a NAT, then you will need to set the inbound and outbound traffic rules at the gateway, for letting the client access the server.
I am writing a distributed Java app, but the networking side of things is stumping me. For some reason it's not working correctly. I think it's because the IP address I get through ipconfig /all is not accessible from outside the LAN. I appreciate any tips or advice.
Overview
You need what is commonly-known as "NAT Traversal", or ICE. There are two primary protocols used on the internet today TCP and UDP. TCP sockets carry a significant amount of session state information in them; consequently it is a significantly more difficult protocol to use for P2P than UDP.
UDP Tunneling
The following list is a simplified outline of the more general STUN Protocol (RFC 5389) that you could use to implement a P2P service based on UDP NAT Traversal...
Deploy a UDP server with a public address and start listening for UDP packets from your clients. Clients will embed their private IP address inside the UDP packets sent to your server; it would be a good idea to implement some form of authentication to ensure you are getting connections from a valid client (instead of some random packet scanner).
The server reads how their private IP address has been translated into a public IP address from the UDP Datagrams.
If you want to make connections between specific users, also embed this information inside packets sent from the clients to your server; your server will implement a username directory to associate client UDP socket information with usernames (that peers will try to connect to).
Your UDP server should send the corresponding information back to the other relevant peer(s).
Now, peers can communicate directly by sending UDP datagrams to these translated addresses; these packets will go through client NAT devices in the path as long as the UDP ports in question are allowed and the delay introduced by this protocol does not trigger state timeouts in the NAT devices.
After you have established UDP connectivity, you could form an UDP SSL VPN between the two clients using something like OpenVPN; this would give you a trivial channel to initiate a TCP connection between the clients. However, there are non-trivial security and trust issues to consider in this connectivity model; it is unlikely to be useful between random users on the internet.
TCP
If TCP connectivity is required, I suggest looking at this internet draft, MMUSIC-ICE-TCP: TCP Candidates with Interactive Connectivity Establishment (ICE)
some ip address are not routable: http://en.wikipedia.org/wiki/IP_address - if you have one of those, you wont be able to access it from outside the lan directly. you can access the other addresses on your lan from one that is on that lan.
if you are outside the lan, you can not start a tcp connection to a specific machine on the lan, but that machine can start one with you: http://en.wikipedia.org/wiki/Network_address_translator