Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried the following code to create a server and a client using C#.NET.

Server and Client both are running but not responding.

But nothing is displayed in Server side.

If i use
C#
MessageBox.Show()
instead of textBox.text, then it's displaying the messages.

I want to display the messages in the textBox.

Please help me.

I guess the problem is in the following lines of code.

C#
/* receive */
                   byte[] bytesFrom = new byte[100000];
                   networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
                   string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                   textBox1.Text="Received From Clinet:" + dataFromClient;


What I have tried:

Server code in C#.NET

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)// start server
        {           
            TcpListener serverSocket = new TcpListener(System.Net.IPAddress.Any, 8888);
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();               
            textBox1.Text="Server Started";
            clientSocket = serverSocket.AcceptTcpClient();
            textBox1.Text="Got a connection from Client";           
            while(true)
            {
                try
                {
                    NetworkStream networkStream = clientSocket.GetStream();
                    /* receive */
                    byte[] bytesFrom = new byte[100000];
                    networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    textBox1.Text="Received From Clinet:" + dataFromClient;
                    /*send*/
                    string serverResponse = "Server says: Hi Client!";
                    Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
       }
    }
}


Client Code in C#.NET


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Client
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
       
        public Form1()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)//connect to server
        {
            clientSocket.Connect("127.0.0.1", 8888);
            MessageBox.Show("Connected to Server");
           
        }  
        private void button1_Click(object sender, EventArgs e) // Send msg
        {
            NetworkStream serverStream = clientSocket.GetStream();    

            /* send msg to server */ 
            // textBox2.Text = msg to be sent to server
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            /* receive msg from server */
            byte[] inStream = new byte[100000]; 
            serverStream.Read(inStream, 0, clientSocket.ReceiveBufferSize);
            string receivedMsg = System.Text.Encoding.ASCII.GetString(inStream);
            textBox1.Text = receivedMsg; // textBox1 to display the received msg


            textBox2.Text = ""; // clear textBox2
            textBox2.Focus();
        }           
    }
}
Posted
Comments
Richard MacCutchan 6-May-16 3:42am    
You need to step through the code with your debugger to see what data is received at each end. It is quite possible that the actual data transfers are preventing the UI from updating the form data. In general you would run the socket code in a background thread. Google should find you some samples.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900