2012-04-24 18 views
1

クライアント側がgroovyで、サーバー側がJavaである小さなソケットプログラムを作成しようとしています。以下は、私がGroovyとJavaプログラム間のソケット通信

クライアント書いたコードされています

def s = new Socket("localhost", 4444); 
s << "Server before withStreams\n"; 
s.withStreams { input, output -> 
    println"Sending message1" 
    output << "server message1\n" 
} 
s.close(); 

は、サーバー:私は両方のプログラムを実行すると

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

public class Logger{ 
    ServerSocket providerSocket; 
    Socket connection = null; 
    BufferedReader in; 
    String message="InitialMessage"; 
    Logger(){} 

    void run() 
    { 
    try{ 
     providerSocket = new ServerSocket(4444, 10); 
     try{ 
     Thread.sleep(1000); 
     } 
     catch(InterruptedException ie) 
     { 
     System.out.println("Sleep Interrupted"); 
     } 
     System.out.println("Waiting for connection"); 
     connection = providerSocket.accept(); 
     System.out.println("Connection received from " + connection.getInetAddress().getHostName()); 

     in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     do{ 
     if(in.ready()) 
     { 
      try{ 
      System.out.println(in.read()); 
      message = in.readLine(); 
      System.out.println("client>" + message); 
      } 
      catch(IOException e) 
      { 
      System.out.println(e); 
      e.printStackTrace(); 
      break; 
      } 
     } 
     } while(!message.equals("bye")); 
    } 
    catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
    finally{ 
     //4: Closing connection 
     try{ 
     in.close(); 
     providerSocket.close(); 
     } 
     catch(IOException ioException){ 
     ioException.printStackTrace(); 
     } 
    } 
    } 

    public static void main(String args[]) 
    { 
    Logger server = new Logger(); 
    while(true){ 
     server.run(); 
    } 
    } 
} 

、ソケット通信が確立されています。ソケットから読み取るときにIOExceptionがサーバーコードに入ります(message = in.readLine();

クライアントのソケットに書込み時に何らかのフォーマット問題があると思います。しかし、正確な問題を把握することはできません。誰でも助けることができますか?

+2

stacktrace – Anton

答えて

0

通常、クライアント接続ごとにServetSocketを閉じたくない場合があります。これを一度(またはサーバーを起動するたびに)行い、各accept()でクライアント接続を処理し、その接続のソケットを閉じますが、サーバーを停止するまでServerSocketを開いたままにします。

ここでは、複数の同時リクエストを処理するクライアント要求ごとに新しいスレッドを作成する、サンプルサーバーの書き換えバージョンを示します。テストクライアントは終了文字列 "bye"を送信しないので、接続とソケットは開いたままであることに注意してください。

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

public class Logger { 
    private ServerSocket providerSocket; 

    Logger() { 

    } 

    public void start() { 
     try { 
      providerSocket = new ServerSocket(4444, 10); 
      while (true) { 
       System.out.println("Waiting for connection"); 
       Socket connection = providerSocket.accept(); 
       new Thread(new Job(connection)).start(); 
      } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (providerSocket != null) { 
      System.out.println("Stopping server"); 
      try { 
       providerSocket.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    } 

    static class Job implements Runnable { 
     final Socket connection; 
     private static int id; 
     private int clientId = ++id; 

     public Job(Socket connection) { 
      this.connection = connection; 
     } 

     public void run() { 
      BufferedReader in = null; 
      try { 
       System.out.println("Connection " + clientId + " received from " + connection.getInetAddress().getHostName()); 
       in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       String message = "InitialMessage"; 
       do { 
        if (in.ready()) { 
         try { 
          // not sure why want to read one character then read the line 
          //int ch = in.read(); 
          //System.out.println(ch); 
          // -1 if the end of the stream has been reached 
          //if (ch == -1) break; 
          message = in.readLine(); 
          // null if the end of the stream has been reached 
          if (message == null) break; 
          System.out.println("client>" + message); 
         } catch (IOException e) { 
          System.out.println(e); 
          e.printStackTrace(); 
          break; 
         } 
        } 
       } while (!message.equals("bye")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       //4: Closing connection 
       System.out.println("Close connection " + clientId); 
       if (in != null) 
        try { 
         in.close(); 
        } catch (IOException ioException) { 
         ioException.printStackTrace(); 
        } 
       try { 
        connection.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    public static void main(String args[]) { 
     Logger server = new Logger(); 
     server.start(); 
    } 
}