hi i'm new in android developing and i want to write an application which use signalR java-client. in first step i did the answer of this and here is my client code:
Platform.loadPlatformComponent(new AndroidPlatformComponent());
String host = "localhost";
HubConnection connection = new HubConnection( host);
HubProxy hub = connection.createHubProxy("HubConnectionAPI");
SignalRFuture<Void> awaitConnection = connection.start(new LongPollingTransport(connection.getLogger()));
try {
awaitConnection.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
hub.subscribe(this);
try {
hub.invoke("DisplayMessageAll", "message from android client").get();
System.out.println("sent!!!");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
and u can download the server code from here
i have following error with awaitConnection.get();
error:
W/System.err: java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
i also have this error:
Caused by: microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException:Invalid status code: 404
can anyone please help me? i searched a lot but i didn't found anything helpful for me
EDIT:
clients can access the hub via this but how can i implement on android so my application can connect?
this is the log file on server:
2015-11-11 09:05:08 10.2.0.18 GET /signalr/negotiate clientProtocol=1.3&connectionData=%5B%7B%22name%22%3A%22hubconnectionapi%22%7D%5D 80 - 10.2.0.253 SignalR+(lang=Java;+os=android;+version=2.0) - 404 0 2 3
changed the String host = "localhost";
to String host = "localhost/signalr";
localhost didn't work for me, i had to deploy the asp.net web application on IIS, allow the port in Firewall inbound rules and put it in signalr config in android.. hope this helps someone... Cheers!
Related
I managed to setup a client and server connection using Java socket. After checking the connection had been establish, I tried sending Protocol commands that are provided by the SDK from the server and I'm using a JButton to execute the commands.
Examples of the commands are play, stop and ping the server.
The code below shows how I setup the connection and send the protocol commands
public void socket1()
{
Socket MyClient;
try {
MyClient = new Socket("192.168.10.61",9993);
os = new DataOutputStream(MyClient.getOutputStream());
is = new DataInputStream(MyClient.getInputStream());
lblerror.setText("Connected");
MyClient.getOutputStream().write("play".getBytes("US-ASCII"));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
lblerror.setText("Don't know about host: hostname");
} catch (IOException e) {
// TODO Auto-generated catch block
lblerror.setText("Couldn't get I/O for the connection to: hostname");
}
}
After pressing the button on the GUI, The server did not response to the command 'play' and there is no error.
As Scarry Wombat wrote, use os.write()
But instead of .write() use .writeBytes(),
because you want to send a byte array(?). Alternatively you could send a String with UTF-encoding by using .writeUTF()
I have an application on Openshift I'm trying to connect to MySQL DB my code is :
private static final String URL = "jdbc:mysql://127.11.240.130:3306/"
+ DB_NAME;
public static String initConnection() {
if (connection == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, USER_NAME,
PASSWORD);
return "Connection Initialized!";
} catch (SQLException e) {
e.printStackTrace();
return e.getMessage();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
return e.getMessage();
}
}
return "Connection Initialized!";
}
and in index.jsp code is :
<p><% out.print(""+com.measyou.DbManager.initConnection()); %> </p>
this code gives me
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
and Caused by :
Caused by: java.net.ConnectException: Connection refused: connect
I have referred This Link and This one as well, but Im unable to solve this problem. Please Help me in solving this one.
Is this a scaled application, or is it running in a single gear? Either way, you should be using OPENSHIFT_MYSQL_DB_HOST and OPENSHIFT_MYSQL_DB_PORT instead of hard-coding the IP and port. And are you sure the mysqld is running? Run the following:
ps -ef | grep mysqld
and you should see running processes. Then run:
netstat -plnt | grep $OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT
if it's running properly, you should see a line like this:
tcp 0 0 127.11.240.130:3306 0.0.0.0:* LISTEN 459740/mysqld
if the process is not running, or it's not listening on the port as expected, then check the mysql logs in ~/mysql/log/ for errors.
In java RMI i am building a chat application. But i am not able to figure out a way to find out, whether the IP which is hitting my server is from my internal organization network(INTRANET) or from external world(INTERNET).
Right now i am using
try {
System.setProperty("java.rmi.server.hostname",InetAddress.getLocalHost().getHostAddress());
Registry statusRegistry = LocateRegistry.createRegistry(ChatConstants.statusPort);
ChatInterface chat = new ChatImpl(ChatConstants.statusPort) ;
statusRegistry.rebind("statusconnection",chat);
System.out.println("RMIStatusConnection Server is started...");
} catch (RemoteException e) {
System.out.println("RMIStatusConnection failed...");
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
Try RemoteServer.getClientHost(), but it may only provide the address of the nearest NAT device.
NB there's not much point in setting java.rmi.server.hostname like that. That's the default. You only need to set it if there's something wrong with the default setting.
I am trying to connect to my website's MySQL database, and I have no knowledge of PHP so I decided to use JDBC. I followed some video tutorials (non JDBC) and I used their steps. I skipped the MAMP step though because I am not hosting the server off of my PC. It is being hosted locally because it is going to be a larger website.
So I have this code entered in my Login Activity (first screen you see):
Connection connection = null;
Statement statement = null;
String username = "username";
String password = "password";
String dbURL = "jdbc:mysql://216.26.176.52:3306/lifesizefoto";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(dbURL, username, password);
statement = connection.createStatement();
System.out.println("Connected.");
} catch (ClassNotFoundException error) {
System.out.println("Cannot connect");
} catch (SQLException error) {
System.out.println("Error: " + error.getMessage());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) { try {connection.close();} catch (SQLException ignore) {} }
if (statement != null) { try {statement.close();} catch (SQLException ignore) {} }
}
I have tried many variations to the .getConnection() statement, but I can't figure it out. I have also contacted the website host and he took down all firewalls for my IP and even opened up a special port for the app.
When I run my app, I get this error:
01-09 18:59:32.769: I/System.out(14178): Error: Communications link failure
01-09 18:59:32.769: I/System.out(14178): The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I would appreciate any help. Thank you in advanced!
Two thoughts:
It's unlikely that your web site's MySQL server is bound to an external interface - it's likely only listening on the localhost interface. Your hosting provider should be able to confirm / possibly fix that for you.
Trying to connect a mobile app directly to a server database is probably not going to work well in the long run - I'd suggest that you either figure out how to write a server-side app (for your mobile app to connect to) in PHP, or find another language that your host supports, and do it in that.
I have a small Java RMI Server and Client program I'm writing. I have spent some time trying to figure out the error messages without success.
The Client generates the following error:
Trying to connect to: 127.0.0.1:3232
ERROR!!!: StockClient: main: Could not connect to the server: java.rmi.UnmarshalException: Error unmarshaling return header; nested
exception is:
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:209)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at StockClient.StockClient.main(StockClient.java:44)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:195)
... 3 more
With the server the following error only when the client attempts to connect.
this address=localhost/127.0.0.1,port=3232
Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlException: access denied
(java.net.SocketPermission 127.0.0.1:62586 accept,resolve)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:549)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkAccept(SecurityManager.java:1157)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(TCPTransport.java:636)
at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(TCPTransport.java:275)
at sun.rmi.transport.Transport$1.run(Transport.java:158)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Because of the server error I'm fairly sure its a security or registry error, security policy for the server is:
grant {
permission java.security.AllPermission;
};
and being run with the following argument
-Djava.security.policy=client.policy
I've tried everything I can find but seem to keep going around in circles.
relevant methods:
Server:
public static void main(String[] args)
{//main(...) starts
// set up the data structures and data
//add users
//Users hard coded as this is an example proof on concept program
//Names and passwords not hashed for simplicity
User alpha = new User("Alpha", "AlphaPass");
User omega = new User("Omega", "OmegaPass");
users.add(alpha);
users.add(omega);
//startup the RMI server
try
{
System.setSecurityManager(new RMISecurityManager());
StockServer server = new StockServer();
StockServerInterface inter = (StockServerInterface)
UnicastRemoteObject.exportObject (server,0);
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry(thisPort);
registry.rebind("StockServer", inter);
}
catch (Exception e)
{
System.out.println("Unable to create StockServer: " + e);
e.printStackTrace();
System.exit(1);
}
}//main(...) ends
/**
* Constructor for StockServer
*
* #throws RemoteException
*/
public StockServer() throws RemoteException
{
//try to get the host machine's IP address
try
{
// get the address of this host.
thisAddress = (InetAddress.getLocalHost()).toString();
} catch (Exception e)
{
throw new RemoteException("can't get inet address. " + e);
}
//Set the port
thisPort = 3232;
//Print out the server address and port
System.out.println("this address=" + thisAddress + ",port=" + thisPort);
}
Client:
private static StockServerInterface stockServer;
static public void main(String args[])
{
Registry registry;
//get the server address from the args
String serverAddress = args[0];
//get the server port from the args
String serverPort = args[1];
//Let the user know we are about to try to connect
System.out.println("Trying to connect to: " + serverAddress + ":" + serverPort);
try
{
// get the registry
registry = LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue());
// look up the remote object
stockServer = (StockServerInterface) (registry.lookup("StockServer"));
//Authenticate the user
authenticate();
//setup the hashset
HashSet<Stock> hashStockSet = null;
//setup the hashset of desired stocks
try
{
hashStockSet = getHashSet();
} catch (IOException e)
{
e.printStackTrace();
System.exit(1);
} catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
//bit of a heavy handed infinte loop so we continue to get the loop
while(true)
{
//Run the ticker
ticker(hashStockSet);
}
// call the remote method
}
catch (RemoteException e)
{
System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
e.printStackTrace();
}
catch (NotBoundException e)
{
System.out.println("ERROR!!!: StockClient: main: Could not connect to the server: "+e);
e.printStackTrace();
}
You don't need a SecurityManager in an RMI server unless the client is relying on the codebase feature to supply the server with classes. Either remove it, or debug the .policy file. Clearly the one you've written isn't being loaded.
Run your server with -Djava.security.debug=access,failure and you will see where all the security domains are getting their configurations from, and the domain that is failing at the point where the exception is thrown.