单个网络流上的多个TCP客户端

时间:2016-08-09 07:48:38

标签: c# imap tcpclient

我的问题是如何使用单个imap服务器连接多个tcp客户端。这是我的程序的测试目的。 因为我可以通过为每个客户制作一个新功能来连接它们,但不能找出多个客户端。 提前谢谢。

public partial class MainWindow:Window     {

    public static TcpClient _imapClient1= new TcpClient("server addres",143);
    public static TcpClient _imapClient2 = new TcpClient(server addres, 143);

    public static NetworkStream _imapNs1;
    public static NetworkStream _imapNs2;
    public static StreamWriter _imapSw1;
    public static StreamWriter _imapSw2;
    public static StreamReader _imapSr1;
    public static StreamReader _imapSr2;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {


        Thread thread1 = new Thread(startThread1);
        Thread thread2 = new Thread(startThread2);

        thread2.Start();
        thread1.Start();
    }


    public static void startThread1()
    {


        connectToIMAP1(_imapClient1);

        LoginUser1("user1", "12345");


        Thread.Yield();
    }

    public static void startThread2()
    {


        connectToIMAP2(_imapClient2);
        LoginUser2("user2", "12345");

        Thread.Yield();
    }




    public static void connectToIMAP1(TcpClient _imapClient)

    {
        try
        {

            _imapNs1 = _imapClient.GetStream();
            _imapSw1 = new StreamWriter(_imapNs1);
            _imapSr1 = new StreamReader(_imapNs1);
            Response1(_imapClient1);

            //  MessageBox.Show("*** Connected ***");
        }
        catch (SocketException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

     public static void connectToIMAP2(TcpClient _imapClient)

    {
        try
        {

            _imapNs2 = _imapClient.GetStream();
            _imapSw2 = new StreamWriter(_imapNs2);
            _imapSr2 = new StreamReader(_imapNs2);
            Response2(_imapClient2);

            //  MessageBox.Show("*** Connected ***");
        }
        catch (SocketException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }




    public static string Response1(TcpClient _imapClient)
    {
        byte[] data = new byte[_imapClient.ReceiveBufferSize];
        int ret = _imapNs1.Read(data, 0, data.Length);
        // string lines = Encoding.ASCII.GetString(data).ToString();
        MessageBox.Show(Encoding.ASCII.GetString(data).ToString());
        return Encoding.ASCII.GetString(data).TrimEnd();



    }

    public static string Response2(TcpClient _imapClient)
    {
        byte[] data = new byte[_imapClient.ReceiveBufferSize];
        int ret = _imapNs2.Read(data, 0, data.Length);
        // string lines = Encoding.ASCII.GetString(data).ToString();
        MessageBox.Show(Encoding.ASCII.GetString(data).ToString());
        return Encoding.ASCII.GetString(data).TrimEnd();



    }



    public static void LoginUser1(string username, string password)
    {
        _imapSw1.WriteLine("$ LOGIN " + username + " " + password);
        _imapSw1.Flush();
        Response1(_imapClient1);

    }


    public static void LoginUser2(string username, string password)
    {
        _imapSw2.WriteLine("$ LOGIN " + username + " " + password);
        _imapSw2.Flush();
        Response2(_imapClient2);

  }

}

0 个答案:

没有答案