SocketException の発生を停止するにはどうすればよいですか?
シリアル化されたオブジェクトをクライアントからローカル マシンのサーバーに簡単に転送しようとしています。
次のコードのわずかなバリエーションを使用して文字列を送信できましたが、オブジェクトを送信しようとすると
Customer customerToReceive = (Customer) input.readObject();// EXCEPTION OCCURS RIGHT HERE
解釈方法がわからない SocketException が発生します。
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at MattServer.runCustomerServer(MattServer.java:44)
at MattServer.<init>(MattServer.java:14)
at MattServerTest.main(MattServerTest.java:10)
以下はクライアントのコードですが、まったく問題がないようです。 public class MattClient { Socket client; ObjectOutputStream 出力。ObjectInputStream 入力; 文字列メッセージ。
public MattClient()
{
runCustomerClient();
}
public void runCustomerClient()
{
try
{
//Connection:
System.out.println("Attempting connection...");
client = new Socket("localhost",12345);
System.out.println("Connected to server...");
//Connect Streams:
//output.flush();
System.out.println("Got IO Streams...");
//SEND MESSAGES:
try
{
for(int i = 1;i<=10;i++)
{
output = new ObjectOutputStream(client.getOutputStream());
Customer customerToSend = new Customer("Matt", "1234 fake street", i);
System.out.println("Created customer:");
System.out.println(customerToSend.toString());
output.writeObject(customerToSend);
output.flush();
};
message = "TERMINATE";
System.out.println(message);
output.writeObject(message);
output.reset();
output.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e2)
{
e2.printStackTrace();
}
finally
{
}
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch(Exception e3)
{
e3.printStackTrace();
}
finally
{
}
}
そして、反乱を起こすサーバー:
public class MattServer
{
ServerSocket server;
Socket socket;
ObjectInputStream input;
ObjectOutputStream output;
String message;
public MattServer()
{
runCustomerServer();
}
public void runCustomerServer()
{
try
{
server = new ServerSocket(12345,100000);
while(true)
{
//CONNECTION:
System.out.println("Waiting for connection");
socket = server.accept();
System.out.println("Connection received...");
//CONNECT STREAMS:
//output = new ObjectOutputStream(socket.getOutputStream());
//output.flush();
input = new ObjectInputStream(socket.getInputStream());
System.out.println("Got IO Streams...");
//PROCESS STREAMS:
System.out.println("Connection successful!");
do
{
System.out.println("Started loop");
try
{
System.out.println("in try...");
System.out.println(socket.getInetAddress().getHostName());
Customer customerToReceive = (Customer) input.readObject();// EXCEPTION OCCURS RIGHT HERE
Object o = input.readObject();
System.out.println("Object of class " + o.getClass().getName() + " is " + o);
System.out.println("Got customer object");
System.out.println(customerToReceive.toString());
}
catch(ClassNotFoundException cnfE)
{
System.out.println("Can't convert input to string");
}
} while(!message.equals("TERMINATE"));
System.out.println("Finished.");
}
}
catch(IOException ioE)
{
ioE.printStackTrace();
}
finally
{
try
{
input.close();
socket.close();
server.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}