john-charles
john-charles

Reputation: 1469

tcp socket server get name of server client used

Let's say I have a server, which has multiple domain names which resolve to its IP address. For example my server is 10.0.0.33 and can be accessed by serverA.mysite.com, serverB.mysite.com, and serverC.mysite.com. If I have a process running with code similar to the following:

#!/usr/bin/env python

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.listen(5)

while True:
   client, client_addr = server.accept()
   #server_name = <some function>(client)
   #Do Something with the client knowing the value of servername...


server.close()

Is there a way to determine if the tcp connection made by the client was aimed at serverA.mysite.com or serverB.mysite.com...?

My Example is in python but I don't need a python specific answer.

Upvotes: 1

Views: 900

Answers (2)

gabhijit
gabhijit

Reputation: 3605

I believe - you are listening on INADDR_ANY ("0.0.0.0") and would like to know exactly which one the client connected to, if you were listening on multiple ports? That's fairly simple you should use s.getsockname after the accept. So your code would look something like this

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 10000))
s.listen(5)
while True:
    s2 = s.accept()
    print s2.getsockname()
    s2.close()

Upvotes: 0

Neil
Neil

Reputation: 55422

No, TCP/IP connections work at the IP address level, so you cannot determine how the client obtained the IP address on which your server was listening.

HTTP works around this by requiring (since 1.0) that the client send the original host name as part of the request data.

Upvotes: 2

Related Questions