[英]Chat client-server with file transfer and hybrid cryptography
I'm trying to realize a chat client-server (in local) that permits to exchange text and files.我正在尝试实现一个允许交换文本和文件的聊天客户端服务器(在本地)。 I utilize
java.security
and java.crypto
to implement hybrid cryptography (and the tool Swing).我利用
java.security
和java.crypto
来实现混合加密(和工具 Swing)。 I exchange text in a serialized way (using ObjectInputStream
and ObjectOutputStream
), inserting it (after I crypted it with an apposite function) in byte[]
format in Message (that is an object that i created and that is effectively exchanged between sockets):我以序列化的方式(使用
ObjectInputStream
和ObjectOutputStream
)交换文本,将它(在我用合适的函数对其进行加密之后)以byte[]
格式插入 Message(这是我创建的一个对象,并在套接字之间有效交换):
import java.io.Serializable;
public class Message implements Serializable{
private static final long serialVersionUID = 1L;
byte[] data;
Message(byte[] data){
this.data = data;
}
byte[] getData(){
return data;
}
}
Chat works fine until I exchange only Message
.聊天工作正常,直到我只交换
Message
。 Now I'm trying to implement file transfer (first I tried to implement file transfer from server to client, without cryptography), therefor I took a file "selectedFile" with FileChoser and I sent it to the client thanks ObjectOutputStream
's method writeObject(selectedFile)
.现在我正在尝试实现文件传输(首先我尝试实现从服务器到客户端的文件传输,没有加密),因此我使用 FileChoser 获取了一个文件“selectedFile”,并将其发送给客户端,感谢
ObjectOutputStream
的方法writeObject(selectedFile)
。 On the client side I recognized if the object that is arrived is File
or Message
with:在客户端,我识别出到达的对象是
File
还是Message
:
class ListenFromServer extends Thread{
public void run(){
while(true){
try{
if((Message.class.isInstance(sInput.readObject()))==true){ //I verify if the recived object belongs to Message class
m=(Message) sInput.readObject();//sInput is an instance of ObjectInputStream class, connected to client socket's InputeStream
decryptMessage(m.getData()); //function that decrypts the content of m and inserts the result in a String that after I append in a text area
}
else
{
File recivedFile= (File) sInput.readObject();
File saveFile=new File(path+"/"+ recivedFile.getName());
save(recivedFile,saveFile);//function that insert the recived file in a specific folder
System.out.println("file ricevuto");
}
} catch (ClassNotFoundException ex) {
System.out.println("INFO: Classe non trovata durante la lettura dell'oggetto Messaggio: " + ex.getMessage() + "\n");
} catch(IOException e){
System.out.println("Connection closed");
break;
}
catch(NullPointerException ne){
ne.printStackTrace();
ne.getCause();
System.out.println("Errore"+ ne.getMessage());
}
The problem is that file is recived by the client only clicking twice server's "sendFile" button , moreover now this problem regards also the action of sending text to client, because client receives Message
object only when I send it twice (I use two different methods to send a Message
object and a File
object).问题是客户端只点击两次服务器的“sendFile”按钮就接收到文件,而且现在这个问题也涉及向客户端发送文本的动作,因为客户端只有在我发送两次时才会收到
Message
对象(我使用两种不同的方法发送一个Message
对象和一个File
对象)。
This problem doesn't occur when I eliminate instruction:当我消除指令时不会发生此问题:
if((Message.class.isInstance(sInput.readObject()))==true){
...
}
I ask you how to overcome this problem, or if there is a better way to distinguish File and Message objects in reception.请问如何克服这个问题,或者有没有更好的方法来区分接收中的File 和Message 对象。
sInput.readObject()
This is an order to read an object.这是读取对象的命令。 You give this twice in sequence, so that's two requests to reads different objects.
你按顺序给出两次,所以这是读取不同对象的两个请求。
To fix this, just read the object once, test the type of the object, and cast it when appropriate:要解决这个问题,只需读取对象一次,测试对象的类型,并在适当的时候强制转换:
Object inputObject = sInput.readObject(); // Read the object once
if (inputObject instanceof Message) { // If the object is a message
Message m = (Message) inputObject; // cast as a Message
... // use the Message m
} else if (inputObject instanceof File) { // else if it's a file
File f = (File) inputObject; // cast as a File
... // use the File f
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.