# Error Socket connection

**URL:** <https://forum.inductiveautomation.com/t/error-socket-connection/86873>\
**Category:** Ignition\
**Created:** [March 6, 2024, 7:44am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873 "2024-03-06T07:44:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhiraj.pawar](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dhiraj.pawar](https://forum.inductiveautomation.com/u/dhiraj.pawar)\
**Post date:** [March 6, 2024, 7:44am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873/1 "2024-03-06T07:44:07Z")

</div>

Hi Team,  
We are trying to connect device which support TCP communication. We are trying to do communication over the web socket. I write below code sometimes its works correctly but some time its gets "[Errno 10061] Connection refused"

**Code:**

```auto

```

```auto
try:	
	import socket
	import sys
	address='10.192.9.70'
	port=8888
	
	s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	print s
	s.connect((address,port))	
	print s	
	cmd1='LLFT?\r\n'
	s.sendall(cmd1.encode())
	buf=s.recv(256)

	CHTemp= repr(buf)

	CHTemp1= CHTemp.split("\\")
	CHTempScalled= (CHTemp1[0])
	LeftLoop1= (CHTempScalled[1:])
	if LeftLoop1=='' or LeftLoop1==None:
		LeftLoop1=0
	else:
		pass

	s.close
	print LeftLoop1
except:
	exc_type, exc_obj,tb = sys.exc_info()
	errorMessage = "Error:"+ str(exc_obj)
	print(errorMessage)
	lineno = tb.tb_lineno
	s.close
	print(lineno)
	sys.exit()

```

**Error:**  
Error:[Errno 10061] Connection refused

Please help us to resolve issue.

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [March 6, 2024, 7:46am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873/2 "2024-03-06T07:46:53Z")

</div>

Normally connection refused if the port you're trying to go to doesn't accept the connection. Make sure whatever you're connecting to is set to accept your connection.

Also be careful using the python socket module, it has some pretty bad memory leaks. I think the recommendation on the forum is to use the Java based socket as that seems to run a bit better. Using `from java.net import Socket` but you'll need to read the java documentation on how to use it.

---

<div class="post-metadata">

**Author:** ![dhiraj.pawar](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dhiraj.pawar](https://forum.inductiveautomation.com/u/dhiraj.pawar)\
**Post date:** [March 7, 2024, 9:46am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873/3 "2024-03-07T09:46:06Z")

</div>

Hello @Hayden_Watson,  
Im not much aware of java-based socket programming. it will be helpful if share some resource or code example through which we can send command in socket programing.  
I have one doubt it can be work i ignition (Scripting consol, gateway script)?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 7, 2024, 10:42am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873/4 "2024-03-07T10:42:51Z")

</div>

Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). There's a link (pencil icon) below your post to edit and fix it. Thanks.

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [March 8, 2024, 8:30am UTC](https://forum.inductiveautomation.com/t/error-socket-connection/86873/5 "2024-03-08T08:30:33Z")

</div>

As I said, you'll need to look into the java class docs. When I did it we had to convert our data into an array of bytes and send that down the socket, then read a response.

Using code like:

```auto
from java.net import Socket as javaSocket
import jarray
skt = javaSocket(ip_addr, port) #Java Socket declare
out_stream = skt.getOutputStream() # set socket send data stream

b_msg = jarray.array(to_convert_b_msg,'b') # Create Java array object for data transmission
		
out_stream.write(b_msg,0,len(b_msg)) # Write Data

```

This code won't work obviously but should be able to start to look up Java docs on how to implement for your case.

We did find that it massively reduced the memory leaking, so worth the time investment, but my java/Jython skills are pretty poor so maybe isn't super complciated to others.

> [@Using Java.net Socket](https://forum.inductiveautomation.com/t/using-java-net-socket/79962):
>
> Hey all, I'm trying to use the java sockets for some TCP devices. We're using the below code, our devices have a sort of API where they expect a byte array to be written. from java.net import Socket as javaSocket ...... skt = javaSocket(ip\_addr, port) #Java Socket declare, see imports at the top of module skt.setSoTimeout(CONNECTION\_TIMEOUT) #set timeout out\_stream = skt.getOutputStream() # set socket send data stream out\_stream.write(b\_msg) # write data out\_stream.flush() # flush data do…

This is the thread people helped me out on, though took some extra time to figure out how to get it working after
