473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bidirectional communication over unix socket (named pipe)

Hi, I feel like I should apologize in advance because I must be missing
something fairly basic and fundamental here. I don't have a book on
Python network programming (yet) and I haven't been able to find an
answer on the net so far.

I am trying to create a pair of programs, one (the client) will be
short-lived (fairly) and the second (server) will act as a cache for
the client. Both will run on the same machine, so I think a simple file
socket is the easiest and most reliable method.

The problem I have is that the client can send to the server, but the
server can't send back to the client because it gets this error:

socket.error: (107, 'Transport endpoint is not connected')

This is despite the client waiting on a socket.recv() statement. Is
the client really not connected, or is the server unaware of the
connection? And how do I fix this?

I was able to get this working by switching to AF_INET, but that is not
what I want.

Unix sockets are bidirectional, correct? I have never programmed one,
but I know that programs like clamav use a socket to receive an email
to scan and return the result.

Any help would be greatly appreciated!

Jeff

*** server.py ***
#!/usr/bin/python
import socket
import os, os.path
import time

if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")

server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
server.bind("/tmp/mysock")

while True:
datagram = server.recv(1024)
if not datagram:
break
print datagram
# the preceeding works, and I see the TEST TEST TEST statement the
client sent

time.sleep(2)
# it dies on the next statement.
server.send("Thank you\n")
server.close()
if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
*** client.py: ***
#!/usr/bin/python

import socket

client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
client.connect("/tmp/mysock")
TX = "TEST TEST TEST"
TX_sent = client.send(TX)

if TX_sent <> len(TX): print "TX incomplete"

while True:
print "Waiting..."
datagram = client.recv(1024)
# the client sits here forever, I see the "waiting appear" but
it doesn't advance beyond
# the recv statement.
if not datagram:
break
print "Received: ",datagram

client.close()

Mar 8 '06 #1
6 9763

OK, never fails that I find a solution once I post a problem. If I use
a stream rather than a datagram, it seems to work fine.

So... for my education, how would I make this work with a datagram, if
I insisted on doing it that way?

Mar 8 '06 #2
In article <11**********************@z34g2000cwc.googlegroups .com>,
"J Rice" <ri**********@gmail.com> wrote:
The problem I have is that the client can send to the server, but the
server can't send back to the client because it gets this error:

socket.error: (107, 'Transport endpoint is not connected')

This is despite the client waiting on a socket.recv() statement. Is
the client really not connected, or is the server unaware of the
connection? And how do I fix this?


You can either connect() as well as bind(), or use
sendto(data, file)

Donn Cave, do**@u.washington.edu
Mar 8 '06 #3

Hi Donn,
Not sure I fully understand your suggestion. bind() only works once --
I can't bind again in the client. Same thing with connect() -- once I
issue a connect in the server, it rejects it in the client.

Doing this as a stream works for what I want, but I would like to
understand why it didn't work with datagram.

Mar 8 '06 #4
In article <11**********************@i40g2000cwc.googlegroups .com>,
"J Rice" <ri**********@gmail.com> wrote:
Hi Donn,
Not sure I fully understand your suggestion. bind() only works once --
I can't bind again in the client. Same thing with connect() -- once I
issue a connect in the server, it rejects it in the client.

Doing this as a stream works for what I want, but I would like to
understand why it didn't work with datagram.


Right, bind should be on one end only, as it creates
the actual socket file.

Connect works on either end, with SOCK_DGRAM. From
man 2 connect:

The parameter s is a socket. If it is of type SOCK_DGRAM,
this call specifies the peer with which the socket is to be
associated; this address is that to which datagrams are to
be sent, and the only address from which datagrams are to be
received. If the socket is of type SOCK_STREAM, this call
attempts to make a connection to another socket.

However, even if we're straight on this, I confess I
have only addressed your question about the unconnected
endpoint.

The other part of the problem remains, as I don't know
how to get data to actually go both ways. I was a little
surprised by this, and have not been able to scare up any
explicit documentation, but the only example program I
could find for two-way UNIX domain datagram IPC, uses two
sockets, not one -
http://docs.hp.com/en/B2355-90136/ch07s06.html

Donn Cave, do**@u.washington.edu
Mar 8 '06 #5

Donn Cave wrote:
The other part of the problem remains, as I don't know
how to get data to actually go both ways.


The problem here is that datagram sockets don't send data from one
process to another, they send data from one address to another. In
this case, the client isn't able to receive data through it's socket
because the socket isn't bound any address. Calling connect() on the
datagram socket doesn't give the socket an address to receive data
with, just a default address to send data to. Since the server's
socket is the only socket bound to "/tmp/mysock" it's the only the only
process that can receive datagrams sent to that address. That includes
any datagrams the server itself sends to the address.

You need multiple Unix domain address so each process has an address it
can receive data with, and an address other than it's own that it can
send data to.

Ross Ridge

Mar 9 '06 #6
Thank you, that answers my question! And it works fine with stream, so
I can do what I want as well.

Mar 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rajarshi Guha | last post by:
Hi, I'm having a little trouble when I read from a named pipe. I create a pipe by os.mkfifo('/tmp/mypipe') and then open it for reading with fin = open('/tmp/mypipe','r+')
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
0
by: Spiros | last post by:
Hi everybody I am creating an application using VC++ that runs on a terminal server with 30 end users. The end users will use thin clients as front end machines. The application consists of a main...
3
by: EricR | last post by:
I am trying to use .NET to "tap into" a named pipe created by a non .NET 3rd party application. Specifically, the application is a table loading utility. It opens a named pipe and waits for input....
2
by: FB's .NET Dev PC | last post by:
I am writing two services in VB.NET, one of which needs to send text strings to the other. After reading, I decided (perhaps incorrectly) that named pipes would be the best interprocess...
0
by: EricR | last post by:
I am trying to use .NET to "tap into" a named pipe created by a non .NET 3rd party application. Specifically, the application is a table loading utility. It opens a named pipe and waits for input....
0
by: olaf.dietsche | last post by:
Hi, The system is Windows XP and DB2 v8.1.7. I'm trying to load a text file via named pipe into a table. I have two programs: the first program creates the named pipe, waits for a client...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
0
by: jeb2000 | last post by:
I am rather new to pipes, but I have two c# programs that are communicating via a named pipe. This is working fine on two of my XP Pro SP2 machines. However, when I port to a third machine (also XP...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.