我有一个客户端和服务器,我在它之间交换消息和对象
我的问题是当我在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
申请背景:
这个程序是一个带有联系人(服务器)的PhooneBook,客户端首先发送一个START,然后服务器接收它并在客户端制作一个包含姓名,工作,街道和电话的对象电话簿后发送WAITING 并发送消息REQUEST_SEARCH服务器获取消息和对象(联系人)并在数组列表中搜索<em> lastname 如果存在联系人,如果存在则发回该对象并显示名称,姓氏,作业和其他toString()
和消息RECORDS然后客户端发送OK并且服务器发回END并且如果联系人不存在则连接正在关闭服务器发送消息NXRECORD客户端发回消息REQUEST_INSERT服务器接受它并在arraylist中添加对象并发送OK并且客户端发送回END并关闭连接。
答案 0 :(得分:1)
我不知道你在这行之后发送给服务器的是什么。
out.writeObject("REQUEST_SEARCH\n" + p.toString());
但我假设您要向服务器发送String
。
在服务器端检索它时,您使用以下行键入PhoneBook
而不是String
:
pB = (PhoneBook)in.readObject();
这会导致流不匹配..因此会出现异常。
此问题的解决方案如下:我假设您的PhoneBook
班级为Serializable
。
在客户端使用:
out.writeObject("REQUEST_SEARCH\n" + p.toString());
out.writeObject(p);//Given that PhoneBook is Serializable.