3

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.

3
  • You want to have same sockets on the both sides? why? your handlers are host and port. on every connection(in or out) you have new socket descriptor.
    – gogagubi
    Commented Sep 20, 2015 at 18:41
  • It's not clear what you mean when you say "socket object". Do you mean "an object that represents a socket", or an object that represents something else, that you want to transmit to another program using a socket as the transport mechanism? Commented Sep 21, 2015 at 1:49
  • The object which represent socket
    – Richa
    Commented Sep 21, 2015 at 19:20

1 Answer 1

2

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

2
  • Hi, I want to know how to pass socket object between two clients so that both clients use same socket instance to connect to the server. Is this possible in Java or any other language to pass socket object to the other cleint and using that information the other cleint is able to connect with server.
    – Richa
    Commented Sep 21, 2015 at 19:18
  • 1
    An object belongs to a program. At best you can reuse the port (if SO_REUSEADDR is enabled) but it is impossible to "share" the socket. I can hold the ball in your hand. but can you give me your hand? it jolts down to this. two different processes can't share the same socket. but they might share the same information
    – Ronnie
    Commented Sep 21, 2015 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.