I would like to create a connection between a server and a client via socket on 2 different computers (but the question is relevant also to a connection on the same computer). I know the IP of the server, but I would like to connect without knowing the port number of the server - is there a way to do so?
for example:
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((127.0.0.1, " "))
s.listen(1)
conn, addr = s.accept()
Is there a way to do so?
Can it also be done without knowing IP?
You can't open a socket without knowing the port number. However, you can use the predefined port numbers in the protocol. For example, if port is not specified, HTTP uses port 80 and HTTPS uses port 443 by default.
Related
I have set up a client and a server using sockets in python where my client sends data to the server, servers performs an operation, then returns some data to the client. Originally both the client and server were to have the same port number (9999). My issue currently is that I have to change the port of the server to 19999, and when I try to run, it does not work. The client is able to send data if its port is also changed to 19999, but it does not work if client is 9999 and server is 19999, which is what I need. New to networking systems so would appreciate any useful links to information or advice.
pic of client (left) and server (right)
Normally, clients will use an ephemeral port for its local port, and connect to the server port. Your client code is attempting to connect to port 9999, which is not the server port, which explains why it is not working. You need to connect to port 19999, since that is the port the server is listening on.
Using an unbound socket causes the client to choose an ephemeral port for its local address when making a connection. If you want the client to bind to a specific port, use bind before you call connect.
# bind locally to 9999
local_addr = (host, 9999)
s.bind(local_addr)
# connect to 19999
remote_addr = (host, 19999)
s.connect(remote_addr)
This question already has answers here:
Does the port change when a server accepts a TCP connection?
(3 answers)
Closed 4 years ago.
Hi I'm just a newbie in networking,
just want to ask, is welcoming port of welcoming socket on a server the same as listening port?
For example, we all know HTTP use port 80, so is port 80 the welcoming port of the web server to initialize TCP's three way handshake? and actual port number of connection socket (for transmission of http message) can be arbitrary number assigned by the server?
From the accept manpage:
The accept() system call is used with connection-based socket types
(SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection
request on the queue of pending connections for the listening socket,
sockfd, creates a new connected socket, and returns a new file
descriptor referring to that socket. The newly created socket is not
in the listening state. The original socket sockfd is unaffected by
this call.
"welcome" port is the listening port. All client initiate connections to webserver "listening" on port 80 ( clients are "welcome" on port 80). The connection in ESTABLISHED state will have a different socket fd than listen fd.
is welcoming port of welcoming socket on a server the same as listening port?
The port on the server stays the same, i.e. 80 for all the clients even after the three-way handshake.
I guess what you really ask is how sever distinguish simultaneous client connections.
Usually network sockets use unique 4-tuples to identify the connection, i.e. source IP, source port, destination IP, destination port: https://en.wikipedia.org/wiki/Network_socket#Socket_pairs
So the destination IP and port stays the same for all the clients (i.e. server's IP and port 80) but the source IP and ports are different. That is how server distinguish different connections to the same port 80.
actual port number of connection socket (for transmission of http message) can be arbitrary number assigned by the server?
The destination port stays the same, i.e. 80 as described above. Instead each client selects its unused source port before establish the TCP connection.
I am setting up a simple server using sys.net.Socket (cpp, linux).
The server is bound like this:
hostSocket.bind(new Host("0.0.0.0"), 20301);
And I connect to the server like this:
clientSocket.connect(new Host("localhost"), 20301);
If the ports do not match, the connection won't work, so that works as expected.
However, when I "accept" the connection on the server side, and want to print information about the client, I get a random port whenever a new connection is incoming, just never 20301:
var connectedClient : Socket = hostSocket.accept();
trace("Incoming connection from " + connectedClient.peer().host.toString()
+ " on port " + connectedClient.peer().port);
Now I get results like this:
Incoming connection from 127.0.0.1 on port 50977
Incoming connection from 127.0.0.1 on port 50978
Incoming connection from 127.0.0.1 on port 50979
What is going on here? Why is the displayed port not 20301?
Both server and client sockets need to bind to some local address (ip,port) for a connection to happen.
The client simply binds to a local free port, and will very likely change a lot, depending on all other connections happening on your machine.
Finally, Haxe sockets are (sometimes indirectly) wrappers over POSIX sockets; the spec for connect() says:
If the socket has not already been bound to a local address, connect() shall bind it to an address which, ..., is an unused local address.
I have a weird query. I have learned that a socket is a combination of IP and Port. So what is socket descriptor? Is it just an integer? What does it do?
Can I have to different socket descriptors on the same port? If yes, then can those be of different types (TCP/UDP)?
I know these are silly questions; I have been blindly using SD for quite a time now :P
TCP and UDP are independent, so you can have TCP and UDP sockets on the same port.
A socket descriptor is to a socket as a file descriptor is to a file.
A TCP connection is actually defined by the tuple: local IP, local port, remote IP, remote port. You can have multiple connections with the same local IP and port, as long as they have different remote IP and/or port.
For instance, a web server uses its local port 80 for all the connections. But each client connection will either come from a different machine (and hence a different remote IP) or different sockets on the same machine (so they'll have the same remote IP but different remote ports).
A socket descriptor is a unique integer returned by the system when you ask it to create a socket with the socket call. Each socket is identifiable by its socket descriptor.
As regards the second part of your question, You will get a different socket descriptor for the same IP+PORT+PROTOCOL, so yes, you can have tcp and udp sockets on the same port, but you will get two different socket descriptors
You should read network programming tutorials like these first: Beej's Network Programming Tutorial
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.
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.
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()