1

我正在尝试对编译服务器进行编程,该服务器编译客户端发送的 C 程序并返回一个目标文件,然后可以在客户端链接和执行该目标文件。这是我的客户端和服务器程序分别

客户端.py:

# Compilation client program

import sys, socket, string


File = raw_input("Enter the file name:") 
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.connect(('localhost', 5000))

csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.connect(('localhost', 5001))

f = open(File, "rb")
data = f.read()
f.close()
ssock.send(File)                 #send filename
ssock.send(data)                 #send file
fd=raw_input("Enter a key to start recieving object file:") 

data=csock.recv(1024)            #receive status
if data=="sucess\n":
    File=File.replace(".c",".o") #objectfile name
    print "Object file, "+File+", recieved sucessfully"
else:
    print "There are compilation errors in " + File
    File="error.txt"             #errorfile name
    print "Errors are reported in the file error.txt"

fobj=open(File,"wb")
while 1:
    data=ssock.recv(1024)        # if any error in c sourcefile then  error gets                                         
                                 # eported in errorfile "error.txt" else objectfile is 
                                 # returned from server
    if not data:break
    fobj.write(data)
fobj.close()
ssock.close()
csock.close()

服务器.py

#Compilation Server program

import subprocess
import socket, time, string, sys, urlparse, os
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.bind(('', 5000))                   
ssock.listen(2)
print 'Server Listening on port 5000'
csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.bind(('', 5001))
csock.listen(2)
print 'Control server listening on port 5001'
client, claddr = ssock.accept()
controlsoc, caddr = csock.accept()


filename=client.recv(1024)        #receive filename
print filename
###############  This code is not working, i'm not getting the reason   #######
###############    I want to receive a file more than 1KB from client   #######
f = open(filename,"wb")           #receive file======
while 1:
    data = client.recv(1024)
    if not data: break
    f.write(data)
f.close()
###############
###############


data="gcc -c " + filename + " 2> error.txt"  #shell command to execute c source file 
                                             #report errors if any to error.txt    
from subprocess import call

call(data,shell=True)                        #executes the above shell command
fil = filename.replace(".c",".o")
if (os.path.isfile(fil))== True:             #test for existence of objectfile 
    data = "sucess\n"                        #no objectfile => error in compilation
    filename = filename.replace(".c",".o")
else:
    data = "unsucessful\n"
    print data+"hi"
    filename = "error.txt"

controlsoc.send(data)
f = open(filename,"rb")
data=f.read()
f.close()
print data

client.send(data)

client.close()
controlsoc.close()

我无法接收多个 KB 的文件。我的代码中是否有任何缺陷,或者我应该如何修改我的代码以实现编写编译服务器的目标。请在这方面帮助我..在此先感谢

4

1 回答 1

1

这里的问题是您假设这ssock.send(File)将导致filename=client.recv(1024)准确读取文件名而不是更多,但实际上接收方不知道文件名在哪里结束,您最终会获得文件名filename变量中的部分数据。

TCP 连接是双向的字节流。它不知道您的消息的边界。一个send可能对应于recv另一侧的多个(反之亦然)。您需要在原始 TCP 之上的应用程序级协议。

在您的情况下,最简单的方法是将表单中的文本行file-size file-name\n作为标题发送。这样,您的服务器不仅可以将标头与文件数据分开(通过换行符),而且还可以知道需要多少字节的文件内容,并为多个文件重用相同的 TCP 连接。

于 2012-08-26T21:09:51.853 回答