2016-10-28 3 views
0

Tcp接続要求に問題があります。私は2つのアプリケーションを持っています.1つはAsp.net Webサイトで、もう1つはAsp.net WebApiプロジェクトです。 Apiは、モバイルアプリケーション用にウェブで使用されているのと同じ機能を公開するために使用されます。私の問題は、tcp接続を介してpdfファイルを要求し、pdf応答を返すメソッドがあることです。 Webアプリケーションをテストするとき、正しいバイト応答が得られますが、Apiでテストしているときに、すべてのバイトを受け取っていません。このバリエーションの原因は何ですか?tcp接続の問題が同じバイト結果を返さない

「RequestDataは反対側のPDFファイルを生成するためのパラメータを持つXML文字列である」ここでTCP要求を行うことで、コードの平和があります。

public static string Connect(string requestData) 
     { 
      // Create all required object and variables 
      TcpClient tcpConnect = new TcpClient(); 
      NetworkStream tcpStream = null; 
      string responseData = string.Empty; 
      int bytesRead = 0; 
      int resLength = 0; 
      using (tcpConnect) 
      { 
       try 
       { 

        tcpConnect.Connect(hostAddress, hostPort); 
       } 
       catch (Exception ex) 
       { 

        throw ex; 
       } 

       // Open TCP stream and send data 
       using (tcpStream = tcpConnect.GetStream()) 
       { 

        byte[] reqBuffer = Encoding.GetEncoding("ISO8859-1").GetBytes(requestData); 

        // Get the Big Endian representation of the length as a byte array. 
        byte[] lenBuffer = new byte[4]; 
        lenBuffer[0] = (byte)((reqBuffer.Length & 0xFF000000) >> 24); 
        lenBuffer[1] = (byte)((reqBuffer.Length & 0x00FF0000) >> 16); 
        lenBuffer[2] = (byte)((reqBuffer.Length & 0x0000FF00) >> 8); 
        lenBuffer[3] = (byte)(reqBuffer.Length & 0x000000FF); 

        // Send length of request as Big Endian byte array 
        tcpStream.Write(lenBuffer, 0, lenBuffer.Length); 

        // Send request data as string 
        tcpStream.Write(reqBuffer, 0, reqBuffer.Length); 

        // Read 4 Bytes from the buffer, unless the socket is closed 
        bytesRead = 0; 

         bytesRead = tcpStream.Read(lenBuffer, bytesRead, lenBuffer.Length); 



        // If the number of bytes returned is 0 then throw an exception 
        if (bytesRead != 4) 
        { 
         throw new Exception("Unable to communicate with server or invalid data received"); 
        } 

        // Get the length of the response byte array from a Big Endian representation. 
        resLength = (lenBuffer[0] << 24) + (lenBuffer[1] << 16) + 
             (lenBuffer[2] << 8) + lenBuffer[3]; 

        // Keep reading from the socket into the buffer until we have the specified number of bytes. 
        byte[] resBuffer = new byte[resLength]; 

        // Read 4 Bytes from the buffer, unless the socket is closed 
        bytesRead = 0; 
// Issue is here on this below while loop. the web app will receive more than 30 000 bytesread and APi get 3 000 and this result to an api receiving a blank pdf 
        while (bytesRead < resBuffer.Length) 
        { 
         bytesRead += tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead); 
         if (bytesRead == 0) 
         { 
          break; 
         } 
        } 

        // If the number of bytes returned is 0 then throw an exception 
        if (bytesRead != resLength) 
        { 
         throw new Exception("Unable to communicate with server or invalid data received"); 
        } 

        // Convert the response to valid string 
        responseData = Encoding.GetEncoding("ISO-8859-1").GetString(resBuffer); 

        // Close the stream 
        tcpStream.Close(); 
       } 

       // Close the connection 
       tcpConnect.Close(); 
      } 

      // Dispose of all objects 
      tcpConnect = null; 
      tcpStream = null;`enter code here` 

      // Return result data 
      return responseData; 
     } 
+0

?私は、httpクライアントが表示されません。純粋なTCPソケットを使用しているようです。 –

+0

モバイルアプリはApiを消費していますので、私はコードを投稿しませんでした。しかし、ありがとう、私は、コードに何も間違っていることがわかりました。 5月のApiメソッドは受け入れられたクエリーストリングパラメータで取得し、私のパラメータの1つはその中にいくつか "+"付きで無視され、私のメソッドに間違った値を渡すことになりました – sipho

+0

読み込みを中止するときは?私はまだその仕組みが分からない。私の答えをチェックしてコメントできますか? 「ブレーク」と呼ばれることはありますか? –

答えて

0

bytesReadは初めて0になるため、これはまったく機能しません。だから、別のコードでない限り、あなたのAsp Webサイトでどのように動作するのか分かりません。あなたは何をすべき

bytesRead += tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead); 
if (bytesRead == 0) 
{ 
    break; 
} 

です:あなたがWEBAPIを使用している

int bRead = tcpStream.Read(resBuffer, bytesRead, resBuffer.Length - bytesRead); 
    if (bRead == 0) 
    { 
     break; 
    } 
bytesRead += bRead; 
+0

このif文は、readが0バイトを返して見た目を壊す場合にのみ実行されます。しかし、bysteRead sipho

+0

@siphoあなたのコメントをありがとうが、私はあなたが間違っていることを恐れています。この休憩は決して実行されません。ここで私がお勧めするテストです。大きなファイルの転送を開始し、転送の途中でサーバーの電源を切る。クライアント上のこの時点では、未処理の例外またはtcpStream.Read()の無限ループが発生します。 –

関連する問題