3

这是一个简单的套接字程序,它有一个服务器和一些客户端。客户端通过简单的 RSA 加密发送他们的文本,然后服务器端解密句子,然后将解密的句子发送回客户端。

服务器:

import socket
import sys
from thread import *
from math import *
from random import *
import random
HOST = ''   
PORT = 8888
size=2**16
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

def decoder(codedString,d,n):
    breakcoded=[]
    coded=(codedString)
    #print coded;
    for i in range (len(coded)):
            breakcoded.append(chr(((int(coded[i])**d) % n)+48))
    stri= ""
    for i in range (len(breakcoded)):
            stri+=breakcoded[i]
    return stri

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
    #infinite loop so that function do not terminate and thread do not end.
    while True:    
    #Receiving from client
        data = conn.recv(1024)
##        data=s.recv(size)
    l = int (data)
    #print l
    coded=[]
    i=0
    data1=conn.recv(size)
print 'Recieved n: ',data1
n = int (data1)
data2=conn.recv(size)
print 'Recieved d: ',data2
d = int (data2)
for i in range (l):
            data3=conn.recv(size)
            #print 'Recieved: ',data3
            print
            coded.append(data3)       
    print 'coded string has been recieved....'
    print ('coded string: ' , coded)
d= decoder(coded,d,n)
print d        
    reply = 'OK... your message decrypted as: ' + d
    if not d:
        break
    conn.sendall(reply)
#came out of loop
conn.close()
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #start new thread takes 1st argument as a function name to be run, second is the tuple of     arguments to the function.
    start_new_thread(clientthread ,(conn,))

s.close()

客户:

import socket
from math import *
from random import *
host='localhost'
port=8888
size=2**16
def declare():
        a = sample([5,3],2)
        return (a[0],a[1])
def coder(input_message):
        (p,q)=declare()
        for i in range (1):
                p=2**p-1
        for i in range (1):
                q=2**q-1
        #print p
        #print q
        #print ("n= ",p*q)
        #print a
        def gcd(a,b):
                if a%b==0:
                    return b
                elif a%b > 0:
                    s=a%b
                        return gcd(b,s)
                else:
                    raise ValueError
        n=p*q
        phi=(p-1)*(q-1)
        e=2
        while gcd(phi,e)!=1:
                e+=1
        d=1
        while ((e*d)%phi)!=1:
                d+=1
        public_key=(n,e)
        special_key=(n,d)
        ListOfAsciis=[]
        coded=[]
        breakcoded=[]
        for i in input_message:
                ListOfAsciis.append(ord(i)-48)
        for j in ListOfAsciis:
                coded.append((j**e)%n)
        #print ("e= ",e)
        #print ("d= ",d)
        #print ("coded= ",coded)
        for i in coded:
                breakcoded.append(chr(((i**d) % n)+48))
        #print ('n= ' , n)
        #print str(coded)
        #print coded
        return (d,n,str(coded[0]))
def decoder(codedString,d,n):
        #input_d= input("please enter your private key d: ")
        #input_n= input("please enter your private key n: ")
        #d = int (input_d)
        #n = int (input_n)
        breakcoded=[]
        coded=(codedString)
        print coded;
        for i in range (len(coded)):
                breakcoded.append(chr(((int(coded[i])**d) % n)+48))

        stri= ""
        for i in range (len(breakcoded)):
                stri+=breakcoded[i]
        return stri
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print 'socket created'
s.connect((host,port))
print 'connected'
data=s.recv(size)
print 'Recieved: ', data
while True:
    input_message= raw_input("please enter your message: ")
    message = list(input_message)
    s.send(str(len(message)))
##  (p,q)=declare()
    #n=p*q
    (d,n,c)=coder('i')

    n=str(n)
print "                  ",
print "                  ",
print "                  ",
print "                  ",
s.send(n)
print "                                                            ",
d=str(d)
s.send((d))
print "   ",
print "   ",
print "   ",
for i in range (len(message)):
            (d,n,c)=coder(input_message[i])
            print "                                                           ",
            print "   ",
            print "   ",
            s.send((c))
    print 'coded string has been sent to the server....'
    data=s.recv(size)
    print 'Recieved: ', data

现在的问题是程序有时可以正常工作,有时不能!在错误的情况下,服务器端通过一个recv 获得客户端的两个发送项。我应该怎么

4

1 回答 1

17

这是 TCP 的固有部分。流套接字是字节流,而不是消息流。

所以,事情完全按照他们应该的方式工作。如果您想通过 TCP 流发送一系列消息,这与将一系列对象保存到文件中的问题完全相同——您需要某种方式来分隔消息。这可能就像使用文本流一样简单,其中换行符分隔消息,或者它可能是一个复杂的协议,但它必须是某种东西

有关更多详细信息,请参阅此博客文章

于 2013-07-15T21:49:22.373 回答