简体   繁体   中英

Data not received by twisted socket connection

I have a twisted server script listening on a unix socket and it receives the data when the client is in twisted but it doesn't work if i send it via a vanilla python socket code.

class SendProtocol(LineReceiver):
"""
This works
"""
  def connectionMade(self):
    print 'sending log'
    self.sendLine(self.factory.logMessage)

  if __name__ == '__main__':

  address = FilePath('/tmp/test.sock')
  startLogging(sys.stdout)

  clientFactory = ClientFactory()
  clientFactory.logMessage = 'Dfgas35||This is a message from server'
  clientFactory.protocol = SendProtocol

  port = reactor.connectUNIX(address.path, clientFactory)
  reactor.run()

But this doesn't (server doesn't get any data)

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

sock_addr = '/tmp/test.sock'
try:
   sock.connect(sock_addr)
except socket.error, msg:
  print >> sys.stderr, msg
  sys.exit(1)

sock.setblocking(0) # not needed though tried both ways
print 'connected %s' % sock.getpeername()
print 'End END to abort'

while True:
  try:
    line = raw_input('Enter mesg: ')
    if line.strip() == 'END':
      break

    line += '\n'
    print 'sending'
    sock.sendall(line)
  finally:
    sock.close()

Your two client programs send different data. One sends \\r\\n -terminated lines. The other sends \\n -terminated lines. Perhaps your server is expecting \\r\\n -terminated lines and this is why the latter example doesn't appear to work. Your non-Twisted example also closes the socket after the first line it sends but continues with its read-send loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM