java中的writeobject和String出错

java中的writeobject和String出错,java,Java,我有一个客户端和服务器,我可以在它们之间交换消息和对象。 我的问题是在toString和字符串的帮助下发送对象。如何从字符串中选择对象并将其添加到列表中; 我写的代码 服务器代码: import java.net.*; import java.util.ArrayList; import java.io.*; public class Server { public static void main(String[] args) { ArrayList <PhoneBook&

我有一个客户端和服务器,我可以在它们之间交换消息和对象。
我的问题是在
toString
和字符串的帮助下发送对象。如何从字符串中选择对象并将其添加到列表中; 我写的代码

服务器代码:

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();
        } } }
     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();
    }
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
错误:

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();
        } } }
     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();
    }
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
应用背景:

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();
        } } }
     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();
    }
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

该程序是一个带有联系人(服务器)的PhooneBook,客户端先发送一个开始,然后服务器接收并发送,等待客户端制作一个对象电话簿,其中包括姓名、姓氏、工作、街道和电话,并发送消息请求。\u搜索服务器接收消息和对象(联系人)如果存在联系人,则使用lastname在数组列表中搜索;如果存在联系人,则返回对象,并使用
toString()显示姓名、姓氏、职务和其他
和消息记录然后客户端发送确定和服务器发送后端和连接关闭如果联系人不存在服务器发送消息NXRECORD客户端发送回消息请求\u插入服务器获取并在arraylist中添加对象,然后发送确定和客户端发送后端和关闭连接。

我不知道你在说什么正在该行之后发送到服务器。

out.writeObject("REQUEST_SEARCH\n" + p.toString());
但我假设您正在向服务器发送一个
字符串。
而在服务器端检索时,您使用以下行将其键入到
PhoneBook
而不是
String

pB = (PhoneBook)in.readObject();
这会导致流不匹配。因此会出现异常。
这个问题的解决方案如下:我假设您的
PhoneBook
类是
Serializable

在客户端使用:

 out.writeObject("REQUEST_SEARCH\n" + p.toString());
 out.writeObject(p);//Given that PhoneBook is Serializable.

我不知道在这一行之后您将向服务器发送什么。

out.writeObject("REQUEST_SEARCH\n" + p.toString());
但我假设您正在向服务器发送一个
字符串。
而在服务器端检索时,您使用以下行将其键入到
PhoneBook
而不是
String

pB = (PhoneBook)in.readObject();
这会导致流不匹配。因此会出现异常。
这个问题的解决方案如下:我假设您的
PhoneBook
类是
Serializable

在客户端使用:

 out.writeObject("REQUEST_SEARCH\n" + p.toString());
 out.writeObject(p);//Given that PhoneBook is Serializable.

如果您使用的是eclipse,请使用
CTRL+SHIFT+F
格式化您的代码。此异常在客户端发生?从服务器发生,您能知道在哪一行发生吗?如果可能的话,把完整的代码放在下面。下面是eclipse的错误//在这一行中是错误如果你使用eclipse,请使用
CTRL+SHIFT+F
格式化你的代码。这个异常在客户端发生?从服务器发生,你能告诉我是在哪一行发生的吗?如果可能的话,请输入完整的代码..下面是eclipse的错误//在这一行中是error在更改后程序采用contact元素并关闭连接不是搜索contact服务器更改后程序采用contact元素并关闭连接不是搜索联系服务器