2017-06-06 4 views
0

GCDAsyncSocketを使用してiOSサーバーを作成し、Java言語を使用してクライアントを作成します。 NSDAtaの形式でデータを送信すると、SocketTester(クライアント接続のテスト用アプリケーション)で読み取ることができますが、Java側では完全に読み取ることはできません。NSDataをJavaで作成されたクライアントサイドに読み込む方法

static void recorderServerClient() throws IOException{ 
    int port =9892; 
     clientSocket = new Socket("localhost", port); 
     System.out.print("Connected to localhost"+port); 

     while (true){ 
     InputStream inputStream = clientSocket.getInputStream(); 
     try { 
      Thread.sleep(3000); 
      } 
     catch (InterruptedException e) { 
      e.printStackTrace(); 
     }/*fetch data with help of output stream and write it with help of PrintWriter */ 
     PrintWriter printWriterObj = new PrintWriter(clientSocket.getOutputStream()); 
      printWriterObj.println(); 
      printWriterObj.flush(); 
      /*created an array for now static and assign some memory */    byte[] buffer = new byte[1000000]; 
      int read; 
      while((read = inputStream.read(buffer)) != -1) { 
       String output = new String(buffer, 0, read); 
       System.out.print(output); 
       System.out.flush(); 
      } 
     } 
     */ 

}

私は、iOSを介して送信されるデータの長さを取得server.andバッファを作成accordingly.Canいずれかが私を助けたいですか?

+0

どのような問題がありますか?あなたの不足しているSocket.accept()呼び出しが見えますか? –

+0

javaのreadIntメソッドでデータ長を読み取ることができません。 – Ruby

答えて

0
import java.io.*; 
import java.net.Socket; 

public class SocketDemo { 
    public static void main(String args[]) throws Exception { 

     Socket socket = new Socket("localhost", 2100); 
     System.out.println("Connected"); 

     DataInputStream ins = new DataInputStream(socket.getInputStream()); 
     DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
     System.out.println("opened OutputStreams"); 

     System.out.println(readResponse(ins)); 
     Thread.sleep(100); 
     out.write("ss 1\n".getBytes());//send command 
     out.flush(); 
     System.out.println("sent command"); 
     Thread.sleep(3000);//important to wait for the server to response 
     System.out.println(readResponse(ins)); 
     socket.close(); 
     System.out.println("Connection to the server closed"); 
    } 

    private static String readResponse(InputStream in) throws IOException { 
     byte[] buf = new byte[1024];//1k 
     StringBuilder sb = new StringBuilder(); 
     while (in.available() > 0) {//server is waiting for client once it sent all data 
      int pos = in.read(buf); 
      String s = new String(buf, 0, pos); 
      sb.append(s); 
      //System.out.println("reading data..." + pos + " s>>>" + s.getBytes() + "available?" + ins.available()); 
      //System.out.print(pos + " ? " + (char) pos); 
     } 
     return sb.toString(); 
    } 
} 
+0

申し訳ありません私のケースではこのコードは機能しません。私のシステムはちょうどハングアップしています。ポートに接続してデータを完全に読み取れるクライアントが必要です。 – Ruby

+0

私はそのデモを述べました。あなたのシステムがハングアップしていると言ったとき。どういう意味ですか?どのようにサーバーソケットに接続しますか?これは、このデモをテストする方法です。プログラムを実行すると、「Server started and listen on 9892」と表示されます。次に、別の端末を開き、「telnet localhost 9892」と入力します。開始する番号を入力すると、入力した内容がエコーされます。あなたがコードを理解しているかどうかは明らかです。 –

+0

私はGOSSyncSocketクラスを使ってiOSにサーバーを書いています。 Javaの終了時にはクライアント部分のみを実行する必要があります。あなたのコードをチェックした後、それは私のためには動作しません。それは単に私のシステムを3回ぶら下げているだけです。カーソルの移動はありません。実行中のアプリケーションはすべてハングアップします。 – Ruby

関連する問題