Esempio n. 1
0
	def create_sockets(self, host):
		if self.client_code == 0:
			print 'creating mcast sockets'
			self.sendsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
			self.sendsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
			self.sendsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			self.sendsock.bind(('', self.tcpsock.getsockname()[1]))
			print('binding sendsock to {}'.format(self.sendsock.getsockname()))
			self.recvsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
			self.recvsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
			self.recvsock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
			self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			self.recvsock.bind(('', self.mcast_group[1]))
			print('binding recvsock to {}'.format(self.recvsock.getsockname()))
			mreq = struct.pack("4sl", socket.inet_aton(self.mcast_group[0]), socket.INADDR_ANY)
			self.recvsock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
		elif self.client_code == 1:
			print 'creating unicast sockets'
			self.recvsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
			self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			self.recvsock.bind(('', self.tcpsock.getsockname()[1]))
			print('binding recvsock to {}'.format(self.recvsock.getsockname()))
			self.sendsock = self.recvsock
		else:
			print 'creating sctp sockets'
			self.recvsock = sctp.sctpsocket_udp(socket.AF_INET)
			self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			self.recvsock.bind(('', self.tcpsock.getsockname()[1]))
			print('binding recvsock to {}'.format(self.recvsock.getsockname()))
			self.sendsock = self.recvsock
			self.sendsock.connect(self.server_addr)
Esempio n. 2
0
	def create_sctp(self):
		sctpseq = sctp.sctpsocket_udp(socket.AF_INET)
		sctpseq.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		try:
			sctpseq.bind(('', self.port))
			print('binding sctpsock to {}'.format(sctpseq.getsockname()))
		except Exception as e:
			print(e)
			self.exit(None, None)
		return sctpseq
Esempio n. 3
0
 def create_sockets(self, host):
     if self.client_code == 0:
         print 'creating mcast sockets'
         self.sendsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                       socket.IPPROTO_UDP)
         self.sendsock.setsockopt(socket.IPPROTO_IP,
                                  socket.IP_MULTICAST_TTL, 32)
         self.sendsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self.sendsock.bind(('', self.tcpsock.getsockname()[1]))
         print('binding sendsock to {}'.format(self.sendsock.getsockname()))
         self.recvsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                       socket.IPPROTO_UDP)
         self.recvsock.setsockopt(socket.IPPROTO_IP,
                                  socket.IP_MULTICAST_TTL, 32)
         self.recvsock.setsockopt(socket.IPPROTO_IP,
                                  socket.IP_MULTICAST_LOOP, 0)
         self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self.recvsock.bind(('', self.mcast_group[1]))
         print('binding recvsock to {}'.format(self.recvsock.getsockname()))
         mreq = struct.pack("4sl", socket.inet_aton(self.mcast_group[0]),
                            socket.INADDR_ANY)
         self.recvsock.setsockopt(socket.IPPROTO_IP,
                                  socket.IP_ADD_MEMBERSHIP, mreq)
     elif self.client_code == 1:
         print 'creating unicast sockets'
         self.recvsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                       socket.IPPROTO_UDP)
         self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self.recvsock.bind(('', self.tcpsock.getsockname()[1]))
         print('binding recvsock to {}'.format(self.recvsock.getsockname()))
         self.sendsock = self.recvsock
     else:
         print 'creating sctp sockets'
         self.recvsock = sctp.sctpsocket_udp(socket.AF_INET)
         self.recvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self.recvsock.bind(('', self.tcpsock.getsockname()[1]))
         print('binding recvsock to {}'.format(self.recvsock.getsockname()))
         self.sendsock = self.recvsock
         self.sendsock.connect(self.server_addr)
    ENUMBER = serverTCPConnection.recv(BUFFER_SIZE)
else:
    serverTCPConnection.send('2')
    connectionPort = int(serverTCPConnection.recv(BUFFER_SIZE))
    LNUMBER = serverTCPConnection.recv(BUFFER_SIZE)
    ENUMBER = serverTCPConnection.recv(BUFFER_SIZE)

sendSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#create the sending socket.
outgoing = (connectionIP, connectionPort)
if clientType != 's':
    sendSocket.bind((serverTCPConnection.getsockname()[0],
                     serverTCPConnection.getsockname()[1]))
    sendSocket.connect(outgoing)
else:
    sendSocket = sctp.sctpsocket_udp(socket.AF_INET)
    sendSocket.bind((serverTCPConnection.getsockname()[0],
                     serverTCPConnection.getsockname()[1]))
    sendSocket.listen(1)

if clientType == 'm':
    #Creating a multicat address to listen to.
    receiveSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    receiveSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    receiveSocket.bind((connectionIP, connectionPort))
    host = socket.gethostbyname(socket.gethostname())
    receiveSocket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF,
                             socket.inet_aton(host))
    receiveSocket.setsockopt(
        socket.SOL_IP, socket.IP_ADD_MEMBERSHIP,
        socket.inet_aton(connectionIP) + socket.inet_aton(host))
