Multiple Socket IP address debugging on same machine - java

I'm designing a chat service that uses Java server sockets.
When a user connects to my server with their browser I need to make sure their connection information stays the same when they open new pages on my site in their browser. So I need to keep track of their remote IP address. I am using this below, but am unable to test it as I am on a local machine.
client.socket.getRemoteSocketAddress()
I want to test my system with multiple IP address from the client conenctions but I can not do so since this method above returns the same IP as I am local. How can I go about debugging and testing such a setup in Eclipse?

You could assign multiple addresses to a single NIC in Windows' Network properties->advance option.
Check out short tutorial:
http://www.tutorialspoint.com/shorttutorials/assign-multiple-ip-addresses-to-windows-8-computer
Also, you most like have to bind the address manually,
i.e. client.bind("some ip");

Related

What is the best way to update the ip adress of serversocket for clients?

I'm programming a little server example with Sockets in Java.
Currently I'm using this for testing:
server= new Socket(InetAdress.getByName("127.0.0.1"),3333)
but my plan is to move it to my Raspberry Pi.
Unfortunately, I don't have a static IP address.
What is the proper way to update the IP address in the code?
I thought about storing it on a webserver and accessing it via an API, but that doesn't sound very secure, and it might slow down my code.
First off, your use of InetAdress.getByName() is redundant. Socket has a constructor that accepts a String as input:
server = new Socket("127.0.0.1", 3333)
That said, you should register a static domain name for your server, and set its DNS records to point at your server's IP. Then clients can use that domain name to connect to the server, instead of using the IP address directly:
server = new Socket("mydomain", 3333)
If your server does not have a static IP, there are plenty of free and cheap "Dynamic DNS" services available, which allow you to update your domain with your current IP address whenever it changes (typically using automated tools to simplify the detection-and-update process).
If the server is behind a router, many routers have built-in support for updating various DynDNS services for you. If your router supports this, you can configure it with your DynDNS account information so it can automatically update the domain whenever its WAN IP changes.

How to connect to SQL Server 2017 on a network computer (That is listening to a local port)

I would like to connect to a database that runs locally on one of our network computers here at work. I can connect to it just fine with the application that I developed that uses Java's sql driver manager. Now I would like to distribute it to the different computers on the network. To do this, I need a url that will point to the database through the network. The database is listening to port 1434. (Static URL string is "//localhost:1434")
MatsysUI.setConnection(DriverManager.getConnection("jdbc:sqlserver:" + MatsysIO.getStaticURL(), txtUser.getText(), txtPassword.getText()));
Problem is, I don't know where to start to find that, and I would like to avoid using an internet connection to connect to this database. Is there a way to route the connection to the network computer, then to its local port?
There are several possible answers to this.
I just want to restate your situation to make sure I understand:
You have an application, written in Java, which requires access to a SQL Server database.
You want to distribute multiple copies of that application to different client machines.
The client machines and the database server are all on a local network.
SQL Server is listening on port 1434
You need to construct the JDBC connection string on the client machines to access SQL Server.
You give the current connection string as //localhost:1434; in that scheme, localhost is the hostname. You can replace this with the fully qualified domain name of the SQL Server machine (this is almost certainly the simplest option). Using the FQDN allows you to replace the machine, or load balance it, etc. without worrying about the connections - but if the machine name ever changes, all your connections break!
You can also connect by IP address - this allows you to change the machine name, but obviously means the IP address can never change. In most circumstances, that's a bad thing.
Your final option is to look for alL SQL Servers on your network, and show them in a drop down for your user (your code suggests you're using username and password inputs). This means you don't have to distribute a new properties file if you want to change your server details.

Different IP Addresses from the same machine

I have a client-server setup. Where client connects to the server and asks which program to execute next. Server after getting a request checks for the hostname of the other end of the socket using below chunk of code which returns "127.0.0.1"
socket.getInetAddress().getCanonicalHostName();
After getting the program name the client creates a process which also tries to connect to the server but this time the above call returns different address. "mypc.foo.com" masking the domain name here
This behavior is bugging me as I am unable to lookup the hashmap where I store all the process details grouped by the machine ip.
Is it a bug in java lookup implementation or am I missing something. Please suggest some other way to do this lookup
I beleive socket.getInetAddress() returns your own address so basically always 127.0.0.1. Try using socket.getRemoteSocketAddress() - to get the other party's ip address.
Using IP address to match distinct users is generally a bad idea though. First of all they can be in some network or behind firewall and you can get requests from multiple clients coming from the same IP address. Also you are not guaranteed that it is static or dynamic IP. And also if your application is running in some strange network setup with strange routing you might end up getting all requests from the router IP address.
The better design would be to share some token between the server and the client and use that for identification. Does it sound familiar? For example http sessions are done like that ;)

Use VPN ip address htttp Urlconnection java

I have openVPN Connect configured on a vista laptop, so that connecting with a web browser allows the computer to change its ip address. What are the steps necessary to allow the following networking code in java to use this new ip address:
HttpURLConnection x=(HttpURLConnection)new URL("google.com/search?sclient=psy-
ab&h1=en&site=&source=hp&q=ip%20address&btnG=Search").openConnection();
And then open an input stream, read it in, and see one of the vpn addresses in the page, after the html: "Your public IP address is "
You can't without calling to an external service.
You will need to have your application call something like http://www.whatismyip.com/ and parse out the IP address the wider internet sees you as. There is probably a REST service you can call which gives you the answer back as XML/JSON/something more computer parseable.
Unfortunately, your local machine does not have enough information for Java to be able to find out without going external.

Java Login Username on Remote Machine

I'm working on a very simple file sharing program in LAN networks using Java. The software uses Socket ans ServerSocket classes to send and receive to/from machines which user knows the IP addresses of.
I want to improve the software in a way that user can enter the other's machine username instead of IP, for example if the user of the other machine which is trying to connect to this machine is a specific username, so the connection is allowed.
A solution would be maintaining some form of username -> IP (or hostname) mapping. This could be done by a DHCP server, but that's a bit of a overkill (or impossible if you cannot set up a DHCP server), so I recommend your program maintains a mapping itself.
To build the mapping, you could try this: When a host goes up, it could send a LAN broadcast message with its username to notify its presence. Upon receiving the notifications, the other hosts would register sender host's IP and username, then answer with their respective user names and IP addresses.
You got to make separate application that will run on computer that is always on and will keep list of IPs associated with Usernames. Your file sharing application will connect to that "server" asking for username and in return it will return IP that your filesharing app use to connect to other computer.

Categories