2014-01-25 2 views
2

클라이언트 코드의 마지막 줄과 코드에서 오류가 발생합니다. java.io.StreamCorruptedException : 유효하지 않은 스트림 헤더 : 36353137.이 시점 전에 스트림에 아무 것도하지 않았으므로 ObjectInputStream에 문제가 발생할 수 있는지 확실하지 않습니다.손상된 inputStream

서버 클래스가 올바르게 작동하고 설정 한 동작을 따르며 오류가 발생한 클라이언트 클래스 만 있습니다.

스트림이 플러시되지 않았으므로 문제가 해결되지 않았으므로 처음에는 문제가 있다고 생각했습니다.

이 외에도이 오류는 코드 초기에 발생하므로 해결하기 위해 추가해야 할 부분이 없어졌습니다.

클라이언트 클래스 -

import java.io.*; 
import java.net.*; 

public class tcpClient { 

    int nonce; 
    Socket requestSocket; 
    ObjectOutputStream out; 
    ObjectInputStream in; 
    String message; 
    tcpClient(){} 
    void run() 
    { 
     try{ 

      requestSocket = new Socket("localhost", 3223); 
      System.out.println("Connected to localhost in port 3223"); 

      out = new ObjectOutputStream(requestSocket.getOutputStream()); 
      out.flush(); 
      in = new ObjectInputStream(requestSocket.getInputStream()); 

서버 클래스 -

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.PrintStream; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Calendar; 
import java.util.Random; 
import java.util.Scanner; 
import javax.swing.Timer; 

public class TCPMasterServer 
    implements ActionListener 
{ 
    ServerSocket server; 
    Socket client; 
    Random random; 
    Calendar rightNow; 
    Timer timer; 
    String ipaddress; 
    PrintWriter out; 
    BufferedReader ir; 

    public TCPMasterServer() 
    { 
    this.random = new Random(); 
    this.rightNow = Calendar.getInstance(); 
    try 
    { 
     this.server = new ServerSocket(3223); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 

    private void listenForConnection() 
    { 
    try 
    { 
     this.client = this.server.accept(); 
     this.ipaddress = this.client.getInetAddress().getHostAddress(); 
     System.out.println(this.rightNow.getTime() + ": Connected from: " + this.ipaddress); 

     this.out = new PrintWriter(this.client.getOutputStream(), true); 
     this.ir = new BufferedReader(new InputStreamReader(this.client.getInputStream())); 

     communicateWithClient(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     listenForConnection(); 
    } 
    } 

    private void communicateWithClient() 
    { 
    this.timer = new Timer(2000, this); 
    this.timer.setRepeats(false); 
    this.timer.start(); 
    try 
    { 
     int nonce = this.random.nextInt(1000000); 
     this.out.println(nonce); 
     System.out.println(this.rightNow.getTime() + ": Send nonce: " + nonce); 


     String input = this.ir.readLine(); 
     Scanner in = new Scanner(input); 
     int nonceResponse = in.nextInt(); 

     System.out.print(this.rightNow.getTime() + ": Received number: " + nonceResponse); 
     if (nonceResponse == nonce + 1) 
     { 
     System.out.println("... OK"); 


     this.out.println("SEND_NAME"); 
     System.out.println(this.rightNow.getTime() + ": Request name"); 

     input = this.ir.readLine(); 
     System.out.println(this.rightNow.getTime() + ": Received name: " + input); 


     this.out.println(input + " ACK"); 
     System.out.println(this.rightNow.getTime() + ": ACK sent"); 
     } 
     this.client.close(); 
    } 
    catch (Exception ex) 
    { 
     System.out.println(this.rightNow.getTime() + ": Error happened. Giving up"); 
    } 
    this.timer.stop(); 
    System.out.println(); 
    listenForConnection(); 
    } 

    public void actionPerformed(ActionEvent evt) 
    { 
    try 
    { 
     System.out.println("Timer fired."); 
     this.client.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    } 

    public static void main(String[] args) 
    { 
    TCPMasterServer server = new TCPMasterServer(); 
    server.listenForConnection(); 
    } 
} 
+0

실제 예외 및 스택 추적을 게시하십시오. – EJP

답변

2

당신은 객체를 사용하는 서버에서 스트리밍하지만 클라이언트에서 독자와 작가. 그것은 작동하지 않습니다. 객체를 읽으려면 객체를 써야합니다.

+0

아, 이제 알았어. 감사. – user1210304