I made a program server and client, The problem is that I don't know how to send the data from Server to Client for example from listBox at a Server to show those data in a client Listbox.
I made some code but still no result.

private string ip = "127.0.0.1";
        private int port = 1234;
        private TcpListener listener = null;

        public void ListenClients()
        {
            listener = new TcpListener(IPAddress.Parse(ip), port);
            listener.Start();
            while (true)
            {
                Socket clientSocket = listener.AcceptSocket();

                Thread thread = new Thread(ClientWorker);
                thread.Name = clientSocket.RemoteEndPoint.ToString();
                thread.Start(clientSocket);
            }
        }
        private void ClientWorker(object objectSocket)
        {

            Socket socket = (Socket)objectSocket;

            while (socket.Connected)
            {
                try
                {
                    byte[] buffer = new byte[1024];
                    int readBytes = socket.Receive(buffer);
                    socket.Send = indeksLista.ConvertToByte[] ;
                   // socket.Send(LexoStudentetFajll());

                    string text = Encoding.UTF8.GetString(buffer, 0, readBytes);
                    Console.WriteLine("Client {0} Send: {1}", socket.RemoteEndPoint, text);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("ClientWorker Error: {0}", ex.Message);
                }
            }
        }

I don't have a clue where I'm wrong or how to convert listbox items to a byte array.
Thank you in advance for your reply

I would use a NetworkStream instead of a Socket to send data from server to client.
Here is an explication Click Here.
Also, you can find here a simple implementation of a client-server application.
Hope those things will help you.

I am using sockets to send the data from server side to a client side
I'm populating the listbox using followig method:

public string MerrShenimet()
        {
            return textBoxID.Text + "\t" + textBoxEmri.Text + "\t" + textBoxMbiemri.Text;
        }

To send the data from server to client I have to convert those data from string to byte array, something like this:

 public string DergoShenimet()
        {
             return (Convert.ToByte(textBoxID.Text) + "\t" + Convert.ToByte(textBoxEmri.Text)
                + "\t" + Convert.ToByte(textBoxMbiemri.Text));
        }

How to send those byte converted data to a client and how to show them in a listbox at a client side?

Edited 3 Years Ago by dr_iton

This article has been dead for over six months. Start a new discussion instead.