メッセージとオブジェクトを交換するクライアントとサーバーがあります。
私の問題は、オブジェクトと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 サーバーはメッセージとオブジェクト (連絡先) を取得し、連絡先が存在する場合は姓で配列リストを検索し、存在する場合はオブジェクトを送り返し、名前、姓、仕事などを表示しますtoString()
メッセージ RECORDS その後、クライアントは OK を送信し、サーバーは END を送信し、連絡先が存在しない場合は接続が閉じられます。サーバーはメッセージ NXRECORD を送信します。クライアントはメッセージを返信します。 END を返信し、接続を閉じます。