Esempio n. 5
0
def startServer(port):
  l=generateNumber()
  e=generateNumber()
  p=generateMulticastGroupPort()
  m=generateMulticastGroupIP()
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind(('', port))
  s.listen(1)
  u = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  u.bind((socket.gethostname(), p))
  (mr,ms) = startMulticastReceiver(m, p)
  sk = sctp.sctpsocket_udp(socket.AF_INET)
  sk.bind(('', port))
  sk.listen(1)
  
  print("Using l=" + str(l) + ' e=' + str(e) + ' p=' + str(p) + ' m=' + str(m) + 
        "\ns on %s" % str(s.getsockname()) + " u on %s" % str(u.getsockname()) + 
        "\nmr on %s" % str(mr.getsockname()) + ' sk on %s' % str(sk.getsockname()))
  
  uList=[]
  mList=[]
  sList=[]
  signal.signal(signal.SIGINT, lambda signum,frame: printConnected(uList, mList, sList))#ctrl-c
  signal.signal(signal.SIGQUIT, lambda signum,frame: close([s,u,mr,ms,sk]))#ctrl-\
  last=''
  socket_list = [sys.stdin, s, u, mr, sk]
  while True:
    try:
      read_sockets,_w,_e = select.select(socket_list,[],[])
      for sock in read_sockets:
        if sock == s:
          sockfd, _addr = s.accept()
          username = sockfd.recv(1024)
          sockfd.settimeout(2)
          try: #TCP self buffers, and usually appends the 0 or 1 on username. Catch that case here
            clientType = int(sockfd.recv(1))
          except:
            clientType = int(username[-1])
            username = username[0:-1]
          sockfd.settimeout(None)
          socket_list.append(handleNewClient(sockfd, clientType, username, mList, uList, sList, m, p, l, e))
        elif sock == u:
          data,address = u.recvfrom(1024)
          if last != data:
            last=data
            handleClientMessage(data,address, u,m, p, l, e, uList, mList, sList, ms)
        elif sock == mr:
          data,address = mr.recvfrom(1024)#note was u in a3
          if last != data:
            last=data
            handleClientMessage(data,address, mr, m, p, l, e, uList, mList, sList, ms)
        elif sock == sk:
          address,_flags,data,_notif = sk.sctp_recv(1024)
          if last != data:
            last=data
            handleClientMessage(data,address, sk, m, p, l, e, uList, mList, sList, ms)
        elif sock == sys.stdin:
          if sys.stdin.readline() == 'exit':
            close([s,u,mr,ms,sk])
        else:
          socket_list.remove(handleOther(sock, uList, mList, sList, m, p, l, e))
    except (select.error, socket.error) as ex:
      #Ctrl-c perhaps, just pass
      if ex[0] == 4:#catch interrupted system call, do nothing
        continue
      else:
        raise
print 'SCTP/UNICAST Host: ', host, 'port: ', MULTI_CAST_PORT
print 'Multicast Host: ', MULTI_CAST_IP, 'Multicast port: ', MULTI_CAST_PORT
print 'L  = ', LNUMBER
print 'E  = ', ENUMBER

#create the UDP socket to relay messages to other clients
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udpSocket.bind(('', MULTI_CAST_PORT))
udpSocket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(host))
udpSocket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(MULTI_CAST_IP) + socket.inet_aton(host))



#create the SCTP socket
stcpSocket = sctp.sctpsocket_udp(socket.AF_INET)
stcpSocket.bind(('', MULTI_CAST_PORT))
stcpSocket.listen(1)

#create the TCP sockets that clients will use to connect to the server
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSocket.bind(('', sport))
tcpSocket.listen(1)



mlist = []
ulist = []
slist = []
    
Esempio n. 7
0
print 'SCTP/UNICAST Host: ', host, 'port: ', MULTI_CAST_PORT
print 'Multicast Host: ', MULTI_CAST_IP, 'Multicast port: ', MULTI_CAST_PORT
print 'L  = ', LNUMBER
print 'E  = ', ENUMBER

#create the UDP socket to relay messages to other clients
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udpSocket.bind(('', MULTI_CAST_PORT))
udpSocket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF,
                     socket.inet_aton(host))
udpSocket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP,
                     socket.inet_aton(MULTI_CAST_IP) + socket.inet_aton(host))

#create the SCTP socket
stcpSocket = sctp.sctpsocket_udp(socket.AF_INET)
stcpSocket.bind(('', MULTI_CAST_PORT))
stcpSocket.listen(1)

#create the TCP sockets that clients will use to connect to the server
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSocket.bind(('', sport))
tcpSocket.listen(1)

mlist = []
ulist = []
slist = []


#Methond for building client list string
def getListString():
    LNUMBER = serverTCPConnection.recv(BUFFER_SIZE)
    ENUMBER = serverTCPConnection.recv(BUFFER_SIZE)
else:
    serverTCPConnection.send('2')
    connectionPort = int(serverTCPConnection.recv(BUFFER_SIZE))
    LNUMBER = serverTCPConnection.recv(BUFFER_SIZE)
    ENUMBER = serverTCPConnection.recv(BUFFER_SIZE)
    
sendSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#create the sending socket.
outgoing = (connectionIP, connectionPort)
if clientType != 's' :
    sendSocket.bind((serverTCPConnection.getsockname()[0], serverTCPConnection.getsockname()[1]));
    sendSocket.connect(outgoing)
else :
    sendSocket = sctp.sctpsocket_udp(socket.AF_INET)
    sendSocket.bind((serverTCPConnection.getsockname()[0], serverTCPConnection.getsockname()[1]));
    sendSocket.listen(1)


if clientType == 'm':
    #Creating a multicat address to listen to.
    receiveSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    receiveSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    receiveSocket.bind((connectionIP, connectionPort))
    host = socket.gethostbyname(socket.gethostname())
    receiveSocket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(host))
    receiveSocket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(connectionIP) + socket.inet_aton(host))
else:
    #Using sendsocket as the receiving socket if it is a unicast client.
    receiveSocket = sendSocket