재시작 처리 방법을 이해할 수 있도록 프로토 타입 클라이언트 &을 만들었습니다. 서버는 서버 소켓을 만들고 영원히 청취해야합니다. 클라이언트가 연결하고, 데이터를 보내고, 소켓을 닫을 수 있지만 서버에 "완료 및 종료"유형 메시지를 보내지 않습니다. 원격 클라이언트가 닫힌 이후 readByte()
때 서버가 EOFException
가져옵니다. EOFException
의 오류 처리기에서 소켓을 닫고 새 소켓을 엽니 다.Java : 소켓에서 재 연결하면 오류가 발생합니다.
다음과 같은 문제가 있습니다. 소켓/입력 스트림/출력 스트림을 성공적으로 연 후에도 클라이언트가 outputStream.write()
호출을 수행 할 때 가끔 SocketWriteError
을 얻습니다. 이 소켓을 열고 닫는 빈도와 관련이있을 수 있습니다. 한 가지 흥미로운 점은 클라이언트가 임의의 수의 쓰기/닫기/다시 연결을 수행하기 전에 수행한다는 것입니다. 첫 번째 재 연결시에는 때로는 쓰레기가 나오기도하고, SocketWriteError
을보기 전에 50 번 다시 연결해야합니다.
서버 :
이public static void main(String[] args)
{
Server x = new Server();
x.initialize();
}
private void initialize()
{
ServerSocket s;
InputStream is;
DataInputStream dis;
while (true) //ADDED THIS!!!!!!!!!!!!!!!!!!!!!!
{
try
{
s = new ServerSocket(4448);
s.setSoTimeout(0);
s.setReuseAddress(true);
is = s.accept().getInputStream();
System.out.println("accepted client");
dis = new DataInputStream(is);
try
{
byte input = dis.readByte();
System.out.println("read: " + input);
} catch (Exception ex)
{
System.out.println("Exception");
dis.close();
is.close();
s.close();
}
} catch (IOException ex)
{
System.out.println("ioexception");
}
}
}
CLIENT :
public static void main(String[] args)
{
Socket s;
OutputStream os;
try
{
s = new Socket("localhost", 4448);
s.setKeepAlive(true);
s.setReuseAddress(true);
os = s.getOutputStream();
int counter = 0;
while (true)
{
try
{
os.write((byte) counter++);
os.flush();
os.close();
s.close();
s = new Socket("localhost", 4448);
s.setKeepAlive(true);
s.setReuseAddress(true);
os = s.getOutputStream();
} catch (Exception e)
{
e.printStackTrace();
System.err.println("ERROR: reconnecting...");
}
}
} catch (Exception ex)
{
ex.printStackTrace();
System.err.println("ERROR: could not connect");
}
}
사람을합니까 여기
java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) at java.net.SocketOutputStream.write(SocketOutputStream.java:115) at bytebuffertest.Client.main(Client.java:37)코드의 일부 조각입니다 : 여기 는 클라이언트 측에서 오류 발생 어떻게 제대로하는지 알아라. 다시 연결 하시겠습니까?
예, 대답이라고 생각합니다. 고맙습니다! – jbu