TCP客户端流

时间:2011-05-12 00:02:37

标签: stream tcpclient gateway

我正在通过电子邮件网关通信。该网关有一个特定的IP和端口。

网关是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\" : \"james@domain.pt\" }";
            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个),网络流就可以了。

但是,如果我发送了数千封电子邮件,我就会陷入混乱的谎言

:{iyo“asn ooyes”“ncd”0,“s_d:”4379“nme”92729,“er_u”,“ed_t_i”2#“p cin_d:”921891010-11:11.725,“s”4663175D0105E6912ADAAFFF6FDA393367“ RPY: “rcein”

为什么会这样?

别担心我不是垃圾邮件发送者:D

2 个答案:

答案 0 :(得分:0)

当您向TCP套接字写入消息时,它将使用发送的数据进行响应。当缓冲区已满时,我希望它为0,但无论如何都要提前发送缓冲区。你应该用返回值推进它:)

编辑:看起来你正在使用一个写入内部缓冲区的流抽象。情况是一样的。当内部缓冲区状态没有说明这一点时,你说“消息已被完全发送”,即位置不等于限制。在继续之前,您需要继续发送,直到剩余的缓冲区数量为0.

答案 1 :(得分: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'));

         ...
}

感谢您的帮助