2013-03-16 25 views
1

我有一个客户端和服务器,它们之间交换消息和对象。
我的问题是当我在toString和字符串的帮助下发送对象时。如何从字符串中挑选出对象并将该对象添加到列表中; 我写了代码。在java中写入对象和字符串的错误

Server代码:

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(服务器)和客户端首先发送一个开始,服务器把它和客户端后,发信等待做一个对象电话簿,包括姓名,工作,街道和电话并发送它与消息REQUEST_SEARCH服务器拿消息和对象(联系人)和搜索数组列表姓氏如果存在联系人,如果存在回发对象并显示姓名,姓氏,工作和其他与toString()和消息RECORDS然后客户端发送确定和服务器发回END和连接正在关闭如果联系人不存在服务器发送混乱年龄NXRECORD客户端发回一条消息REQUEST_INSERT服务器拿它并在arraylist中添加对象并发送OK并且客户端发回END并关闭连接。

+0

如果您使用的是Eclipse,请用'CTRL + SHIFT + F'你的代码.. – 2013-03-16 20:21:43

+0

这个例外是在正在添加客户端? – 2013-03-16 20:25:02

+0

从服务器来的 – 2013-03-16 20:28:17

回答

1

我不知道你在这条线后发送给服务器。

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

但我认为你发送String到服务器。 而在服务器端,同时取回你使用它下面的行铸字来代替PhoneBookString

pB = (PhoneBook)in.readObject(); 

这导致流mismatch..Hence的异常引起的。
此问题的解决方案如下所示:我假设您的PhoneBook类是Serializable

在客户端使用:

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

之后改变程序取接触和关闭连接的元素是不是联系服务器进行搜索联系 – 2013-03-17 09:04:02

相关问题