title: Spark Streaming over TCP
body: <p>I'm currently trying to get Spark Streaming over TCP working but I constantly get a "[Errno 111] Connection refused" error...</p>
  
  <pre><code>import socket
  TCP_IP = 'localhost'
  TCP_PORT = 40123
  MESSAGE = "Test data Test data Test data"
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((TCP_IP, TCP_PORT))
  s.send(MESSAGE)
  s.close()
  </code></pre>
  
  <p>Spark part</p>
  
  <pre><code>import time
  from pyspark import SparkContext
  from pyspark.streaming import StreamingContext
  
  ssc = StreamingContext(sc,1)
  
  lines = ssc.socketTextStream('localhost',40123)
  counts = lines.flatMap(lambda line: line.split(" ")).map(lambda x: (x, 1)).reduceByKey(lambda a, b: a+b)
  counts.pprint()
  
  ssc.start()
  </code></pre>