Saikiran Yerram
Saikiran Yerram

Reputation: 3092

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()

Upvotes: 1

Views: 285

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48315

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.

Upvotes: 3

Related Questions