def process_request(): """ <Purpose> Server thread for processing connections using select ... """ while True: #Pass list of Inputs, Outputs for select which returns if any activity #occurs on any socket. ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5) #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) inputs.append(newsockfd[2]) #Write to a file... elif sock is filefd: emultimer.sleep(1) assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) inputs.remove(sock) #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #We make the ouput ready, so that it sends out data... if sock not in outputs: outputs.append(sock) else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from readfd's. lind_test_server.close_syscall(sock) inputs.remove(sock) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from monitoring. outputs.remove(sock) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out this socket, it's no longer ready for writing #remove this socket from writefd's. outputs.remove(sock) lind_test_server.close_syscall(serversockfd)
def process_request(): """ <Purpose> Server thread for processing connections using select ... """ while True: #Pass list of Inputs, Outputs for select which returns if any activity #occurs on any socket. ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5) #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) inputs.append(newsockfd[2]) #Write to a file... elif sock is filefd: emultimer.sleep(1) assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) inputs.remove(sock) #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #We make the ouput ready, so that it sends out data... if sock not in outputs: outputs.append(sock) else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from readfd's. lind_test_server.close_syscall(sock) inputs.remove(sock) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from monitoring. outputs.remove(sock) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out this socket, it's no longer ready for writing #remove this socket from writefd's. outputs.remove(sock) lind_test_server.close_syscall(serversockfd)
def client1(): """ <Purpose> Thread for client 1 to connect to server, and send/recv data... """ lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(clientsockfd, "test", 0) #Short sleeps are not working, give enough time... emultimer.sleep(1) assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \ "Write failed in select while processing client 1..." lind_test_server.close_syscall(clientsockfd)
def tester(): # need simultaneous communication... emultimer.createthread(helper) emultimer.sleep(0.1) lind_test_server.send_syscall(sockets[1], "SocketPair test.", 0) msg = None msg = lind_test_server.recv_syscall(sockets[1], 1024, 0) assert msg != None, "Sockpair recv failed in tester..." emultimer.sleep(0.1) lind_test_server.close_syscall(sockets[1]) lind_test_server.close_syscall(sockets[0]) return
def tester(): # need simultaneous communication... emultimer.createthread(helper) emultimer.sleep(0.1) lind_test_server.send_syscall(sockets[1], "SocketPair test.", 0) msg = None msg = lind_test_server.recv_syscall(sockets[1], 1024, 0) assert msg != None, "Sockpair recv failed in tester..." emultimer.sleep(0.1) lind_test_server.close_syscall(sockets[1]) lind_test_server.close_syscall(sockets[0]) return
""" rip, rport, newsockfd = lind_test_server.accept_syscall(serversockfd) #Process first test... try: msg = lind_test_server.recvfrom_syscall(newsockfd, 100, MSG_PEEK) assert len(msg[2]) == 100, "Length expected 100 bytes to peek, but only" \ + " got " + str(len(msg[2])) + " bytes." msg = lind_test_server.recvfrom_syscall(newsockfd, 100, 0) assert len(msg[2]) == 100, "Length expected 100 bytes to read, but only" \ + " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'TEST:- W:100, P:100, R:100 : ', e emultimer.sleep(0.2) #Process second test... try: msg = lind_test_server.recvfrom_syscall(newsockfd, 20, 0) assert len(msg[2]) == 20, "Length expected 20 bytes to read, but only" \ + " got " + str(len(msg[2])) + " bytes." msg = lind_test_server.recvfrom_syscall(newsockfd, 20, MSG_PEEK) assert len(msg[2]) == 20, "Length expected 20 bytes to peek, but only" \ + " got " + str(len(msg[2])) + " bytes." msg = lind_test_server.recvfrom_syscall(newsockfd, 80, 0) assert len(msg[2]) == 80, "Length expected 80 bytes to read, but only" \ + " got " + str(len(msg[2])) + " bytes." except Exception, e: print 'Test:- W:100, R:20, P:20, R:80 : ', e
"Failed to read from a file..." #test for file finished, remove from polling. to_go = [x for x in polled if x['fd'] == sock] map(polled.remove, to_go) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out of this socket, it's no longer ready for writing, #modify it only read mode. [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLIN lind_test_server.close_syscall(serversockfd) #Thread for running server ... emultimer.createthread(process_request) emultimer.sleep(1) def client1(): """ <Purpose> Thread for client 1 to connect to server, and send/recv data... """ lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(clientsockfd, "test", 0) #Short sleeps are not working, give enough time... emultimer.sleep(1) assert lind_test_server.recv_syscall(clientsockfd, 100, 0) == "test", \ "Write failed in select while processing client 1..." lind_test_server.close_syscall(clientsockfd)
#Both client and server are run from this file, hence opening sockets for both listensockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) #Bind the socket to an address, this is important in repy, because the recvfrom #_syscall() doesn't work properly without binding the address first. lind_test_server.bind_syscall(listensockfd, '127.0.0.1', 50300) def process_request(): msg = None # Read the data in the socket. try: msg = lind_test_server.recvfrom_syscall(listensockfd, 1024, 0) assert msg != None, "Socket failed to recv message." except Exception, e: print "UDP Connect Test : ", e #Run the listen socket in seperate thread, since send/listen should be started #simultaneously. emultimer.createthread(process_request) emultimer.sleep(0.1) #This is another way to send message in UDP, instead of sendto_syscall(). lind_test_server.connect_syscall(sendsockfd, '127.0.0.1', 50300) lind_test_server.send_syscall(sendsockfd, "UDP Connect Test", 0) #close send & listen sockets... lind_test_server.close_syscall(listensockfd) lind_test_server.close_syscall(sendsockfd)
# bind to a socket lind_test_server.bind_syscall(serversockfd, '127.0.0.1', 50431) lind_test_server.listen_syscall(serversockfd, 10) def do_server(): newsocketfd = lind_test_server.accept_syscall(serversockfd) fd = newsocketfd[2] lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0) lind_test_server.setshutdown_syscall(fd, SHUT_RDWR) try: # should fail!!! lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0) except: pass else: print "send after shutdown didn't fail!!!" createthread(do_server) # wait for the server to run... sleep(.1) # should be okay... lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50431) assert(lind_test_server.getpeername_syscall(clientsockfd) == \ ('127.0.0.1', 50431))
listensockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) sendsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) def recvmessages(): # let's wait for packets here... lind_test_server.bind_syscall(listensockfd,'127.0.0.1',50102) assert lind_test_server.recv_syscall(listensockfd,10000,0) == 'test',\ "UDP recv test 1 failed." lind_test_server.recv_syscall(listensockfd,10000,0) == 'test2',\ "UDP recv test 2 failed." # I need a thread... createthread(recvmessages) # let's wait for it to start... sleep(.1) # send a message lind_test_server.sendto_syscall(sendsockfd,'test','127.0.0.1',50102,0) # get another socket, bind and then send sendsockfd2 = lind_test_server.socket_syscall(AF_INET, SOCK_DGRAM, 0) lind_test_server.bind_syscall(sendsockfd2,'127.0.0.1',50992) lind_test_server.sendto_syscall(sendsockfd2,'test2','127.0.0.1',50102,0) sleep(0.1) lind_test_server.close_syscall(sendsockfd2) lind_test_server.close_syscall(sendsockfd) lind_test_server.close_syscall(listensockfd)
lind_test_server.listen_syscall(serversockfd, 10) def do_server(): newsocketfd = lind_test_server.accept_syscall(serversockfd) fd = newsocketfd[2] lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0) lind_test_server.setshutdown_syscall(fd, SHUT_RDWR) try: # should fail!!! lind_test_server.send_syscall(fd, 'jhasdfhjsa', 0) except: pass else: print "send after shutdown didn't fail!!!" createthread(do_server) # wait for the server to run... sleep(.1) # should be okay... lind_test_server.connect_syscall(clientsockfd, '127.0.0.1', 50431) assert(lind_test_server.getpeername_syscall(clientsockfd) == \ ('127.0.0.1', 50431))