troubleshooting Question

Server / Client Listener Help C#

Avatar of Pretzel_Jesus
Pretzel_Jesus asked on
.NET ProgrammingC#
3 Comments1 Solution1755 ViewsLast Modified:
I have tried fairly hard to make a server / client application to pass simple text back and forth with different things I found around the net. I understand most of it... but I am a bit confused as I can only send one round of text and then I cant send any more. How do I made this perpetually listen and receive text?
IPHostEntry host;
            string ipAddress = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
                if (ip.AddressFamily.ToString() == "InterNetwork")
                    ipAddress = ip.ToString();
            //IPAddress ipAddress = IPAddress.Parse(ipAddress);
            int port = 8001;
            /* Initializes the Listener */
            TcpListener listener = new TcpListener(IPAddress.Parse(ipAddress), port);
            /* Start Listeneting at the specified port */
            Socket socket = listener.AcceptSocket();
            byte[] bytes = new byte[100];
            int k = socket.Receive(bytes);
            List<string> info = new List<string>();
            int i = 0;
            while (i < k)
            string data = "";
            foreach (string item in info)
                data = data + item;
            ASCIIEncoding asen = new ASCIIEncoding();
            socket.Send(asen.GetBytes("The string was recieved by the server."));
            StreamWriter file = new StreamWriter(@"C:\listener.txt");
TcpClient tcpclnt = new TcpClient();
        private void button1_Click(object sender, EventArgs e)
            string ipAddress = textBox1.Text;
            int port = Convert.ToInt32(textBox2.Text);
            //TcpClient tcpclnt = new TcpClient();
                tcpclnt.Connect(ipAddress, port);
                listBox1.Items.Add("Successfully connected to " + ipAddress + ":" + port);
                listBox1.Items.Add("Unable to connect to " + ipAddress + ":" + port);
            // use the ipaddress as in the server program
        private void button2_Click(object sender, EventArgs e)
            String data = textBox3.Text;
            Stream tcpStream = tcpclnt.GetStream();
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(data);
            tcpStream.Write(ba, 0, ba.Length);
            byte[] bb = new byte[100];
            int k = tcpStream.Read(bb, 0, 100);
            for (int i = 0; i < k; i++)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros