2016-04-28 6 views
0

スキャナまたはプリントライターを使用できません。I/Oを使用してクライアントからサーバーに、そしてクライアントに戻す方法

私はクライアントとサーバーを持っています。

Creating Server Socket 5095 . . . 
SUCCESS!!! 
in while loop 
Waiting for connection. 

が、私は、クライアントを実行し、出力され、私は、サーバーを実行し、出力され

Creating Client Socket 
SUCCESS!!! 
initializing variables 
variables flushed 
initializing input 
java.io.EOFException 
at java.io.DataInputStream.readFully(Unknown Source) 
at java.io.DataInputStream.readLong(Unknown Source) 
at java.io.DataInputStream.readDouble(Unknown Source) 
at Client.runClient(Client.java:38) 
at Client.main(Client.java:51) 
私はクライアントにいくつかのダブルスを初期化し、送りたい

それらをサーバーに送り、そこで解析され、操作され、その結果がクライアントに返されます。

クライアントがEOFExceptionで失敗すると、サーバーは、私が間違って何をやっているClassCastException

で失敗しますか?

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

public class Client { 

static void pStr(String p) { 
    System.out.println(p); 
} 

static void runClient() { 

    Socket client; 
    DataOutputStream output; 
    DataInputStream input; 

    try { 
     pStr("Creating Client Socket "); 
     client = new Socket("127.0.0.1", 5095); 
     pStr("SUCCESS!!!"); 

     input = new DataInputStream(client.getInputStream()); 
     output = new DataOutputStream(client.getOutputStream()); 

     System.out.println("initializing variables"); 
     double arg1 = 5; 
     output.writeDouble(arg1); 
     output.flush(); 

     double arg2 = 3; 
     output.writeDouble(arg2); 
     output.flush(); 

     System.out.println("variables flushed"); 

     System.out.println("initializing input"); 
     // error occurs at next line 
     double ans = input.readDouble(); 
     System.out.println("input has been read"); 
     System.out.println(ans); 

     input.close(); 
     output.close(); 
     client.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
// this is second class file 
public static void main(String args[]) { 
    Client.runClient(); 
} 
} 
public class SimpleServer { 

static void pStr(String p) { 
    System.out.println(p); 
} 

static void runServer() { 
    ServerSocket server; 

    try { 
     // Create a ServerSocket. 
     pStr("Creating Server Socket " + 5095 + " . . . "); 
     server = new ServerSocket(5095); 
     pStr("SUCCESS!!!"); 

     while (true) { 
      System.out.println("in while loop"); 
      Socket connection; 
      DataOutputStream output = null; 
      DataInputStream input = null; 

      pStr("Waiting for connection."); 
      connection = server.accept(); 
      pStr("Done"); 

      System.out.println("entering try"); 

      try { 
       // Get input and output streams. 
       input = (DataInputStream) connection.getInputStream(); 
       output = (DataOutputStream) connection.getOutputStream(); 

       String ansStr; 

       double arg1, arg2; 
       try { 
        System.out.println("int try block"); 
        arg1 = input.readDouble(); 
        arg2 = input.readDouble(); 

        ansStr = "" + arg1 + "," + arg2; 
        System.out.println(ansStr); 

        output.writeDouble(arg1); 
        output.writeDouble(arg2); 

       } catch (Exception e) { 
        ansStr = e.getMessage(); 
       } 
       output.flush(); 
      } catch (Exception e) { 
       pStr("Error in Protocol: " + e.getMessage()); 
      } finally { 
       try { 
        input.close(); 
       } catch (Exception e) { 
       } 
       try { 
        output.close(); 
       } catch (Exception e) { 
       } 
       try { 
        connection.close(); 
       } catch (Exception e) { 
       } 
      } 
     } 
    } catch (Exception e) { 
     pStr("Error making ServerSocket"); 
    } 
} 

public static void main(String args[]) { 
    runServer(); 
} 
} 
+2

どのようなエラーが表示されますか? – Pablo

+0

私は、doubleをoutputStreamに書き込もうとすると、クライアントがクラッシュすると思います。 – rickyjoepr

+3

"クラッシュ"は非常に曖昧な言葉ですが、エラーがスローされる必要があります。 – Deximus

答えて

1

サーバーにコードを使用すると、unaccountably言及に失敗しているClassCastExceptionでクラッシュしています。したがって、データは送信されずに接続が閉じられているため、クライアント側の例外が発生します。

キャストだけでソケットからDataInputStreamまたはDataOutputStreamを取得することはできません。クライアントで使用しているのと同じプロセスを使用する必要があります。

+0

私は解決策を含めるために最初の投稿を変更しました – rickyjoepr

+3

@rickyjoeprあなたは**そうしないでください**。それはあなたの質問を無意味にします。私はそれを巻き戻した。 *実際の問題を表示するように変更する必要があります。つまり、サーバー出力から省略した 'ClassCastException'が表示されます。 – EJP

関連する問題