服务器不响应tcp,服务器已连接,但无响应TCP套接字编程

#!/usr/bin/python2.7

import sys

import socket

import time

import threading

import re

from collections import defaultdict

successor = defaultdict(dict)

peer = int(sys.argv[1])

successor[1] = int(sys.argv[2])

successor[2] = int(sys.argv[3])

serverName = 'localhost'

peerPort = 50000 + int(peer)

address = (serverName,peerPort)

#-------------proceed input string---------------------------

def getFileNum(name):

fileValid = re.match('^request ([0-9]{4})$',name)

if fileValid is None:

print 'invalid file!'

return

else:

hashName = fileValid.group(1)

return hashName

#----------------get request input--------------------------------

def getRequestInput(clientSocketTCP):

while flag == 0:

fileName = raw_input()

hashname = getFileNum(fileName)

if hashname is not None:

hashname = re.sub('^(0)*','',hashname)

hashnum = int(hashname) % 256

info = 'FILE_REQUEST'+str(hashname) + ','+ str(hashnum) + ','+ str(peer) + ',1'

clientSocketTCP.send(info)

print 'File request message for '+ str(hashname) + ' has been sent to my successor.'

clientSocketTCP.close()

#-------------------send file to successor---------------------------

def sendRequestToS(clientSocketTCP):

global important

while flag == 0:

if important:

an = re.match('^FILE_REQUEST([0-9]{4}),',important)

if an:

hashname = an.group(1)

clientSocketTCP.send(important)

print 'File request message for '+ str(hashname) + ' has been sent to my successor.'

important = ''

clientSocketTCP.close()

#-----------------------find file-------------------------------------

def findF():

global flag

global important

while flag == 0:

if re.match('^FIND',important):

obj = re.match('^FIND[0-9]{1,3},([0-9]{4}),([0-9]{1,3})',important)

n = int(obj.group(2))

info = important

ff = threading.Thread(target=clientTCPTemp,args=(n,info))

ff.start()

ff.join()

important = ''

#--------------------set up client temporary---------------------------

def clientTCPTemp(n,info):

global flag

clientConn = False

clientSocketTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serverPortTCP = 50000 + n

print serverPortTCP

while not clientConn:

try:

clientSocketTCP.connect((serverName,serverPortTCP))

clientConn = True

print "Now client connection works!!!!!"

except:

print "fail"

clientSocketTCP.send(info)

print info

print 'A response message, destined for peer '+ str(n) +', has been sent.'

clientSocketTCP.close()

#--------------------TCP server---------------------------------------

def serverTCP():

global flag

global serverSetUp

global important

serverSocketTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serverConn = False

while not serverConn:

try:

serverSocketTCP.bind((serverName,peerPort))

serverConn = True

serverSocketTCP.listen(2)

serverSetUp = 0

print 'The server is ready to receive'

except:

pass

while flag == 0:

connectionSocket, addr = serverSocketTCP.accept()

print 'connect by'+ str(addr)

threeinfo = connectionSocket.recv(1024)

print threeinfo

if re.match('^FILE_REQUEST',threeinfo):

obj = re.match('^FILE_REQUEST([0-9]{4}),([0-9]{1,3}),([0-9]{1,3}),([01])$',threeinfo)

if obj is not None:

filename = obj.group(1)

hashn = int(obj.group(2))

peerID = int(obj.group(3))

endCircle = int(obj.group(4))

if peer < hashn and endCircle:

print 'File ' +filename +' is not stored here. '

important = threeinfo

if peer > successor[1]:

important = re.sub('1$','0',threeinfo)

else:

print 'File '+ filename+' is here.'

important = 'FIND'+str(peer)+','+ filename +','+ str(peerID)

elif re.match('^FIND',threeinfo):

dest = re.match('^FIND([0-9]{1,3}),([0-9]{4})','',threeinfo)

fromP = dest.group(1)

fileP = dest.group(2)

print 'Received a response message from peer '+fromP+', which has the file '+fileP

connectionSocket.send('i receive from you------------------------')

print sen

connectionSocket.send('can you hear me?')

connectionSocket.close()

#--------------------TCP client----------------------------------------

def clientTCP(n):

global flag

global serverSetUp

global important

clientConn = False

# while serverSetUp == 1:

# pass

clientSocketTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serverPortTCP = 50000 + n

while not clientConn:

try:

clientSocketTCP.connect((serverName,serverPortTCP))

clientConn = True

print "Now client connection works!!!!!"

except:

pass

try:

rt = threading.Thread(target=getRequestInput,args=(clientSocketTCP,))

sr = threading.Thread(target=sendRequestToS,args=(clientSocketTCP,))

ff = threading.Thread(target=findF,args=())

rt.start()

sr.start()

ff.start()

except:

print 'thread failed'

sen = raw_input()

clientSocketTCP.send(sen)

m = clientSocketTCP.recv(1024)

print m

clientSocketTCP.close()

#----------------start thread---------------------------------

#------adapt from https://www.tutorialspoint.com/python/python_multithreading.html --------

flag = 0

serverSetUp = 1

important = ''

findFile = False

try:

serTCP = threading.Thread(target=serverTCP,args=())

cliTCP = threading.Thread(target=clientTCP,args=(successor[1],))

serTCP.start()

cliTCP.start()

except:

print "thread can not be set up"

while flag == 0:

try:

pass

except KeyboardInterrupt:

flag = 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值