![]() |
|
[Java] Java Client-Server socket sending/receiving - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Java Client-Server socket sending/receiving (/showthread.php?tid=5679508) |
Java Client-Server socket sending/receiving - +Ervinas - Jul 27, 2017 Hello, got a huge problem. Stuck here for a week now.. Need help with my code a bit. I'm trying to send object from client to server. Steps: 1. Run server 2. Run client 3. Connection - OK. 4. Server creates thread that waits for client input. 5. Client sends AUTH information to server , server RECEIVES and checks it. 6. Auth completed, client joined server. 7. Client sends String[] object to server 8. Server gets it and thows me error : Code java.io.StreamCorruptedException: invalid type code: ACAfter all googling and reading, i know that 1 socket can have only 1 ObjectInputStream and 1 ObjectOutputStream on each sides (client/server). I double checked my ServerComm.java and ServerThread.java if there are any more, but there arent! Everything is fine. Don't know what to do. ServerComm.java : It's class from Client side, to communicate with server. Code package client;ServerThread.java : it's a thread that accept client connections and check auths and waiting for new String[] object from client to accept. Code package server;P.S. This method gets me error. Code private static String[] getInputFromClient() throws ClassNotFoundException, IOException {Code String[] temp = (String[]) ois.readObject();* - matt - Jul 27, 2017 This error occurs because you're initializing a new ObjectOutputStream each time you want to send data, you want to use only 1 throughout the life of the socket. Also I noticed that when you're recieving data its good practice to not cast it right away as what you're recieving may not be what you want so I'd do something like Code Object o = ois.readObject();* - +Ervinas - Jul 27, 2017 (Jul 27, 2017 01:20 AM)Midnight Blue™ Wrote: This error occurs because you're initializing a new ObjectOutputStream each time you want to send data, you want to use only 1 throughout the life of the socket. Also I noticed that when you're recieving data its good practice to not cast it right away as what you're recieving may not be what you want so I'd do something like I know what you said about socket and one ObjectOutputStream, i do use one only. Am i understanding usage? And is it possible to make them work faster? Like flushing socket and receiving? * - matt - Jul 27, 2017 (Jul 27, 2017 01:34 AM)+Ervinas Wrote: I know what you said about socket and one ObjectOutputStream, i do use one only. Am i understanding usage? Every time you call sendAuth() you're initializing a new stream * - sexyPudding - Jul 27, 2017 I would just use JSON and just use the http protocol for almost all communication * - matt - Jul 27, 2017 (Jul 27, 2017 01:00 PM)sexyPudding Wrote: I would just use JSON and just use the http protocol for almost all communication that's lame with a lot of unnecessary overhead * - sexyPudding - Jul 29, 2017 (Jul 27, 2017 01:56 PM)Midnight Blue™ Wrote: that's lame with a lot of unnecessary overhead If you are only sending data betwen Java JVMs, then either choice is possible. A textual representation (XML, JSON, or custom) has several advantages: it's easier to make it interoperable between Java and other languages it's less brittle in the face of version changes or slightly different versions of your code at each end of the socket it's vastly easier to test and debug Depending on the format, it may be a little slower, but this often not significant. * - matt - Jul 29, 2017 (Jul 29, 2017 01:26 PM)sexyPudding Wrote: If you are only sending data betwen Java JVMs, then either choice is possible. I like your point that it's easily usable between languages but I could argue that it's good practice to use proper protocols when applicable so you don't fall in to bad habits. :P |