2017-10-17 158 views
0

我想使简单的客户端服务器程序发送和接收使用TCP套接字的文件表单服务器。至于从服务器获取文件不是问题,服务器将创建一个具有相同名称的文件并将该文件中的数据放入该文件中,但是当涉及到将文件放入服务器时,有时它会很好,但总是有机会,因此服务器正在获取文件名以及文件内容,而不是将其写入文件,它将文件名和内容写入新文件名,并且该文件保持空白。如果有人能提出任何解决方案,将会有很大帮助。Tcp套接字发送和接收文件,使用python

server.py

import socket 
import sys 
HOST = 'localhost' 
PORT = 3820 

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
socket.bind((HOST, PORT)) 

socket.listen(1) 
while (1): 
    conn, addr = socket.accept() 
    print 'New client connected ..' 
    reqCommand = conn.recv(1024) 
    print 'Client> %s' %(reqCommand) 
    if (reqCommand == 'quit'): 
     break 
    #elif (reqCommand == lls): 
     #list file in server directory 
    else: 
     string = reqCommand.split(' ', 1) #in case of 'put' and 'get' method 
     reqFile = string[1] 

     if (string[0] == 'put'): 
      with open(reqFile, 'wb') as file_to_write: 
       data=conn.recv(1024) 
       while True: 
        if not data: 
         break 
        else: 
         file_to_write.write(data) 
         data=conn.recv(1024) 
        file_to_write.close() 
        break 
      print 'Receive Successful' 
     elif (string[0] == 'get'): 
      with open(reqFile, 'rb') as file_to_send: 
       for data in file_to_send: 
        conn.sendall(data) 
      print 'Send Successful' 
    conn.close() 

socket.close() 

client.py

import socket 
import sys 

HOST = 'localhost' # server name goes in here 
PORT = 3820 


def put(commandName): 
    socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    socket1.connect((HOST, PORT)) 
    socket1.send(commandName) 
    string = commandName.split(' ', 1) 
    inputFile = string[1] 
    with open('clientfolder/'+inputFile, 'rb') as file_to_send: 
     data=file_to_send.read(1024) 
     while(data): 
      socket1.send(data) 
      data=file_to_send.read(1024) 
      file_to_send.close() 
    print 'PUT Successful' 
    socket1.close() 
    return 


def get(commandName): 
    socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    socket1.connect((HOST, PORT)) 
    socket1.send(commandName) 
    string = commandName.split(' ', 1) 
    inputFile = string[1] 
    with open('clientfolder/'+inputFile, 'wb') as file_to_write: 
     while True: 
      data = socket1.recv(1024) 
      # print data 
      if not data: 
       break 
      # print data 
      file_to_write.write(data) 
    file_to_write.close() 
    print 'GET Successful' 
    socket1.close() 
    return 


msg = raw_input('Enter your name: ') 
while(1): 
    print 'Instruction' 
    print '"put [filename]" to send the file the server ' 
    print '"get [filename]" to download the file from the server ' 
    print '"ls" to list all files in this directory' 
    print '"lls" to list all files in the server' 
    print '"quit" to exit' 
    sys.stdout.write('%s> ' % msg) 
    inputCommand = sys.stdin.readline().strip() 
    if (inputCommand == 'quit'): 
     socket.send('quit') 
     break 
    # elif (inputCommand == 'ls') 
    # elif (inputCommand == 'lls') 
    else: 
     string = inputCommand.split(' ', 1) 
     if (string[0] == 'put'): 
      put(inputCommand) 
     elif (string[0] == 'get'): 
      get(inputCommand) 
#current working directory is server location 
#get will get file from current directory to clientfolder directory. 

回答

0

TCP是流协议,所以你要设计的消息闯入你的协议。例如:

s.send('put filename') 
s.send('data') 

可接收为:

s.recv(1024) 
# 'put filenamedata' 

所以缓冲区接收的数据,并只能提取完整的消息。一种方法是在消息前发送消息的大小。

+0

尝试先发送文件名,但不是解决问题,文件名称正在复制 –