0

I am trying to create a TCP/IP based socket programming in which I am trying to establish a connection between client and server.

I have an hardware connected and my server is an interface between my form and hardware device.

When I send some commands (ASCII values) the server responds.

My server is running perfectly.

Even my client is getting connected to the server when I click the button and I am also able to send message to server.

Problem arises when I want to receive the message from server.

Their is no response.

Below is my code:

public partial class Form1 : Form
{
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
    NetworkStream serverStream = default(NetworkStream);
    string readData = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnopen_Click(object sender, EventArgs e)
    {
        msg("Client Started");
        clientSocket.Connect("127.0.0.1", 11111);
        label1.Text = "Client Socket Program - Server Connected ...";
    }

    public void msg(string mesg)
    {
        textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox1.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    private void btnRecieve_Click(object sender, EventArgs e)
    {
        try
        {
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox1.Text + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
            Thread ctThread = new Thread(getMessage);
            ctThread.Start();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    private void getMessage()
    {
        while (true)
        {
            serverStream = clientSocket.GetStream();
            int buffSize = 0;
            byte[] inStream = new byte[10025];
            buffSize = clientSocket.ReceiveBufferSize;
            serverStream.Read(inStream, 0, buffSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            readData = "" + returndata;
            msg();
        }
    }

    private void msg()
    {
        if (this.InvokeRequired)
            this.Invoke(new MethodInvoker(msg));
        else
            textBox2.Text = textBox2.Text + Environment.NewLine + " >> " + readData;
    } 

    private void btnClose_Click(object sender, EventArgs e)
    {
        clientSocket.Close();
    } 
}

This is my form design:

Form_Design

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 1
    You've got the classic mistake here of *ignoring the return value* from `Read`. You're probably assuming that you get "messaging" here, but you don't. There's no guarantee that calls to `Write`/`Send` on one side will be matched 1-1 with calls to `Read`/`Receive` at the other. If you want *messaging*, it's up to you to implement that yourself atop the TCP abstraction of a (potentially) infinite stream of bytes, or to move to a higher level protocol that already implements messaging. – Damien_The_Unbeliever Mar 03 '17 at 10:51
  • @Damien why does this question get asked five times a day, and don't we still have a canonical duplicate? [I tried it once](http://stackoverflow.com/questions/23713664/why-does-my-client-socket-not-receive-what-my-server-socket-sends). – CodeCaster Mar 03 '17 at 10:54

0 Answers0