2016-11-09 11 views
-1

私は、Javaソケットで小さな例を作ろうとしています。しかし、私はそれを動作させることはできません。サーバーはクライアントの請願を正しく受け取ります。しかし、問題は、私がサーバーに文字列 "hello"を送信しようとしているときに来ます。javaソケットサーバーとソケットクライアントの接続

私はデバッグしたので、サーバーのInputStreamはnullなので、何も出力しません。私はこの問題は、多分私はBufferedWriterのような他のクラスで試してみました、別のクラスを使用する必要があり、PrinterWriterでなければならないと思いますが、私はそれを動作させることができませんでしたここ

は私のコード

サーバー

です事前に
public class ServidorFechaHora { 

    static final int port = 5000; 
    static String line; 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     ServerSocket ss = new ServerSocket(port); 
     while (true) { 
      /* accept nos devuelve el Socket conectado a un nuevo cliente. 
      Si no hay un nuevo cliente, se bloquea hasta que haya un 
      nuevo cliente. 
      */ 
      Socket soc = ss.accept(); 
      System.out.println("Cliente conectado"); 
      // Obtenemos el flujo de entrada del socket 
      InputStream is = (InputStream) soc.getInputStream(); 
      //Función que llevaría a cabo el envío y recepción de los datos. 
      processClient(is); 
     } 
    } 

    private static void processClient(InputStream is) throws IOException { 
     // TODO Auto-generated method stub 
     BufferedReader bis = new BufferedReader(new InputStreamReader(is)); 

     while ((line = bis.readLine()) != null){ 
      System.out.println(line); 
     } 

     bis.close(); 
    } 

} 

クライアント

public class ClienteFechaHora { 
    public static void main(String[] args) throws IOException, InterruptedException { 
     Socket client = null; 
     PrintWriter output = null; 
     //BufferedOutputStream out = null; 
     DateFormat hourdateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 

     try { 
       client = new Socket("127.0.0.1", port); 
       output = new PrintWriter(client.getOutputStream(), false); 

       while (true) { 
        System.out.println("Enviando datos..."); 
        output.write("hello"); 
        output.flush(); 
        Thread.currentThread().sleep(2000); 
        //System.out.println("Fecha y hora: " + hourdateFormat); 
       } 
     } 
     catch (IOException e) { 
      System.out.println(e); 
     } 
     output.close(); 
     client.close(); 
    } 
} 

ありがとう!

+0

にクライアントを変更しようと私はちょうど私とサーバーを確認ダミークライアントで正常に動作しています –

+0

サーバの入力ストリームがnullになることはありません。あなたが何を求めているのか不明です。 – EJP

+0

EJP私は、クライアントからサーバーへString "hello"を送り、 "processClient"というサーバーメソッドでそれを表示しようとしていると言っています。私は、説明やコードを読むだけで何をしようとしているのか、はっきりしていると思います。私にマイナスポイントを与えるのではなく、次回より注意深く読んでください。 :) – Javi

答えて

1

サーバーは、ソケットからライン・バイ・ラインを読んでいる:次のように

BufferedReader bis = new BufferedReader(new InputStreamReader(is)); 

while ((line = bis.readLine()) != null){ 
    System.out.println(line); 
} 

BufferedReader.readLine()が文書化されています

は、テキストの行を読み込みます。行は、改行( '\ n')、復帰( '\ r')、改行の直後に続く改行のいずれかで終了すると見なされます。

したがって、行終端文字を読み取るまでは戻りません。一方、

あなたのクライアントは、単にラインターミネータなしで「こんにちは」の文字列を書いている:

output.write("hello"); 
output.flush(); 

サーバーのテキストの行を読みたい場合は、あなたがの行を送信する必要がクライアントでの(行終端を含む)テキスト:

output.write("hello\n"); 
output.flush(); 
-1

あなたの問題はPrintWriterのは「文字指向」ストリーム、ライタークラスを拡張していることのようです。 そして、あなたはでのInputStream、とのデータをフェッチしようとするサーバー上で、「バイト指向」し、InputStream

を拡張するには、

in = new BufferedReader (new InputStreamReader (sock. getInputStream()); 
関連する問題