none
TCP Server/Client client wont receive data sim card RRS feed

  • Question

  • Hi 

    I have established the connection between the client and server, however when i try to send info from the client (GSM) to the server, the server would not read the data. When using the sockettester the server reads the data .

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                while(client.Connected)
                {
                    try
                    {
                        receive = STR.ReadLine();
                        this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("You : " + receive + "\n"); }));
                        receive = "";
                    }
                    catch(Exception x)
                    {
                        MessageBox.Show(x.Message.ToString());
                    }
                }
            }

    This is the code i use to read the data.

    Thank you for your time and Effort.


    Wednesday, February 8, 2017 5:43 AM

All replies

  • Hi imdeic,

    Thank you fro posting here.

    According to your question is more related to socket, I will move it to Windows Communication Foundation, Serialization, and Networking forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 9, 2017 6:33 AM
  • Hi imdeic,

    Based on your description, it seems you are sending data from client which is mobile to server. Could you share us what your client is and what your service is? Is the server a winform application?

    >> When using the sockettester the server reads the data .

    It seems there is no problem in server side. Could you share us what sockettester is? Is it in server side or any other places?

    It would be helpful if you could share us how you send data from client(GSM).

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 9, 2017 6:47 AM
  • Hi Edward, Thank you for replying to my question.

    This is the code I am using

                                     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;





    namespace server
    {


            public partial class Form1 : Form
        {
            private TcpClient client;
            public StreamReader STR;
            public StreamWriter STW;
            public string receive;
            public String text_to_send;
            public Form1()
            {
                InitializeComponent();
                IPAddress[] localIP = Dns.GetHostAddresses(Dns.GetHostName());
                foreach(IPAddress address in localIP)
                {
                    if(address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        textBox3.Text = address.ToString();
                    }
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {

            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

                private void button2_Click(object sender, EventArgs e)
                {


                    TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(textBox4.Text));
                    listener.Start();
                    client = listener.AcceptTcpClient();
                    STR = new StreamReader(client.GetStream());
                    STW = new StreamWriter(client.GetStream());
                    STW.AutoFlush = true;

                    backgroundWorker1.RunWorkerAsync();
                    backgroundWorker2.WorkerSupportsCancellation = true; 


                }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                while(client.Connected)
                {
                    try
                    {
                        receive = STR.ReadLine();
                        this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("You : " + receive + "\n"); }));
                        receive = "";
                    }
                    catch(Exception x)
                    {
                        MessageBox.Show(x.Message.ToString());
                    }
                }
            }

            private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
            {
                if (client.Connected)
                {
                    STW.WriteLine(text_to_send);
                    this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.AppendText("Me : " + text_to_send + "\n"); }));
                }
                else
                {
                    MessageBox.Show("Send FAiled");   
                }
                backgroundWorker2.CancelAsync();

            }

            private void button3_Click(object sender, EventArgs e)
            {
                client = new TcpClient();
                IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(textBox5.Text), int.Parse(textBox6.Text));

                try
                {
                    client.Connect(IP_End);
                    if(client.Connected)
                    {
                        textBox2.AppendText("Connected To Server" + "\n");
                        STW = new StreamWriter(client.GetStream());
                        STR = new StreamReader(client.GetStream());
                        STW.AutoFlush = true;

                        backgroundWorker1.RunWorkerAsync();
                        backgroundWorker2.WorkerSupportsCancellation = true;

                    }

                }catch(Exception x)
                {
                    MessageBox.Show(x.Message.ToString());
                }
            }

            private void Send_Click(object sender, EventArgs e)
            {
                if (textBox1.Text != "")
                {
                    text_to_send = textBox1.Text;
                    backgroundWorker2.RunWorkerAsync();
                }
                textBox1.Text = "";

            }
        }
    }

    Thank you for your time and effort.

    Sunday, February 12, 2017 3:56 AM
  • Hi imdeic,

    >> however when i try to send info from the client (GSM) to the server, the server would not read the data

    How did you send info from client(GSM)? Based on your above code, it seems to be Server code which is used to listen the incoming message and a client in winform application which uses TcpClient to send message.

    Do you mean there are some issues related with above code? If so, could you share us more detailed information like operation steps about buttons and unexpected result?

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 13, 2017 7:17 AM