currently I am working on a school Project. The goal of this project is to receive data from a Server and writing data to the Server. For this purpose I am using Sockets and ServerSockets. To encrypt the messages I am using Apache Commons Codec 1.9.
(Server is a java .jar file executed on an Ubuntu Server and the client is an android app)
Server/Client
To encrypt the messages I am exchanging public keys.
private void Schluesselaustausch() {
try {
GenerateKeys gk = new GenerateKeys(4069);
gk.createKeys();
ServerPrivateKey = gk.getPrivateKey();
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
oos.writeObject(gk.getPublicKey());
oos.flush();
ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
Object obj = ois.readObject();
ClientPublicKey = (PublicKey) obj; //Line 108
} catch (Exception e) {
e.printStackTrace();
}
}
The error occurs when an object is casted to an PublicKey (Line 108).
The Client has the same function, but he is receiving and then sending an object.
The Error
java.lang.ClassNotFoundException: com.android.org.conscrypt.OpenSSLRSAPublicKey
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:374)
at java.base/java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:685)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1879)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1765)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2053)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1587)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at Engine.ClientVerbindung.Schluesselaustausch(ClientVerbindung.java:108)
at Engine.ClientVerbindung.run(ClientVerbindung.java:49)
There is more
Now to the strange part: Everything works if I am using a client running on PC. So I figured out that my server was coded on Java 10 but on my Ubuntu Server there was Java 11 installed. I downgraded Java and tested it again. Nothing changed.
My Questions
Is there an error in Android?
Do I have to upgrade java to 11?
What is the problem?
Thank you for your help.
RT
You are not sending a key (some string or number value) but the whole Java object back and forth through the Object*Streams. When you use a PC you are basically using the same Java implementation as the server, so the server can deserialize this Java object. If you System.out.println the .getClass() of the received object on the server, you will find that it does not talk about com.android.....
Yet, when you connect from your mobile device, the Android Java version is used and it obviously contains Android specific objects of class com.android.org.conscrypt.OpenSSLRSAPublicKey. The server does not have these class definitions anywhere in its library, so it cannot deserialize them.
Rather then de-/serializing the Java key objects, you should send the plain keys around.
Related
I'm trying to create application that will send object through local network using Sockets. When i run server and client code in Intellij Idea they work fine, but when i run server code on one pc and client code on another pc i get errors like java.io.StreamCorruptedException: invalid type code: 00 or java.io.StreamCorruptedException: invalid stream header: 6C69656E
byte[] readBuffer = new byte[4096];
int num = inStream.read(readBuffer); //inStream is socket input stream
ByteArrayInputStream bis = new ByteArrayInputStream(readBuffer);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject(); //this line throws error
The thing is that writing and reading object to socket stream works on server (which is on pc where i created project) but reading from input stream on client (another pc where i copied project) throws error.
Can someone help me with this? I searched everywhere for solution but i can't figure out what is problem with serializing, because it works on same pc but won't on another. Is there any way that i can make this pc independent? This also happens when i create jar files and run it on same pc where it works in Intellij Idea.
It can because that client didnt read message fully.
But the real mistake is that you work with TCP socket like a message protocol transport but TCP is a stream protocol so you have to create your own message protocol on top of TCP.
Why it works fine on local system?
Because transport data between client and server happen too fast in local test and maybe in just one frame so all the message transported in just one IO-call but in internet or a network it doesn't work like you think.
There is 2 way to handle this mistake:
1- Pass SocketInputStream directly to ObjectInputStream instance and let it handle read objects.
2- Create a message protocol for example you can put the size of message in 2 or more first bytes. Then you can workd like this :
Read 2(or more) first bytes and detect size of packet.
Create a buffer for this size and read packet bytes.(make sure you read all of packet data from socket . You can use return value of SocketInputStream.read(byte[]) method to calculate it)
Pass the packet to ObjectInputStream and read object !
We are building a websocket webapp for a school project and would like to store the incoming information to MySQL. Currently we are using netbeans and the Server Endpoint is written in Java. The info sent from the client side is a JSON obj. We can decode it but dont't know how to insert it into the table
Problems we are facing
1.We are used to programming Java application and using mysql JDBC but this time we cannot find the library to add, like we use to do.
2.We have tried AJAX but since the PHP is on a different server we cant do it, and I can't find the option to add a PHP file to the current project in netbeans. -- Currently I'm trying to learn how to do AJAX with JSP
3.We think we need to spin a thread so that the reply doesn't have to wait for the insert to complete but when we try to spin a thread the Endpoint no longer works, so we commented it out
4.We are trying to keep third party Frame works to a minimum
Below is the code to our Server Endpoint
public void onMessage(String message, Session session) {
// Create JsonObject from message
JsonObject jsonObject = new Message(message).getJObject();
// Decode JsonObject
message = Decoder(jsonObject);
for (Session peer : peers) {
try {
if (!peer.equals(session)) {
peer.getBasicRemote().sendText(message);
}
} catch (IOException ex) {
Logger.getLogger(ServerEndpoint.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This is our current decoder. We wanted to spin a thread from this function, I don't know if that is a good idea or not
public String Decoder(JsonObject jObject) {
String message;
message = jObject.getString("msg");
return message;
}
Since Netbeans use Glassfish, I downloaded the MySQL JDBC and put it in the lib folder as I would if I was running Tomcat. Then I go into the Admin console and added the JDBC Connector then added the JDBC Resource. Next restart the server then redeploy the app.
I have a server and client applications.
The client and server are in the same machine.
The client and server are using same java version.
I'm using ObjectOutputstream /objectInputStream to write and read
the objects
The transmitted object is notbeing modified while writing
the code :
mySocketClient.getOos().writeObject((replyobject));
mySocketClient.getOos().flush();
mySocketClient.getOos().reset();
Reading Code :
objectInputStream.readObject();
The application is working good but randomly I have the below exception
java.io.StreamCorruptedException: unexpected end of block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
What might be causing this?
I'm writing a Java code which have to send some data to an elecronic system and to receive some data from it through wireless. The electronic system is made of PIC32 and RN-171 module. I'm now trying to connect to the RN-171 network and to send and receive some data. Although I can in my java code set up an OutputStream and send some data to the RN-171 properly, I can't set up an InputStream and my app launches the following exception:
java.io.StreamCorruptedException: invalid stream header: 2A48454C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at TestController.sendParametersToWirelessModule(TestController.java:44)
at TestController.main(TestController.java:30)
The code in my java app, which generates the exception is:
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("1.2.3.4", 2000);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
--> in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
sendMessage(message); }
(The arrow indicates the code line which generates exception)
Is there a solution? Could anyone help me please?
Thanks
Use the following code instead:
out = requestSocket.getOutputStream();
in = requestSocket.getInputStream();
ObjectOutputStream/ObjectInputStream are used to serialize/deserialize Java objects. There is also no point in flushing the output stream before writing to it.
I'd like to establish a server(Java)/client (Matlab) communication using socket. They can send messages to each other. An example shows how to do this in Java server and Java client, http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html.
When I try to rewrite the client part in Matlab, I only can get the first message that the Java server sends and display it in the Matlab command window.
When I type a message in the Matlab command window, I can't pass it to the Java Server.
Jave code:
kkSocket = new Socket("localhost", 3434);
Matlab equivalent:
kkSocket = Socket('localhost', 3434);
Java code for client:
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
What would be a Matlab equivalent for this? Thanks in advance.
For the input stream:
input_stream = input_socket.getInputStream;
d_input_stream = DataInputStream(input_stream);
For the output stream:
output_stream = output_socket.getOutputStream;
d_output_stream = DataOutputStream(output_stream);
If you are trying to use MATLAB and the Java application on the same machine then matlabcontrol may do everything that you are looking for. It automatically establishes a connection to a session of MATLAB. It uses Java's Remote Method Invocation under the hood which makes use of sockets. matlabcontrol is designed specifically to only enable communication on localhost; the sockets it creates will not accept remote connections due to the security issues that could allow. However, if you need to allow remote connections you may find parts of matlabcontrol's source code to be useful.