Click here to Skip to main content
12,622,568 members (33,088 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 ore how to convert listbox items to a byte.
Thank you in advance for your reply
Posted 31-Jan-13 22:28pm
dr_iton879
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Please check this

Server

private static TcpListener ser;
        private static Socket sock;
 
        static void Main(string[] args)
        {
            try
            {
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                ser = new TcpListener(localAddr, 4545);
                ser.Start();
                Console.WriteLine("Client Connected.........");
                Console.WriteLine("Server Started...");
 
                sock = ser.AcceptSocket();
                Console.WriteLine("Client Connected...");
 
                Thread t = new Thread(new ThreadStart(ReadClientData));
                t.Start();
 
                while (sock.Connected)
                {
                    string data = Console.ReadLine();
                    byte[] bytedata = Encoding.ASCII.GetBytes(data);
                    sock.Send(bytedata, bytedata.Length, 0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
 

 
        }
 
        private static void ReadClientData()
        {
            while (sock.Connected)
            {
                try
                {
                    byte[] Buffer = new byte[255];
                    sock.Receive(Buffer, 255, 0);
                    string data = Encoding.ASCII.GetString(Buffer);
                    Console.WriteLine("Client: " + data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }


Client

private static TcpClient client = new TcpClient();
        private static NetworkStream ns;
        static void Main(string[] args)
        {
            try
            {
                client.Connect("localhost",4545);
                Console.WriteLine("Connected with server .....");
                ns = client.GetStream();
                Thread t = new Thread(new ThreadStart(ReadServerData));
                t.Start();
                while (client.Connected)
                {
                    string data = Console.ReadLine();
                    byte[] bytedata = Encoding.ASCII.GetBytes(data);
                    ns.Write(bytedata, 0, bytedata.Length);
 
                }
            }
                catch(Exception ex)
                {
                    Console.Write(ex.Message);
                    Console.ReadLine();
                }
        }
            private static void ReadServerData()
            {
                while(client.Connected)
                {
                    try
                    {
                        byte[] Buffer = new byte[255];
                        ns.Read(Buffer,0,255);
                        string data = Encoding.ASCII.GetString(Buffer);
                        Console.WriteLine("Server: " +data);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
 

                }
            }

This is a simple console application which send and receive plain text.
  Permalink  
Comments
dr_iton 6-Feb-13 17:10pm
   
One more question.
As you know, I saved the data in text files, afterwards I can populate a listbox in a Server form with those data from text files.
Is there a way that i can send those lisbox data to another form in a Client side or I have to read them from text files in Server side.
What is my solution, I know that i have to convert those data in byte and save somehow in a buffer, afterwards the client has to accept them and populate the listbox.
I don't know how to do this.
Cheers.
Renju Vinod 6-Feb-13 23:12pm
   
Hi,
You need to send one server side list box value to one client side list box right?
dr_iton 7-Feb-13 2:49am
   
Yes, and sincerely, I have only 3 hours sleep and can not find a solution.
I populate a Server listbox with following:

public void ShtoStudentList()
{
try
{
ListaStudentet.Add(new KlasaStudentet(Convert.ToInt32(textBoxID.Text), textBoxEmri.Text, textBoxMbiemri.Text));
}
catch
{
MessageBox.Show("Gabim ne shenime", "Gabim!");
}
}
I make the connection with client with following:

private string ip = "127.0.0.1";
private int port = 9090;
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;

NetworkStream ns = new NetworkStream(socket);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);

string welcome = "Welcome to my test server";
sw.WriteLine(welcome);
sw.Flush();

while (true)
{
try
{
data = sr.ReadLine();
}
catch (IOException)
{
break;
}

Console.WriteLine(data);
sw.WriteLine(data);
sw.Flush();
}
MessageBox.Show("I shkyqur nga serveri {0}", ip);
sw.Close();
sr.Close();
ns.Close();

}

What to do next?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100