I want to communicate between a python script (server) and a .NET application (client) via TCP/IP.
I’m able to communicate between two python scripts server/client with two instances of VC, but when I switch the python client to a .NET client I get an error message:
“System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it”
I let the .NET app through the firewall so it should not be related to firewall. But I still suspect the issue is on the .NET side or related to Windows security.
The python script looks like this:
from vcScript import *
from socket import *
hostName = gethostbyname(gethostname())
PORT_NUMBER = 4006
SIZE = 1024
def OnStart():
print (“Test server receiving packets from local host IP {0}, via port {1}\n”.format(hostName, PORT_NUMBER))
establishSocket()
def establishSocket():
global mySocket
mySocket = socket( AF_INET, SOCK_STREAM )
mySocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
mySocket.bind( (hostName, PORT_NUMBER) )
mySocket.setblocking(0)
def OnRun():
global mySocket
delay(0.001)
rc = robot.findBehavioursByType(VC_ROBOTCONTROLLER)[0]
mt = rc.createTarget()
mt.MotionType = VC_MOTIONTARGET_MT_JOINT
mt.UseJoints = True
jv = mt.JointValues
while True:
try:
(data,addr) = mySocket.recvfrom(SIZE)
data = data.translate(None, ‘[]’)
print data
continue
data1 = data.split(’,’)
for i in range(len(data1)):
val = float(data1[i])
jv[i] = float(val)
mt.JointValues = jv
controller.moveImmediate(mt)
except error:
delay(0.001)
app = getApplication()
comp = getComponent()
RobotName = comp.getProperty(‘RobotName’).Value
robot = app.findComponent(RobotName)
controller = robot.findBehavioursByType(VC_ROBOTCONTROLLER)[0]
.NET code (VB .NET):
Public Sub StartClient()
' Data buffer for incoming data.
Dim bytes(1024) As Byte
'Connect to a remote device.
Try
' Establish the remote endpoint for the socket.
' This example uses port 4006 on the local computer.
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(1)
Dim remoteEP As IPEndPoint = New IPEndPoint(ipAddress, 4006)
' Create a TCP/IP socket.
Dim sender As Socket = New Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp)
' Connect the socket to the remote endpoint. Catch any errors.
Try
sender.Connect(remoteEP) ' Here it fails
TxtWin.AppendText($"Socket connected to {sender.RemoteEndPoint.ToString()}")
' Encode the data string into a byte array.
Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test<EOF>")
' Send the data through the socket.
Dim bytesSent As Integer = sender.Send(msg)
' Receive the response from the remote device.
Dim bytesRec As Integer = sender.Receive(bytes)
TxtWin.AppendText($"Echoed test = {Encoding.ASCII.GetString(bytes, 0, bytesRec)}")
' Release the socket.
sender.Shutdown(SocketShutdown.Both)
sender.Close()
Catch ane As ArgumentNullException
TxtWin.AppendText($"ArgumentNullException : {ane}")
Catch se As SocketException
TxtWin.AppendText($"SocketException : {se}")
Catch e As Exception
TxtWin.AppendText($"Unexpected exception : {e}")
End Try
Catch e As Exception
TxtWin.AppendText(e.ToString())
End Try
End Sub
Any help is highly appreciated.
Fredrik