I have written a client server program in which the server sends a program to the client, and the client executes the received program. In this case it is a line drawing program in OpenGL. The problem is that on running the server and client the whole program i.e. sending of the program to server and client execution takes place at times, but at times the execution does not take place. The client gets connected and then its stuck. The program doesn't get stopped either.The same works in my friends system,but in mine it works only sometimes(most of the times it wont). What could be the reason? Am I missing something?

My server code:

from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys
import threading
import os
import socket

class ClientThread ( threading.Thread ):
 # Override Thread's __init__ method to accept the parameters needed:
 def __init__ ( self, channel, details ):
  self.channel = channel
  self.details = details
  threading.Thread.__init__ ( self )

 #Codes to be executed when thread is executed:
 def run ( self ):
  a1=self.channel.recv(1024)
  print "client says:"+a1
  print 'Received connection:', self.details [ 0 ]
  finp = open("stringcheck.py","r")
  #self.channel.send("#start\n")
  info = finp.readlines() 
  for record in info:
   self.channel.send(record)
  #self.channel.send(info)
  self.channel.send("#p") 
 

server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '127.0.0.1', 2713) )
#Listens for connections made to socket.
#Specifies maximum number of queued connection.
server.listen ( 5 )
while True:
 channel, details = server.accept()
 #Create an instance of thread class and call its start method.
 ClientThread ( channel, details ).start()

Client code:

from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import os
import socket
import threading
import thread
import subprocess

class ConnectionThread( threading.Thread ):

 def run ( self ):
  
  client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  client.connect( ( '127.0.0.1', 2713) )
  client.send("connected")
  a=client.recv(1024)
  b=a
  f=1
   
  while f: 
   a = client.recv(1024)
   if a=="#p":
    f=0
    break
   b+=a
   
  #print b
  exec(b) 
  client.close()

ConnectionThread().start()

What could the problem be? I searched the net, but looked in the wrong places I think.

Among the possible issues:

1/ The threading.Thread class has a special rule: If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.. See http://docs.python.org/library/threading.html#threading.Thread .This is a frequent source of errors, so you should move line 14 immediately after line 11 in the server code

2/ socket.send() is not guaranteed to send all the data, you should use socket.sendall() instead

3/ The calls to recv() are not guaranteed to yield the same sequence of strings that were passed to send(). The client loop should probably be written

while True: 
   a = client.recv(1024)
   b += a
   if b[-2:] == "#p":
    break

4/ Why does your client code need to start a new thread ? The main thread could receive and execute the code. Otherwise you should wait for the child thread:

conn_thread = ConnectionThread()
conn_thread.start()
conn_thread.join()

Personnaly, I don't like to use raw sockets. You could have considered other solutions (like xmlrpc), which do all the sockets details for you.

Edited 5 Years Ago by Gribouillis: n/a

Thanx for the response...i followed your sugestions1,2 and 3.Then i started the server and client.There was no output.Then i closed the server and then the output appeared.What should i do now?

As Grib said. Its always a good idea to overide and declare just after the

def __ini__()

Not only for threading but all. It will save you from pain.
Also i will like an open server binding. line 31.
with that, the server can be used any where.

Secondly, client side.
number 17-19 i personaly dont like that. Put the logic in a while loop.
Bring them under line 21.

You should perhaps call self.channel.close() on the server side after the #p was sent. Also on the client side, you could close the socket before exec(b). Did the client receive the whole code ?

Another tip (unrelated to your issue), you should call

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

just after the socket creation. This avoids blocking the port between different executions of the server program.

Edited 5 Years Ago by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.