2011-05-12 251 views
0

我與電子郵件網關通信。該網關具有特定的IP和端口。TCP客戶端流

請求網關是JSON格式化的,網關通常會先處理進程狀態,然後用確認或錯誤狀態進行響應,也用JSON表示。

使請求和接收響應的代碼是:

using System; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Collections.Generic; 
using System.Threading; 

using Microsoft.Win32; 

public class TcpClientSample 
{ 
    public static void SendMessage(TcpClient client, string msg) 
    { 
     Console.WriteLine("REQUEST:" + msg); 

     NetworkStream stream = client.GetStream(); 

     byte[] myWriteBuffer = Encoding.ASCII.GetBytes(msg); 
     stream.Write(myWriteBuffer, 0, myWriteBuffer.Length); 

     byte[] myWriteBuffer2 = Encoding.ASCII.GetBytes("\r\n"); 
     stream.Write(myWriteBuffer2, 0, myWriteBuffer2.Length); 

     string gResponse = ""; 
     BinaryReader r = new BinaryReader(stream);   

     int receivedMessages = 0; 
     while (true) 
     {  
      while (true) 
      { 
       char currentChar = r.ReadChar(); 
       if (currentChar == '\n') 
        break; 
       else 
        gResponse = gResponse + currentChar; 
      }   

      if (gResponse != "") 
      { 
       Console.WriteLine("RESPONSE:" + gResponse); 
       receivedMessages = receivedMessages + 1;  
      } 

      if (receivedMessages == 2) 
      { 
       break; 
      } 

     }   
    } 

    public static void Main() 
    { 
     List<string> messages = new List<string>();   

     for (int i = 0; i < 1; i++) 
     { 
      String msg = "{ \"user\" : \"James\", \"email\" : \"[email protected]\" }"; 
      messages.Add(msg); 
     } 

     TcpClient client = new TcpClient(); 

     client.Connect("someIp", somePort); 

     int sentMessages = 0; 
     int receivedMessages = 0; 
     foreach (string msg in messages) 
     { 
      Thread newThread = new Thread(() => 
      { 
       sentMessages = sentMessages + 1; 
       Console.WriteLine("SENT MESSAGES: " + sentMessages); 
       SendMessage(client, msg); 
       receivedMessages = receivedMessages + 1; 
       Console.WriteLine("RECEIVED MESSAGES: " + receivedMessages);     
      });    
      newThread.Start(); 

     }   

     Console.ReadLine(); 
    } 
} 

如果我發送幾封電子郵件(最多10個)網絡流正常。

但是,如果我發郵件的數千我搞的一團糟字符騙

:{伊予 「ASN ooyes」 「名片」 0 「S_D:」 4379" NME」 92729 「er_u」, 「ed_t_i」 2 # 「p cin_d: 」921891010-11:11.725,「 S」 4663175D0105E6912ADAAFFF6FDA393367" RPY: 「rcein」

這是爲什麼

不要擔心,我不是垃圾郵件發送者:d

回答

0

我具有一個單一的方法解決了這個問題,只是從流中這樣寫着:

private TcpClient client; 
private NetworkStream stream; 

public void ListenFromGateway() 
{ 
    ... 
    while (true) 
     { 
     byte[] bytes = new byte[client.ReceiveBufferSize]; 

     //BLOCKS UNTIL AT LEAST ONE BYTE IS READ 
     stream.Read(bytes, 0, (int)client.ReceiveBufferSize); 

     //RETURNS THE DATA RECEIVED 
     string returndata = Encoding.UTF8.GetString(bytes); 

     //REMOVE THE EXCEDING CHARACTERS STARTING ON \r 
     string returndata = returndata.Remove(returndata.IndexOf('\r')); 

     ... 
} 

感謝您的幫助

0

當你寫一個消息給TCP套接字時,它會以發送的數據做出響應。當緩衝區滿時,我期望它是0,但是你提前發送無論如何緩衝。你應該通過返回值來提高它:)

編輯:它看起來像你正在使用流抽象寫入內部緩衝區。情況是一樣的。當內部緩衝區狀態不是說這個,即位置不等於限制時,你說「消息已經完全發送」。在繼續之前,您需要繼續發送,直到緩衝區的剩餘量爲0。