def server_initialize(): x,y=stdscr.getmaxyx() stdscr.addstr(0,13,"PyChat") stdscr.addstr(1,0,"A P2P chat application based on sockets") stdscr.addstr(2,0,"Connected to HOST: "+str(HOST)) stdscr.addstr(3,0,"Listening on PORT: "+str(PORT)) a=curses_initialize() server_name = a.my_raw_input(stdscr,4,0,"Enter your nickname: ") stdscr.addstr(5,0,"Enjoy chatting on PyChat " + server_name + " !") stdscr.addstr(6,0,"Waiting for a connection...") return server_name
def main(): x,y=stdscr.getmaxyx() server_name=server_initialize() stdscr.refresh() # SOCKET OBJECT INITIALIZATION socket_object = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_object.bind((HOST, PORT)) socket_object.listen(1) # WAITING FOR CONNECTION ... (conn1,addr1) = socket_object.accept() (conn2,addr2) = socket_object.accept() # CONNECTION ESTABLISHED ! #INITIALIZING SEND AND RECEIVE connect = SetConnection(conn1,conn2) # INITIALIZING SERVER AND CLIENT NAMES conn2.send(server_name) client_name = conn1.recv(DATA_BUFFER(1024)) stdscr.addstr(7,0,"Connection Established !You are now connnected to " + client_name) stdscr.refresh() receive = server_receive(connect['recv'],client_name) stdscr.refresh() stdscr.clear() stdscr.addstr(x/2,y/2-20," PRESS ANY KEY TO START CHATTING ") z=stdscr.getch() stdscr.clear() # RECEIVE THREAD STARTS HERE receive.start() a=curses_initialize() # SEND STARTS HERE while 1: x,y=stdscr.getmaxyx() stdscr.addstr(x-5,0,"Press ctrl + c to quit chat safely") stdscr.addstr(x-4,0,"_"*y) send_data=a.my_raw_input(stdscr,x-2,0," >>> ") while send_data=='': send_data=a.my_raw_input(stdscr,x-2,0," >>> ") message_send(connect['send'],server_name,send_data) stdscr.addstr(x-1,0," "*(y-1)) stdscr.addstr(x-2,0," "*(y-1))
def main(): x,y=stdscr.getmaxyx() client_name=client_initialize() stdscr.refresh() # SOCKET OBJECT INITIALIZATION socket_object1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM) socket_object1.connect((HOST,PORT)) # SELECTING SEND AND RECEIVE SOCKETS socket_object1.send("WILL SEND") # telling server we will send data from here socket_object2 = socket.socket(socket.AF_INET,socket.SOCK_STREAM) socket_object2.connect((HOST,PORT)) socket_object2.send("WILL RECV") # telling server we will recieve data from here # CONNECTION ESTABLISHED # INITIALIZING SERVER AND CLIENT NAMES socket_object1.send(client_name) server_name = socket_object2.recv(DATA_BUFFER(1024)) stdscr.addstr(7,0,"Connection Established !You are now connnected to " + server_name) receive = client_receive(socket_object2,server_name) stdscr.refresh() stdscr.clear() stdscr.addstr(x/2,y/2-20," PRESS ANY KEY TO START CHATTING ") z=stdscr.getch() stdscr.clear() # RECEIVE THREAD STARTS HERE receive.start() a=curses_initialize() # SEND STARTS HERE while 1: x,y=stdscr.getmaxyx() stdscr.addstr(x-5,0,"Press ctrl + c to quit chat safely") stdscr.addstr(x-4,0,"_"*y) send_data=a.my_raw_input(stdscr,x-2,0," >>> ") while send_data=='': send_data=a.my_raw_input(stdscr,x-2,0," >>> ") message_send(socket_object1,client_name,send_data) stdscr.addstr(x-1,0," "*(y-1)) stdscr.addstr(x-2,0," "*(y-1))