我有一個客戶端和服務器,它們之間交換消息和對象。
我的問題是當我在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並關閉連接。
如果您使用的是Eclipse,請用'CTRL + SHIFT + F'你的代碼.. – 2013-03-16 20:21:43
這個例外是在正在添加客戶端? – 2013-03-16 20:25:02
從服務器來的 – 2013-03-16 20:28:17