线程和网络

时间:2012-05-30 20:19:27

标签: c# multithreading networking

我正在制作客户端 - 服务器应用程序。将有许多客户端同时连接到服务器。我的第一种方法是使用TCPClient和TCPListener,但我对许多客户端这样做有点困惑。我想到了这一点:当客户端想要登录时,服务器创建新线程,将tcpclient传递给新线程,然后他们将进行通信,而主程序将等待来自其他客户端的新连接。一切都差不多了,但总是首先打包到main函数,第二个去应该得到的线程,第三个得到主程序等等。有人可以给我建议如何解决这个问题吗?

我最初没有添加它,因为它很大。 这是我的服务器主循环代码:

    public static String DoWork()
    {
        String ret = "";
        TcpClient client = server.AcceptTcpClient();
        Byte[] bytes = new Byte[256];
        String data = null;
        NetworkStream stream = client.GetStream();
        int i;
        try
        {
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                if (data == "hello")
                {
                    SingleConnection conn = new SingleConnection(client, stream);
                    connections.Add(conn);
                    Thread oThread = new Thread(new ThreadStart(conn.DoWork));
                    //make new thread with client and its stream. run it, so it will run async
                    data = "wait, i will make new thread for you";
                    oThread.Start();
                }
                else
                {
                    data = "1";
                }
                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                stream.Write(msg, 0, msg.Length);
            }
        }
        catch (System.IO.IOException e)
        {
        }
        if (data == "1")
            client.Close();
        return ret;
    }

这里是SingleConnection类(每个连接都适用于这个类):

class SingleConnection
{
    TcpClient client;
    Player player;
    Byte[] bytes;
    String data;
    NetworkStream stream;
    bool isAlive;
    int ssid;
    public SingleConnection(TcpClient client, NetworkStream stream)
    {
        this.client = client;
        this.stream = stream;
        isAlive = true;
        Console.WriteLine("Making new object for thread");
    }
    public void DoWork()
    {
        Console.WriteLine("We will listen from client");
        int i = 0;
        bytes = new Byte[256];
        data = null;
        while (isAlive)
        {
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                String[] respond = data.Split('|');
                if (data[0] == '2')
                {
                    client.Close();
                    isAlive = false;
                    //user want to logout
                }
                else
                {
                    Console.WriteLine("user says: "+data);
                    byte[] msg = System.Text.Encoding.ASCII.GetBytes("Hi");
                    stream.Write(msg, 0, msg.Length);
                }
            }
        }
    }
}

简单的客户:

public partial class Form1 : Form
{
    String sessionid;
    TcpClient client;
    Int32 port = 13641;
    String server = "192.168.2.101";
    String message;
    Byte[] data;
    NetworkStream stream;
    String responseData;
    String GameData;
    public int action = 0;
    public String args;
    bool isAlive;
    public bool newData;
    public int ping;
    public int ping_second;
    int i = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        String message = "hello";
        data = System.Text.Encoding.ASCII.GetBytes(message);
        stream.Write(data, 0, data.Length);
        data = new Byte[256];
        responseData = String.Empty;
        Int32 bytes = stream.Read(data, 0, data.Length);
        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
        listBox1.Items.Add(responseData);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        client.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        client = new TcpClient(server, port);
        stream = client.GetStream();
    }
}

0 个答案:

没有答案