简体   繁体   English

服务器如何知道要与之通信的特定客户端的地址?

[英]how does the server comes to know about the address of a particular client to which it wants to communicate?

There is a server and multiple clients. 有一个服务器和多个客户端。 The server accepts the connection requests from multiple clients. 服务器接受来自多个客户端的连接请求。 The sockets created are stored in an array. 创建的套接字存储在数组中。 There is a list box in my application. 我的应用程序中有一个列表框。 On selecting a particular item it refers to the corresponding socket in the array of sockets (eg if I select first item, it will consider first socket in the array). 在选择特定项目时,它指的是套接字数组中的相应套接字(例如,如果我选择第一项,它将考虑数组中的第一个套接字)。 But the problem is - as the clients can connect to the server in random fashion how the server keep tracks of the clients if it has to send data to a particular client. 但是问题是-由于客户端可以以随机方式连接到服务器,因此如果服务器必须向特定客户端发送数据,服务器将如何跟踪客户端。 Here is my code: 这是我的代码:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        int i=0;
        TcpListener listener = new TcpListener(8888);

        listener.Start();

        while(true)
        {
            Socket soc = listener.AcceptSocket();
            socarray[i] = soc;
            i++;
            if (i == NUMBEROFCLIENTS)
                break;


        }


       // Thread writetodatabase = new Thread(datawrite);
      //  writetodatabase.Start();

        Application.Run(new Form1());


    }
      private void button5_Click(object sender, EventArgs e)
    {
        if (listBox1.Text == "Reader1")
        {
            reader_flag = 1;
            toolStripStatusLabel1.Text = "reader1 selected";
            a = toolStripStatusLabel1.Text;
        }

        if (listBox1.Text == "Reader2")
        {
            reader_flag = 2;
            toolStripStatusLabel1.Text = "reader2 selected";
            a = toolStripStatusLabel1.Text;

        }

     }


          namespace WindowsFormsApplication1
     {
           public partial class Form1 : Form
         {
            string a;
            public static int reader_flag = 0;

              public Form1()
             {
                InitializeComponent();
              }
            private void Form1_Load(object sender, System.EventArgs e)
          {


            }


      private void button2_Click(object sender, EventArgs e)
     {
        Form3 formmm = new Form3();
        formmm.Show();
     }



    private void button4_Click(object sender, EventArgs e)
    {
        Form2 formm = new Form2();
        formm.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {

            byte[] bytesFrom = new Byte[1000];
            Program.socarray[0].Receive(bytesFrom);
            char[] stuffed =  System.Text.Encoding.UTF8.GetString(bytesFrom).ToCharArray();

            int i;
            char escape='@';

            List<char> unstuffed = new List<char>();

             for(i=0;i<stuffed.Length;i++)

             {

              if(stuffed[i]==escape)

                {
                   i++;
                   unstuffed.Add(stuffed[i]);

                 }

               else

                {

                 unstuffed.Add(stuffed[i]);

                }

              }
                  unstuffed.RemoveAt(0);
                  unstuffed.RemoveAt(unstuffed.Count-1);


                  char[] final;
                  final = unstuffed.ToArray();
                  string foo = new string(final);
                  textBox1.Text = foo;



System.IO.File.WriteAllText(@"C:\Users\cdac\Desktop\server\server\TextFile2.txt", foo);

                 }
           finally { }

             }

            private void textBox1_TextChanged(object sender, EventArgs e)
             {

             }

       private void button3_Click(object sender, EventArgs e)
       {
        Form4 mm = new Form4();
        mm.Show();
       }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
      {
          toolStripStatusLabel1.Text = a;
      }

      private void button5_Click(object sender, EventArgs e)
      {
        if (listBox1.Text == "Reader1")
        {
            reader_flag = 1;
            toolStripStatusLabel1.Text = "reader1 selected";
            a = toolStripStatusLabel1.Text;
        }

        if (listBox1.Text == "Reader2")
        {
            reader_flag = 2;
            toolStripStatusLabel1.Text = "reader2 selected";
            a = toolStripStatusLabel1.Text;

        }

    }

    private void button6_Click(object sender, EventArgs e)
    {
        string MyConString = "server=localhost;" +
               "database=cdac;"+
               "User Id=root;"

               +"password=cdac56;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            connection.Open();

               //StreamReader reader = new StreamReader("C:\\tag_log_030610.txt");
               StreamReader reader = new StreamReader("C:\\Users\\cdac\\Desktop\\server\\server\\TextFile2.txt");

               string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] parts = line.Split(';');
                   //command.CommandText = "insert into st_attn(rollno,Th_attn,Name) values('" + parts[0] + "','" + parts[1] + "','" + parts[2] + "')";

                    command.CommandText = "insert into st_attn(rollno) values('" + parts[0] + "')";



                    Reader = command.ExecuteReader();
                }





            }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    }
}

Client-Server apps ALWAYS start with a request from the client to the server. 客户端-服务器应用程序总是从客户端到服务器的请求开始。 No matter if it's a business app, a game, a website, a webservice or even something to tell the time, the client will always first ask the server. 无论是商务应用程序,游戏,网站,Web服务,还是什至可以说时间的东西,客户端始终会首先询问服务器。

This means that if you're using sockets, you will always have to let the request come from the client. 这意味着,如果您使用套接字,则始终必须让请求来自客户端。 He will send a request to the server. 他将向服务器发送请求。 The server will then keep a record internally of where they can find the client. 然后,服务器将在内部记录他们可以在哪里找到客户端的记录。 This is kept in the Endpoint property of the Socket. 这保留在Socket的Endpoint属性中。 But normally, you will not have to worry about this unless you're working with push apps. 但是通常情况下,除非您使用的是推送应用程序,否则您不必担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM