HI guys

I am need to connect my pc with the PLC on the Ethernet Connection and I don't know about socket programing,
I have very search for this work and find any code in this site and usage then.
but have a some problem.

this is my usage code:

    private void timer1_Tick(object sender, EventArgs e)
    {
        IPAddress ipAd = IPAddress.Parse("192.168.0.1");
        TcpListener newList = new TcpListener(ipAd, 2000);
        newList.Start();/////  *****
        Socket s = newList.AcceptSocket();
        byte[] b = new byte[100];
        int k = s.Receive(b);
        textBox1.Text = k.ToString();
        ASCIIEncoding asen = new ASCIIEncoding();
        s.Send(asen.GetBytes(textBox1.Text));
        s.Close();

    }

I don't know this code is right, pls tell me that code is true or not?
and when run this code show this error as *** line:
" The requested address is not valid in its context"

detaile of this connection:
Pc IP: 192.168.0.2
PLC IP: 192.168.0.1
Port: 2000

Pls help me
tnx

and I use a another code like this:

private void timer1_Tick(object sender, EventArgs e)
        {
           IPEndPoint m_IpEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 2000);
            Socket m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_Socket.Connect(m_IpEndPoint);
            NetworkStream m_Stream = new NetworkStream(m_Socket);
            m_Stream.Write(System.Text.Encoding.ASCII.GetBytes("Hello world!"), 0, 12);
            byte[] read_buffer = new byte[4096];
            m_Stream.Read(read_buffer, 0, 4096);
            textBox1.Text = read_buffer.ToString(); 
            m_Stream.Close(); 
            m_Socket.Shutdown(SocketShutdown.Both);
            m_Socket.Close();
        }

but when connected line showes this error:
"
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.0.1:2000

"

pls help me
tnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.