Skip to content

Problem with sockets in c# for eCLR library

Hi there,

so i tried to let a python programm communicate with a c# eCLR library.

From python side, everything is working fine.

Unfortunately, there is no c# Socket Example on the github repository available, so i tried to solve it using this code example from microsoft: https://docs.microsoft.com/de-de/dotnet/framework/network-programming/synchronous-server-socket-example

 

here's a code snippet:

socketproblem1

as you can see i'm using breakpoints and it seems like they are working, but after:

Socket handler = listener.Accept();

nothing happens, no exception, no errormessage, but even the function block in plcnext engineer freezes:

socketproblem12

Even when setting run back to false, the starter variable stays true.

 

In debug mode you can see that the following method is not returning at all.

Socket handler = listener.Accept();



Do you have any hint for solving the problem or some example source available?

 

Best regards

Fspecht

 

 

Comments

  • Hello Felix,

    If I'm reading your code correctly, the part that doesn't seem to be working is executed on every scan cycle while the "starter" input is true. If so, then this is probably not helping ... this section of code should only be called once, when you want to set up the Socket.

    Better would be to do rising-edge detection on the "starter" input before the if statement - e.g. use another bool with Class scope, initialised with FALSE, that "remembers" the value of the "starter" input at the end of the _Process function. The "if" statement in the _Process function then becomes:

    if (starter && !starter_memory) { ... }

    (or something ... my C# is a bit rusty).

    I think you will also need to put your Socket variable at Class scope, outside the _Process() function, to prevent it going in & out of scope (and being garbage collected) between scan cycles.

    Hope this helps ... please let us know how you get on ...

    - Martin.

  • Hello Fspecht,


    since I have the same problem right now, I was wondering if you already solved it? And if so you could post your Solution...

    Thank you and Best regards,


    Kevin

  • Hello to everybody.

    We have same issue with sockets on c++ code. On some network environments when we create socket on plc side, remote client is failing to connect. This happens like in plc next library and in separate linux application.

    I think problem lies somewhere in network stack of plcnext.

    Solution that helps us was to change ip address to another and back after some time after plcreboot.(sudo ifconfig eth0 xxx.xxx.xxx.xxy && sudo ifconfig eth0 xxx.xxx.xxx.xxx)

  • The three cases mentioned above may or may not be related, but I will deal with Artem's first, since it suggests a problem with the network stack on the PLC, which would be independent of PLCnext Engineer and the PLCnext runtime.

    I have tried to reproduce the problem that you (Artem) describe, but I am unable to.

    I have compiled the following C++ code for AXC F 2152 FW 2020.0:

    // Server side C/C++ program to demonstrate Socket programming 
    #include  
    #include  
    #include <sys/socket.h> 
    #include  
    #include <netinet/in.h> 
    #include  
    #define PORT 8080 
    int main(int argc, char const *argv[]) 
    { 
    	int server_fd, new_socket, valread; 
    	struct sockaddr_in address; 
    	int opt = 1; 
    	int addrlen = sizeof(address); 
    	char buffer[1024] = {0}; 
    	char *hello = "Hello from server"; 
    	
    	// Creating socket file descriptor 
    	if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) 
    	{ 
    		perror("socket failed"); 
    		exit(EXIT_FAILURE); 
    	} 
    	
    	// Forcefully attaching socket to the port 8080 
    	if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, 
    												&opt, sizeof(opt))) 
    	{ 
    		perror("setsockopt"); 
    		exit(EXIT_FAILURE); 
    	} 
    	address.sin_family = AF_INET; 
    	address.sin_addr.s_addr = INADDR_ANY; 
    	address.sin_port = htons( PORT ); 
    	
    	// Forcefully attaching socket to the port 8080 
    	if (bind(server_fd, (struct sockaddr *)&address, 
    								sizeof(address))<0) 
    	{ 
    		perror("bind failed"); 
    		exit(EXIT_FAILURE); 
    	} 
    	if (listen(server_fd, 3) < 0) 
    	{ 
    		perror("listen"); 
    		exit(EXIT_FAILURE); 
    	} 
    	if ((new_socket = accept(server_fd, (struct sockaddr *)&address, 
    					(socklen_t*)&addrlen))<0) 
    	{ 
    		perror("accept"); 
    		exit(EXIT_FAILURE); 
    	}
    	valread = read( new_socket , buffer, 1024); 
    	printf("%s\n",buffer ); 
    	send(new_socket , hello , strlen(hello) , 0 ); 
    	printf("Hello message sent\n"); 
    	return 0; 
    } 
    

    (EDIT: unfortunately this forum does not handle angled brackets - the above was copied from the socket server section of this sample)

    I tried starting this socket server:

    (a) After the PLCnext Runtime starts.

    (b) From an init.d script, so before the PLCnext Runtime has completed start-up.

    In both cases a remote client was always able to connect to the PLC on port 8080 and exchange data with the socket server application. So far none of these attempts have failed, so the socket always appears to be created correctly.

    Unless I can reproduce the error it will not be possible to investigate it, so if you are able to give me any other information that might help, I will look at it again.

    ~ Martin.

     

     

  • The problem is that I also can't reproduce this issue in controlled environment. On my table everything is working fine. But I've met this issue for 6 times already. We have 4 controllers in production with this issue.

     

     

  • OK, thanks for the update.

    If you do manage to get any more information that might help us reproduce the problem, please let us know. Otherwise, if anyone else reports a similar problem, and we are able to reproduce it, I will report that here.

    ~ Martin.

Sign In or Register to comment.