简体   繁体   中英

Sending objects using sockets - JAVA

So I am creating an application using sockets. And I have the client who sends a message and it passes to the server using writeObject(new String("Name|Message")); .

And I read the message in the server using readObject();

I am trying to pass this string object to an array. But I get [Ljava.lang.String;@6bb9ae1a .

Here is what I am trying:

ObjectInputStream saida = new ObjectInputStream(client.getInputStream());
String[] read = saida.readObject().toString().split("|");
System.out.println(read);

I tried also to make variables for each split:

   String readm = read[1];
   String readn = read[0];

But it returns me "" as the name and "A" as the message (?)

Ow, and the socket is working, because if I do (String) saida.readObject(); it returns me the normal string.

use like that :

saida.readObject().toString().split("\\|"); 

and then

String readm = read[1];
String readn = read[0];

Because pipe symbol is the special character and splitting special chars is different. And you cannot use systemoutprintln to print the string array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM