2011-08-07 15 views
2

私はJavaサーバーを書かれており、ここにコードされています:ServerSocket java-serverは入力を1回だけ読み込みますか?

try 
{ 
    ss = new ServerSocket(8080); 
    while (true) 
    { 
     socket = ss.accept(); 
     System.out.println("Acess given"); 

     in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

     //out = new PrintWriter(socket.getOutputStream(),true); 

     line = in.readLine(); 
     System.out.println("you input is :" + in.readLine()); 
    } 
} 

とiPhoneアプリケーションはクライアントであり、そのためのコードがあります:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    socket = [[LXSocket alloc]init]; 

    if ([socket connect:@"10.211.55.2" port:8080]) { 
     NSLog(@"socket has been created"); 
    } 
    else { 
     NSLog(@"socket couldn't be created created"); 
    } 

    @try { 


    }@catch (NSException * e) { 
     NSLog(@"Unable to send data"); 
    } 

    [super viewDidLoad]; 
} 

-(IBAction)sendData{ 
    [socket sendString:@"A\n"]; 
} 

私はここ2つの問題を抱えています:まず、サーバーが入力を1回だけ読み取っていることです。 2つ目は、データを出力しようとすると、メソッドを2回呼び出すまで出力されません(uibuttonを2回クリックするまで)。ここで何が起こっているのか分かりません。私は間違って何をしていますか?

while (server-socket is accepting new connections) 
{ 
    // The server-socket's job is to listen for connection requests 
    // It does this typically in a loop (until you issue server-shutdown) 
    // on accept the server-socket returns a Socket to the newly connected client 
    // 
    socket s = server-socket.accept-connection(); 

    // various options here: 
    // 
    // typically fire off a dedicated thread to servie this client 
    // but also review NIO or (home-grown) connection-map/handler patterns 
    // the general pattern: 
    // create a dedicated thread per connection accepted. 
    // pass Socket (s) to the handler method (a Runnable) and start it off 
    // and that is it. 

    // Here we use the general pattern and create a dedicated 
    // handler thread and pass of the new connections' socket reference 
    // 
    Thread handler-thread = new Thread (handler-routine-as-runnable, s); 
    handler-thread.start(); 
} 
+0

あなたはあなたの[前の質問](http://stackoverflow.com/questions/6975410/serversocket-java-not-reading-inputstream)に受け入れた答えの最初の部分を実装していませんでした。それを行う。バッファリングはあなたをブロックしている可能性が非常に高いです。また、ある時点でストリームを閉じてください。 – Mat

+0

ありがとうございます –

答えて

3

あなたのwhileループ内の新しいリーダー毎回作成されています

+0

ありがとうございます。 ) –

0

Tushar、

一般的なパターンは、この(ほとんどのJavaが、擬似コード)です。代わりにwhileループの外側にコードを移動し、readLine()を呼び出してブロックします。

socket = ss.accept(); 
in = new BufferedReader(new InputStreamReader(socket.getInputStream()); 
String line = ""; 
while (true) { 
    line = in.readLine(); 
    System.out.println("you input is :" + line); 
    if ("Bye".equals(line)) 
     break; 
} 

ここにはexampleサーバーサイドプログラムがあります。

1

alphazeroパターンを掲示しているので、私は簡単にストリップダウン実装掲載します:

public class SocketThread extends Thread { 
private Socket communicationSocket = null; 
public SocketThread(Socket clientSocket) { 
communicationSocket = clientSocket; 
try { 
      input = new ObjectInputStream(communicationSocket.getInputStream()); 

     } catch (IOException e) { 
      logger.info("Error getting communication streams to transfer data."); 
      try { 
       communicationSocket.close(); 
      } catch (IOException e1) { 

       e1.printStackTrace(); 
      } 
     } 
} 

public void run() { 
boolean listening=true; 
     DataObject command = null; 
     while (listening) { 
      try { 
       Object currentObject = input.readObject(); 
       if (currentObject != null 
         && currentObject instanceof DataObject) { 
        command = (DataObject) currentObject; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 

      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } finally { 
// If we got to this point is because we received a request from 
// the client 
// we can exit the loop 
       listening = false; 
      } 
     } 
} 
} 

注:

try { 
      ServerSocket ss = new ServerSocket(portNumber); 
      logger.info("Server successfully started on port " + portNumber); 
      // infinite loop that waits for connections 
      while (true) { 
       SocketThread rst = new SocketThread(ss.accept()); 
       rst.start(); 

      } 
     } catch (IOException e) { 
      logger.info("Error: unable to bind to port " + portNumber); 
      System.exit(-1); 
     } 

SocketThreadのようなものがある:これは、サーバーで

を: "DataObject"はカスタムクラスです。あなたが読んでいるバイト数を心配することなく、ソケットからDataObject自体を読むことができるので、もっと実用的かもしれません。 DataObjectはSerializableとしてフラグが立てられます。

希望します。

関連する問題