I want to design a P2P network. This is a simplified program that explains my problem.
I want to ask is there a way to connect and accept connection simultaneously.
If not, do P2P networks use two ports, one to accept and other to connect.
I am using thread because I have to run sample of this program in all machines. That's why I'm taking input of host and port. On main thread it accepts connection and other connect.
The program below give me the following error:
socket.error: [Errno 106] Transport endpoint is already connected
import socket
from threading import Thread
s = socket.socket()
s.bind(('localhost', 6000))
def rec():
s.listen(1)
c, addr = s.accept()
print 'Connection received from ' , addr
def test():
host = raw_input("Enter Host address : ")
port = input("Enter port : ")
s.connect((host, port))
print s.getsockname()[0]
def main():
t = Thread(target=rec, args=())
t.start()
test()
if __name__ == '__main__':
main()