I have a plc with eternet port.
I want to connect between PLC and my PC through TCP/IP.
How can it be done ?
using (TcpClient client = new TcpClient())
{
Console.WriteLine("Attempting to connect to the server ","on port 8000.");
client.Connect(IPAddress.Parse("192.168.1.97"), 8000);
}
The question needs a bit more details... but I will write some considerations:
a modern PLC typically can manage an TCP/IP stack and most has already an integrated webserver at some port (80?), depending on the kind of PLC you can have some info and maybe access to I/O data.
The connection is straightforward like any other TCP/IP device providing an IP and a port address, if the socket does not connect means there is nothing responding
If you are not connecting to an already working server in the PLC, as some noted in comments You should implement in the PLC program a server loop that answers to your requests, you have to check vendor documentation
Related
How does one connect to a client from a remote server using UDP if all we can get is the global IP (the router's IP)?
I am getting the global IP of the client using the code:
string externalIp = new WebClient().DownloadString("http://icanhazip.com");
Is there a way to get a client's local IP? Also to which IP address should my server connect when using UDP?
What I ended up doing is to use a game service like GameSparks to send TCP and UDP port information from my server to each game client. The game client would then connect to the TCP and UDP port which would give the server knowledge of the client's endpoint. This way a two way communication can be established with both TCP and UDP.
How can I send data from my C# socket program to my clients?
Currently I have two programs, server and client.
I start my server, then my clients connect to server, in this way they are connected together, but now I want to send directly from server to client?
As my client cannot start its own connection, of course I have IP and port of my client, how can I send data from my server to a client with known IP and port?
Thanks.
Since your clients are not computer programs you can control, you really have no chance but to contact them from the server. If your clients can handle TCP communications, you need to treat them as servers, and Connect from the server to each client (open the socket on the server side, and Connect to each client IP and port).
It's possible that your clients understand UDP and not TCP. That is actually going to be easier for you, as you only need to create one UDP socket, and use SendTo to send a data to each client (one SendTo call per client).
Let's just hope your clients aren't stuck on the Ethernet level...
I would say that COM-ports are slightly easy to communicate than implementing TCP/IP protocol on your device. Could your device read/write its COM-port?
This question already has answers here:
How do I find the port number assigned to a UDP client (in .net/C#)?
(2 answers)
Closed 9 years ago.
I am creating a client server application using c sharp. The server is using tcplistener with fixed port number. the client connect to the server using tcpclient. Once connected, both client and server communicate using this connection. The application then create new udp connection to send and receive message. Since the server suppose to accept multiple connection from single client, i have to differentiate each connection with different port. In order to do this, i have to first
1. at server, create a udpclient (automatically use unused udp port at the server).
2. sends the port number used by the server udpclient to the client.
3. the client sends data to the server using specified port number.
The problem is, how to create a udpclient where you can know the port number used?
Here are the answer to my questions.
UdpClient udpClient = new UdpClient(0));
Console.WriteLine("UDP port : " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port.ToString());
0 as the constructor parameter set the app to automatically find free udp port. ((IPEndPoint)udpClient.Client.LocalEndPoint)).Port.ToString() is used to find the port number.
I believe you can use the Socket.RemoteEndPoint property to know what the IP/Port of the client connected to the server is (you know your local IP/port because you started the socket on that port, but it is also available through the LocalEndPoint property.
Also see the MSDN UdpClient for a simple example on how to use the UdpClient properly.
I think you cannot use UdpClient at server side to achieve your goal, as it does not have a Bind method to bind to an IPEndPoint.
You should use a Socket object to do that, which allows you to monitor a port for incoming UDP messages. Then no doubt you can tell the client which port the server is monitoring.
I'm trying to write a client-server console application on C# using UDP.
And i have one question.
So, when i send command from client to server - servers must have a read IP adress.
client must get some settings from server,but in this case client must have real IP adress too. Other application like games do not require client real IP. What must do I to it work in such a way?
Games either use the host computer with a public IP as a server or the server itself as a server.
A client with private IP establishes a connection with a server and the server then uses the connection to return data to a client (similar to browsers).
In C# you could use the NetworkStream class for TCP and UdpClient for UDP.
If I understand your question correctly: you could use Socket.ReceiveFrom, which will tell the server the IP address of the client who is sending a request.
Using Socket.BeginReceiveFrom / EndReceiveFrom the server gets the IP address of the client. If the server needs to reply, it uses the IPEndPoint.
Is there a way to locate a TCP Server running under local network using raw sockets and C#?
-The Client searching for the Server is running under the same local network as the Server and it knows the port the Server is running with.
-is it by Broadcast?
Yes it's possible to do this with the Socket class if the server sends UDP broadcasts on a periodic basis or in response to a solicitation. The client would have to listen for UDP broadcasts and optionally send a solicitation to discover the server.
MSDN: Using UDP Services
If you're not strictly limited to the Socket class, have a look at DNS-SD / Zero configuration networking. This has been invented for the exact purpose of discovering services on a local network.