Python服务器和c#客户端

时间:2017-03-06 14:59:29

标签: c# python sockets server client

我正在使用python服务器和C#客户端进行服务器/客户端架构的套接字通信!此应用程序用于通过套接字传输文件。我得到的问题是用python编写的服务器永远不会从(而数据!=“”)循环中退出,并且在接收到整个文件之后它会在指令上被阻塞(data = client.recv(128))

这是我的客户代码:

        try
        {
            using (StreamReader fs = new StreamReader(path,System.Text.Encoding.ASCII,false,128))
            {    
                while (str !="")
                {
                    Byte[] sentBytes= new Byte[data];
                    str = fs.ReadToEnd();
                    sentBytes = Encoding.ASCII.GetBytes(str);
                    clientSocket.Send(sentBytes);                             
                }
            }
        }
        catch (Exception ex)
        {             
            Console.WriteLine(ex.ToString());              
        }

这是服务器代码:

import socket
import timeit

port=15555
host = "127.0.0.1"
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host,port))

socket.listen(5)
print ("waiting for connection..")

while True:
    client, addr = socket.accept()
    print ("Connection established to {}".format(addr)) 
    with open('received_file.csv','wb') as f:
        print ("file opened")
        size=0
        start=timeit.default_timer()
        data = client.recv(128)
        while data!="":         
            print ('receiving data= ',(size))
            f.write(data)
            size=size+128
            data = client.recv(128)
        stop=timeit.default_timer()
    f.close()
    time=stop-start
    print ("sending finished! time= ", time , " seconds")
    client.close()
socket.close()
请帮忙!

0 个答案:

没有答案