サーバーがクライアントにプログラムを送信し、クライアントが受信したプログラムを実行するクライアントサーバープログラムを作成しました。この場合、OpenGLの線描画プログラムです。問題は、サーバおよびクライアントを実行すると、プログラム全体、すなわちサーバおよびクライアントへのプログラムの送信が時々行われるが、時には実行が行われないことである。クライアントが接続され、その接続が滞ってしまいます。プログラムは停止しません。私の友人システムでも同じことができますが、私の場合は時には(たいていの場合、それは習慣にならない)動作します。理由は何でしょうか?何か不足していますか? ネットワークプログラミングの問題
私のサーバーコード: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', 4500))
#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()
私のクライアントコード:
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
OpenGL.ERROR_CHECKING=False
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', 9000))
client.send("connected")
a=client.recv(1024)
b=a
f=1
while f:
print 'sdf'
a = client.recv(1024)
print 'qwe'
if a=="#p":
f=0
break
b+=a
print b
exec(b)
client.close()
ConnectionThread().start()
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
OpenGL.ERROR_CHECKING=False
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', 9000))
client.send("connected")
a=client.recv(1024)
b=a
f=1
while f:
a = client.recv(1024)
print 'qwe'
if a=="#p":
f=0
break
b+=a
print b
exec(b)
client.close()
ConnectionThread().start()
シングルスペースインデント...実際にこれを読み取ることはできますか?あなたの目を撫でることなく? – thkala
あなただけがスレッドを実装する必要がある理由はありますか?ツイスト? – Kimvais
@Kimvais:彼は組み込みの 'threading'モジュールを使用しています。これはTwistedよりはるかに軽量ですが、すべてのPythonディストリビューションに標準で付属しています。だから問題はどこにあるの? –