Note that there are some explanatory texts on larger screens.

plurals
  1. POjava client server serialization problem
    primarykey
    data
    text
    <p>hi i am working on a client server programme with containing some special functions like sending private message, show online list etc. so i know i must use serialization and first i managed it but after a while a screwed up :) nowadays i spending time for learn to serialize. i will share only meaningfull parts for prevent comlpexity. and i wanna learn where i making wrong. so thanks for your help anyway. here is a part of server code;</p> <pre><code> public class Server { private ServerSocket ss; private Socket socket; private Map&lt;Socket,DataOutputStream&gt; list = new HashMap&lt;Socket,DataOutputStream&gt;(); private LinkedList&lt;Person&gt; client_list = new LinkedList&lt;Person&gt;(); private String socketName; private Object lockObj = new Object(); public Server(int port_number) throws IOException{ create_Server(port_number); } public static void main(String[] args) throws IOException { int port_number=23; new Server(port_number); } private void create_Server(int port_number) throws IOException{ ss = new ServerSocket(port_number); System.out.println("Server is ready!"); while(true){ socket=ss.accept(); System.out.println(socket.getLocalAddress().getHostName() + " was connected!"); send_con_mes(); list.put(socket,new DataOutputStream(socket.getOutputStream()) ); ServerThread st = new ServerThread(socket,this); Person per = new Person(socket.getInetAddress().toString()); client_list.add(per); st.start(); } } public LinkedList&lt;Person&gt; send_list(){ return client_list; } </code></pre> <p>so i am creating the server and waiting for response for any socket. moreover i used list for save the socket and its output and clien_list saves Object person (person is a serializable object ).</p> <p>here is serverthread part</p> <pre><code>public class ServerThread extends Thread { private Socket s; private Server srv; private String socketName; private StringTokenizer str; private String message = ""; private LinkedList&lt;Person&gt; client_list; private ObjectOutputStream oos; private static int i=0; public ServerThread(Socket s,Server srv){ this.s = s; this.srv = srv; try { oos = new ObjectOutputStream(s.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void setList(){ client_list = srv.send_list(); } private LinkedList&lt;Person&gt; getList(){ return client_list; } @Override public void run() { String msg; String token; DataInputStream dis; try { dis = new DataInputStream(s.getInputStream()); while(true){ msg = dis.readUTF(); srv.send_to_All(msg, s); setList(); oos.writeObject(getList()); oos.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { srv.remove_Connection(s); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } </code></pre> <p>so i send client_list over on a <code>ObjectOutputStream oos</code> to clients.</p> <p>finally here is client part which takes the list and deserialize person object and read the information...</p> <pre><code>public class Client extends javax.swing.JFrame implements Runnable { private DataOutputStream dos; private DataInputStream dis; private Socket s; private String Client_name; private String Ip_addr; private Font font = new Font("Arial", Font.PLAIN, 13); private int click_num_b=0; private int click_num_i=0; private LinkedList&lt;Person&gt; client_list; private FileOutputStream fos; private PrintStream pts; private ObjectInputStream socketIn; /** Creates new form Client */ public Client() { initComponents(); Screen.setEditable(false); Text_Field.setFont(font); Screen.setFont(font); start_Chat(); } @Override public void run() { try { while(true){ read_list(); String message = dis.readUTF(); Screen.append(message + "\n"); } } catch (IOException ex) { Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); } } private void read_list() throws IOException{ socketIn = new ObjectInputStream(s.getInputStream()); try { client_list = (LinkedList&lt;Person&gt;) socketIn.readObject(); for (Iterator&lt;Person&gt; itr = client_list.iterator(); itr.hasNext();) { Person per = itr.next(); pts.println(per.getnickName() ); } socketIn.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void start_Chat() { try { Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : "); s = new Socket(Ip_addr, 23); Client_name = JOptionPane.showInputDialog("Enter your Nickname : "); dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); fos = new FileOutputStream("personList.txt"); pts = new PrintStream(fos); new Thread(Client.this).start(); </code></pre> <p>in here private <code>ObjectInputStream socketIn;</code> takes the serializable object and writes on a file. here is some errors which i am facing with</p> <pre><code>java.io.EOFException at java.io.DataInputStream.readUnsignedShort(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at ServerThread.run(ServerThread.java:58) SEVERE: null java.io.StreamCorruptedException: invalid type code: 00 at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at Client.read_list(Client.java:81) at Client.run(Client.java:61) at java.lang.Thread.run(Unknown Source) </code></pre> <p>so i appreciated if you can help me to handles this issue. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload