1

I have a client and server which I exchange messages and objects between.
My problem is when I send object with the help of toString and a string together. How do I pick out the object from the string and add the object to a list ; I wrote the code.

Server code:

import java.net.*;
import java.util.ArrayList;
import java.io.*;

public class Server  {

public static void main(String[] args) {
    ArrayList <PhoneBook> listPhone=new ArrayList<PhoneBook>(); 

    PhoneBook a = new PhoneBook("a","a","","",1);
    listPhone.add(a);
    PhoneBook pB = new PhoneBook();
    String ob;
    try{

        ServerSocket server = new ServerSocket(5555,50);

        System.out.println("Waiting Incoming Connection...");
        System.out.println("Local Address :"+server.getInetAddress()+" Port :"+server.getLocalPort());
        Socket sock = server.accept();

        ObjectOutputStream out =new ObjectOutputStream(sock.getOutputStream()); 
        ObjectInputStream  in =new ObjectInputStream(sock.getInputStream()); 

        String strin =(String) in.readObject();

        if (strin.equals("START")){ 
        out.writeObject("WAITING");
        out.flush();}
        strin =(String) in.readObject();
        String[] str=strin.split("\n");
        if(str[0].equals("REQUEST_SEARCH")){

               try{
                    // in this line is error
                   pB = (PhoneBook)in.readObject();  //String cannot be cast to PhoneBook
                   out.flush();
        }catch(ClassNotFoundException classnot){
          System.err.println("Data received in unknown format");}
               out.writeObject("RECORSDS"); 
               out.flush(); 
               String sName = pB.getsurName(); 
               for(int i=0;i<listPhone.size();i++) 
              if(listPhone.get(i).getsurName().equals(sName)){ 
              out.writeObject(pB.toString()); 
              out.flush(); 

        }else{
        out.writeObject("NXRECORD");
        out.flush();
        }}
        strin =(String) in.readObject();
        String[] st=strin.split("\n");

        if(st[0].equals("REQUEST_INSERT")){

            listPhone.add(pB); 
            System.out.println("The contact is add");
            out.writeObject("OK");
            out.flush();
        }
        out.flush();

        if(strin.equals("END")){ //bye = terminate the conversation

        in.close();
        out.close();
        sock.close();
        System.out.println("Connection Closing...");}
        }
        catch (Exception ex){
        System.out.println("Error during I/O");
        ex.getMessage();
        ex.printStackTrace();
        } } }

Client code:

     if (strin.equals("WAITING")) {
        System.out.println("The server says: " + strin);
        // out.writeObject("REQUEST_SEARCH\n");
        // out.flush();
        System.out.println("The server says: " + strin);
        System.out.print("Write the contact elements ");
        System.out.print("Write the name: ");
        String name = input.nextLine();
        System.out.print("Write the surname: ");
        String surName = input.nextLine();
        System.out.print("Write the job: ");
        String job = input.nextLine();
        System.out.print("Write the street: ");
        String street = input.nextLine();
        System.out.print("Write the phone number: ");
        int number = input.nextInt();
        PhoneBook p = new PhoneBook(name, surName, job, street, number);
        out.writeObject("REQUEST_SEARCH\n" + p.toString());
        out.flush();
    }

The error:

Connection reset
at java.net.SocketInputStream.read(Unknown Source)  at
java.net.SocketInputStream.read(Unknown Source)     at
java.net.SocketInputStream.read(Unknown Source)     at
java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)  at
java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) at 
       java.io.ObjectInputStream.readObject0(Unknown Source)    at
       java.io.ObjectInputStream.readObject(Unknown Source)     at Server.main

Application background:

This program is a PhooneBook with contacts (the server) and the client send first a START and the server take it and send WAITING after the client make a object phonebook which include name, surname, job, street and phone and sends it with the message REQUEST_SEARCH the server take the message and object (the contact) and search in array list with lastname if exist a contact, if exist send back the object and show the name, surname, job and other with toString() and message RECORDS then client send OK and server send back END and connection is closing if the contact is not exist the server send the message NXRECORD the client sent back a message REQUEST_INSERT the server take it and add the object in arraylist and sent OK And client send back END and closing connection.

5
  • If you're using eclipse, format your code using CTRL+SHIFT+F.. Commented Mar 16, 2013 at 20:21
  • This Exception is comming at client side? Commented Mar 16, 2013 at 20:25
  • From server is comming Commented Mar 16, 2013 at 20:28
  • Can you tell at which line? and if possible put the complete code.. Commented Mar 16, 2013 at 20:30
  • under of this commend is the error of eclipse // in this line is error Commented Mar 16, 2013 at 20:55

1 Answer 1

1

I don't know what you are sending to server after this line.

out.writeObject("REQUEST_SEARCH\n" + p.toString());

But I assume that you are sending a String to Server . Whereas at Server side while retrieving it you are typeCasting it to PhoneBook instead of String using following line:

pB = (PhoneBook)in.readObject();

That's leading to stream mismatch..Hence the exception is arising.
The solution to this problem could be as follows: I assume that your PhoneBook class is Serializable.

At client side use :

 out.writeObject("REQUEST_SEARCH\n" + p.toString());
 out.writeObject(p);//Given that PhoneBook is Serializable.
Sign up to request clarification or add additional context in comments.

1 Comment

after the change the program take the element of contact and closing the connection is not make a search for contact the server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.