I am confused on how the client actually makes the connection to the server if the server is remote(not on the same machine as the client). My code works fine using localhost but i cant figure out how the client actually makes the connection to the server host so that it looks up the rmiregistry. I'm confused on what gets stored in the registry for the server, is it Sample or localhost? This may be dumb but i tried to convert localhost to its ipaddress on the client side and do String url = "//" + server + ":" + 1099 + "/Sample"; where server is the ip from getbyname() but i get a exception: java.rmi.NotBoundException: 127.0.0.1:1099/Sample
That was with the client and server on both machines. I'm just trying to figure out how the two connect remotely but it didn't even work on the same machine using the ip address of localhost.
Client:
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
String url = "//" + "localhost" + ":" + 1099 + "/Sample";
SampleInterface sample = (SampleInterface)Naming.lookup(url);
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
Server:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) throws IOException {
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
String url = "//localhost:" + 1099 + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
The server should bind to a Registry running on 'localhost'.
The client should lookup a Registry at the server host.
It's as simple as that.
I'm confused on what gets stored in the registry for the server, is it Sample or localhost?
Neither. You're confusing three different things:
The hostname, in this case 'localhost'.
The bind-name, in this case 'Sample'.
The object which is bound, which is the remote stub.
Related
I have websocket server class where I want to print when user connects. It works without a problem, however if I want to execute a method outside of this websocket server, it doesn't print and closes the client's connection and prints out java RuntimeException error.
package com.rupla.myapp.endpoint;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.rupla.myapp.encoders.MessageEncoder;
import com.rupla.myapp.http.MyOtherClas;
import com.rupla.myapp.encoders.MessageDecoder;
#ServerEndpoint(value = "/chatnow", encoders = MessageEncoder.class, decoders = MessageDecoder.class)
public class WebSocketServer {
#OnOpen
public void onOpen(Session session) {
extracted(session);
}
private void extracted(Session session) {
System.out.println("Session Opened (Client to Server) - ID: " + session.getId());
}
#OnMessage
public void handleTextMessage(String message, Session s) throws IOException {
System.out.println("New Text Message Received: " + message + " - From session: " + s.getId());
//So here we send client's message to the client - This works
s.getBasicRemote().sendText("Server: " + message);
try {
// Here I try to print "hello" but from 'MyOtherClas'.class using printTest() method
MyOtherClas.printTest();
//Right after this code, server closes the connection to the client and doesn't print anything
}
catch(Exception e) {
System.out.println("fail: "+e);
}
}
/**
* If I would call this method below, it would work since it's in the same class (but why?)
*/
private void printInsideClas() {
System.out.println("hello");
}
#OnClose
public void onClose(Session session) throws IOException, EncodeException {
System.out.println("Session Closed");
}
#OnError
public void onError(Throwable t) {
System.out.println("onError: " + t);
}
}
Pretty basic websocket server for java. However when I run this on my tomcat 9.0 and connect with javascript client and try to send message from my client to the server. It closes the connection between server and client and then prints this error.
Session Opened (Client to Server) - ID: 4
New Text Message Received: asd - From session: 4
onError: java.lang.RuntimeException: org/apache/http/Header
Session Closed
I tried to google this for few days for now but still I have no answers, I really need help with this.
The exception is in a server-side webservices stack. The exception is saying that it cannot load a core class that is part of Apache HTTPComponents.
Here are some possible explanations:
Your server-side runtime is missing the JAR file.
The JAR is present, but it is not on the right runtime classpath.
The Header class depends on some other class that has failed class initialization. If this has happened, there should be evidence earlier in the logfile.
The JAR file you should be looking for is "httpcore-.jar" or the equivalent from the old Jakarta project.
Figure out which of the above has happened, and you are close to solving the problem.
I have a server code in Java which I run on my machine and my friend has a client code which runs on his machine. When he enters my IP so as to connect to my server and get the date, connection fails and nothing happens. Note that when I run server and client programs on my own machine and enter localhost as the address, connection is successful and I get the date message correctly. I'm looking for possible errors and problems causing this.
Server code in Java:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9999);
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}
}
Client code in Java:
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9999:");
Socket s = new Socket(serverAddress, 9999);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
Some routers might isolate computers in different networks. Try it with both computers on Wifi or both wired to the router. Are your IPs on the same network? Can you see your friend's computer on the network? There might also be some security configurations on your router.
Other than that and firewall issue (which you have disabled), the code looks like it should work fine.
I want to test this simpel server-client application on my own machine at home. How can I run this in Eclipse and then see if the other side can see my message. I want to at some point be able to make a chat window that anyone could have on their machine and send messages to anyone that is online that is linked into the chat window.
But first I have to be able to see that I have a connection. Should I install a server on my computer, or someone told me that there was a server already installed on my computer but I just had to have windows turn it on. (Windows 7)
Question: How can I test this client-server on my computer at home?
Code:
Client Side:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;
public class DateClient {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
Server side:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}
}
Code I want to Add for new message:
out.println("Hello Doug, how are you!);
This will not show in my message box when it shows up on the screen. Is 127.0.0.1 always the IP address that needs to be entered when testing from eclipse or how would I change this around so that I could let the user determine their own IP address.
You can just open two terminals. For the DateClient, just use localhost or 127.0.0.1 as the address. If you really must use Eclipse, then you can run one of the program from Eclipse and the other from a terminal.
You don't need any server.
In Eclipse (assuming your coding is correct), you can run multiple programs (Java files with a main method) simultaneously.
First, say DateServer -> Run As -> Java Application
Next, say DateClient -> Run As -> Java Application
To run this, you don't need any additional server, but Win 7 might ask you for permissions to unblock these programs from accessing the network. You should say yes to these permissions.
If you want to get a feel of separate client and server, this might be better
Keep the DateClient and DateServer in two different projects
Compile both projects as JARs
Open two different DOS consoles and run these two applications in these separate DOS consoles.
Client Side :
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClientSide {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 1234);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Hello");
} catch (IOException ex) {
System.out.println("Connection failed");
}
}
}
Server Side :
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
public class ServerSide {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(1234);
System.out.println("waiting for connection...");
DataInputStream dis = new DataInputStream(ss.accept().getInputStream());
System.out.println("Successfully Connected\n" + dis.readUTF());
} catch (IOException ex) {
System.out.println("Server Not Started : " + ex);
}
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
public class DNSLookup
{
public static void main(String args[])
{
String host = "www.google.com";
try
{
InetAddress inetAddress = InetAddress.getByName(host);
// show the Internet Address as name/address
System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());
// get the default initial Directory Context
InitialDirContext iDirC = new InitialDirContext();
// get the DNS records for inetAddress
Attributes attributes = iDirC.getAttributes("dns://8.8.8.8/www.google.com", new String[] {"A"});
// get an enumeration of the attributes and print them out
NamingEnumeration<?> attributeEnumeration = attributes.getAll();
System.out.println("");
while (attributeEnumeration.hasMore())
{
System.out.println("" + attributeEnumeration.next());
}
attributeEnumeration.close();
}
catch (UnknownHostException exception)
{
System.err.println("ERROR: Cannot access '" + host + "'");
}
catch (NamingException exception)
{
System.err.println("ERROR: No DNS record for '" + host + "'");
exception.printStackTrace();
}
}
}
If I run this code, I get error like below...
www.google.com 74.125.128.103
ERROR: No DNS record for 'www.google.com'
javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'www.google.com'
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:300)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:430)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:231)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:139)
at com.sun.jndi.toolkit.url.GenericURLDirContext.getAttributes(GenericURLDirContext.java:103)
at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
at DNSLookup.main(DNSLookup.java:24)
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:121)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:786)
at com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:411)
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:203)
... 7 more
But if I use "dns:/www.google.com" as query, not "dns://8.8.8.8/www.google.com", it works perfectly without any error.
Error only happens when I try to specify DNS server to use.
"dns://8.8.8.8/www.google.com" -> ERROR
"dns:/www.google.com" -> Work!
http://docs.oracle.com/javase/7/docs/technotes/guides/jndi/jndi-dns.html
In this document, there is exact same usase what I want to use. I cannot understand why this makes problem.
DirContext ictx = new InitialDirContext();
Attributes attrs3 = ictx.getAttributes("dns://server1.example.com/host3.example.com",
new String[] {"MX"});
There is nothing wrong with your code, but I suspect that you're running it from behind a proxy or firewall that blocks outgoing DNS.
When you ask for dns:/www.google.com you're essentially asking your local DHCP-supplied resolver for the IP of www.google.com - just as when typing the address in the browser address line.
However, when using dns://8.8.8.8/www.google.com you're asking to use Google DNS to resolve google.com which requires that your proxy/firewall allows outgoing TCP/UDP port 53 traffic, which is usually not the case in corporate environments...
Cheers,
I am trying to run my server named SampleServer. I am using windows and this is what i did:
in cmd:
javaw rmiregistry 1099
cd C:\Users\Home\workspace\RMI\src
java -Djava.security.policy=policy SampleServer 1099
i get the following error:
binding //localhost:1099/Sample
New instance of Sample created
Sample server failed:Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
I've tried using a different port # such as 4719 for rmiregistry but i receive the same error. I made sure that my firewall was disabled but the problem persist. I made sure that the port is not already being used. I really hope someone can help me.
Picture of my desktop with folders of project, eclipse window and cmd open:
http://s22.postimg.org/uq00qzslr/picyture.png
Code:
SampleServer:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("usage: java SampleServer rmi_port");
System.exit(1);
}
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
// first command-line argument is the port of the rmiregistry
int port = Integer.parseInt(args[0]);
String url = "//localhost:" + port + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
SampleClient:
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
try {
if (args.length < 3) {
System.err.println("usage: java SampleClient host port string... \n");
System.exit(1);
}
int port = Integer.parseInt(args[1]);
String url = "//" + args[0] + ":" + port + "/Sample";
System.out.println("looking up " + url);
SampleInterface sample = (SampleInterface)Naming.lookup(url);
// args[2] onward are the strings we want to reverse
for (int i=2; i < args.length; ++i)
// call the remote method and print the return
System.out.println(sample.invert(args[i]));
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
SampleInterface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SampleInterface extends Remote {
public String invert(String msg) throws RemoteException;
}
Sample:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.*;
// this is the class with remote methods
public class Sample
extends UnicastRemoteObject
implements SampleInterface {
private int a;
public Sample() throws RemoteException {
System.out.println("New instance of Sample created");
a = 1;
}
public String invert(String m) throws RemoteException {
// return input message with characters reversed
System.out.println("invert("+m+") a=" + a);
return new StringBuffer(m).reverse().toString();
}
}
policy:
grant {
permission java.security.AllPermission;
};
javaw rmiregistry 1099
Stop right there. This is already wrong. 'rmiregistry' is an executable, not the name of a Java class you can execute with 'java' or 'javaw'. Just use 'rmiregistry'.
This error occurs when there is no service running on the port, you are trying to connect. As said by EJP, rmiregistry is a tool which can be started by rmiregistry & in the background (JDK 7). I would recommend you that you check your firewall or connectivity issue with the port.