How to pass socket objects between two clients in any language C++, Python, Java, C
Question: I have an idea like how basic communication between _client_ and _server_ is
established. So serialize data streams can be passed between _client_ and
_server_. But I want to know, how **_socket objects_** can be passed between
two _clients_ : I want to know is it possible to **_pass socket objects
between two clients_** and both share the same socket instance. Please
suggest.

    
    
    Client class:
    
    import socket
    import sys
    
    # create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    #connect the socket to the port where server is listening
    server_address = ('localhost',2000)
    print >>sys.stderr, 'connecting to %s port %s' % server_address
    sock.connect(server_address)
    
    #after connection is established, data can be through socket with sendall() and recv()
    
    try:
        #send data
    
        message = 'This is Message. It will be repeated'
        print >>sys.stderr, 'sending "%s"' % message
        sock.sendall(message)
    
        #llok for the response
        amount_received = 0
        amount_expected = len(message)
    
        while amount_received < amount_expected:
            data = sock.recv(16)
            amount_received += len(data)
            print amount_received
            print >>sys.stderr, 'received "%s"' % data
    
    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()
    

_Server_ class created to receive message from the _client_ and revert with
some message.

    
    
    Server class:
    
    import socket
    import sys
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ("localhost",2000)
    print >>sys.stderr, 'starting up on %s port %s' %server_address
    sock.bind(server_address)
    
    sock.listen(1)
    
    while True:
        print >>sys.stderr, 'waiting for connection'
        connection, client_address = sock.accept()
    
        try:
            print >>sys.stderr, 'connection from', cleint_address
    
            while True:
                data = connection.recv(16)
                print >>sys.stderr, 'received "%s"' % data
                if data:
                    print >>sys.stderr, 'sending data back to the client'
                    connection.sendall(data)
                else:
                    print >>sys.stderr, 'no more data from', client_address
                    break
        finally:
            connection.close()
    

After _server_ started the _client_ connects with _server_ and displays
suitable messages. Now instead of sending messages between _client_ and
_server_ , I want to send **_socket object_** to another _client_ which can be
achieved using either `TCP` or `UDP`. In `TCP`, serialization of data is
required. I want to know is there any way to wrap **socket object** and pass
it over.


Answer: Socket objects can not be transported (or you know, teleported :D ) to another
language or anything. At most, you can create a protocol by which an instance
of the socket can be transferred to another language. But again, I don't see
how it may help you.

You have a server socket listening on 2000 port. And another Java socket may
connect to it using a client socket. So, what's the point of sending one of
the socket to the another? the communication link is somehow twirled. Like, we
can just eat ourself to regenerate us. But that would be impossible

Similarly, at most, you can send an instance of the server socket to the java
socket. But on the same computer, the Java won't be able to recreate it,
because the port is already being listened by another program.

Next, if two programs could listen on the same port, that would make stealing
of data and forging quite easy. So, it is not possible for two programs to
listen on the same port.

I think what you are looking for is that, two programs combinedly handle the
I/O of the same socket. That is rational, at least.

For that, you should create some sort of bidirectional communication link
between these two processes. Like another socket on a different port.

Like S is the Server (the sole owner of the socket S1) meanwhile A and B are
the handlers. S should be listening on two different ports. where only A and B
are connected. Then any data that comes to S, at S' discretion would be A or B
appropriate, then, A or B will reply to that request. And then S will respond
appropriate.

Another approach would be S is the `main` server socket. and A and B are
servers listening on different ports. Whenever data comes to S, S sends it to
A or B depending on content.

Thirdly, and the most messy solution would be that, A is the server and it
offloads some tasks to B via some sort of communication (server-client or
threads or a subprocess) and they handle data appropriately. The reason of
calling it messy is that one has to handle two tasks and its harder to
maintain its functionality.

But still, sharing a socket is like using the same page of a copy for two
different tasks. Hope it helped

