title: Python Socket over wifi in raspberry pi
body: <p>I have programmed a socket in python. Basically there are 2 raspberry pi's who talk to each other and send the gpio data over wifi using a socket. The code works perfectly fine at times but sometimes it either doesn't work or shows a lot of lag. What can the possible issue. Have I missed something. I am new to networking and python. Please help me!!</p>
  
  <p>The Server code is </p>
  
  <pre><code>#!/usr/bin/python
  import RPi.GPIO as GPIO
  import socket
  HOST='192.168.0.106'
  PORT=5002
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen(1)
  conn, addr=s.accept()
  s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  print 'Connected by', addr
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(04, GPIO.IN)
  GPIO.setup(17, GPIO.IN)
  GPIO.setup(27, GPIO.IN)
  while True:
      if (GPIO.input(04)==True):
          if (GPIO.input(17)==False):
                  if (GPIO.input(27)==False):
                          conn.send('0')
                  elif(GPIO.input(27)==True):
                          conn.send('1')
          elif (GPIO.input(17)==True):
                  if (GPIO.input(27)==False):
                          conn.send('2')
                  elif (GPIO.input(27)==True):
                          conn.send('3')
      elif (GPIO.input(04)==False):
          conn.send('5')
  s.close()
  </code></pre>
  
  <p>The client code is here</p>
  
  <pre><code>#!/usr/bin/python
  import socket
  import RPi.GPIO as GPIO
  HOST='192.168.0.106'
  PORT=5002
  s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((HOST, PORT))
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(02, GPIO.OUT)
  GPIO.setup(03, GPIO.OUT)
  GPIO.setup(11, GPIO.OUT)
  GPIO.setup(10, GPIO.OUT)
  while 1:
      data=s.recv(8096)
      if data=='0':
              print 'Forward'
              GPIO.output(02,True)
              GPIO.output(03, False)
              GPIO.output(11, False)
      elif data=='1':
              print 'Backward'
              GPIO.output(02, False)
              GPIO.output(03, True)
              GPIO.output(11, True)
              GPIO.output(10, False)
      elif data=='2':
              print 'Left'
              GPIO.output(02, False)
              GPIO.output(03, False)
              GPIO.output(11, False)
              GPIO.output(10, True)
      elif data=='3':
              print 'Right'
              GPIO.output(02, True)
              GPIO.output(03, False)
              GPIO.output(11, False)
              GPIO.output(10, False)
      elif data=='5':
              print 'Stop'
              GPIO.output(02, False)
              GPIO.output(03, False)
              GPIO.output(11, False)
              GPIO.output(10, False)
  s.close()
  </code></pre>