Socket connection to my ip rather than 127.0.0.1 - java

Well, i am at new at Socket programming in java. I tried to implement a simple socket program to send the message with 127.0.0.1:4242 as localhost. But i want to send message to specific IP. How can i achieve that? Will it be possible to send message to my own IP as client-server running simultaneously?

An endpoint in socket communications is an endpoint. Anything you can do using 127.0.0.1 can be done using that machines ip address. See here for more details.

Every IP datagram has a source address and a destination address in the IP header, plus a transport protocol number, which is for majority of Internet traffic is either TCP or UDP. Then the header for that transport protocol lists source and destination port numbers.
So here you have it - sending, or better said "client", application gets assigned source address and port, usually automatically - address determined by the local routing table, port number assigned out of range of ephemeral ports, while "server" application listens on a well known port bound to an address at a particular machine. This tuple (source IP, source port, destination IP, destination port) is enough for the datagram to get from here to there.
127.0.0.1, and actually all the addresses in the range 127/8, are reserved for the loopback, a virtual local interface, i.e. it's the way to say "no matter what my real address is, or even if I don't have one, connect to this machine I'm at right now".
Read up on the TCP/IP suite of protocols - it's a fairly simple concept (with ton of interesting details, of cource).

Related

In a tcp connection, how possibly can a server handle more than 65535 client at an instant?

I've been reading this socket tutorial by Oracle and stumbled upon the following text:
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
Now if I'm not wrong then the port size is 16 bit which limits the max no of ports around 65K. This means that a server can't handle more than 65535 connections at any instant if all of it's port are bound to some client local port. While some answers like this on stackoverflow suggest that there's no limit on active connections. What is true about this and what is wrong?
Edit 1: If indeed a server can't handle more than 2^16-1 connections, then how do websites like Google handle this limitation?
A unique TCP connection is defined by a unique combination of client IP, client port, server IP and server port. For a specific service server IP and port are constant (i.e. port 80 for HTTP), but client IP and port can vary. Since the port range is only 1..65535 this means that the server can only handle at most 65535 different connections from the same client IP address at the same time, because these are all possible unique combinations of the connection tuple when only the port can be changed. But, if there are multiple clients with different IP addresses this limitations applies to each of these clients separately. If you then look at the amount of different possible IP addresses (IPv4 and IPv6) you'll see that there is essentially no real limit of how much connections the server could handle in theory.
In practice each of these TCP connections takes memory at the server since the current state has to be kept. Additional memory is needed in kernel and application for file descriptor and application protocol state etc. This means that there is a practical limit based on the resources of the machine which might be less then 64k but also way more, depending on the system and its configuration.
They use something like NAT (network address translation) for your ISP.
You can access different computer behind your router because your router maps the routes to the PCs internally.
E.g. Google data center does the same thing. Mapping "Google.com" to different internal server allowing them to accept more than 65k connections in total.

DatagramSocket.Receive without port set

I've recently started to learn about socket usage, more specifically in Java.
In this link is shown a simple client-server UDP application.
My question is: How does the client receives the response with receive() method when this datagramsocket object calling receive doesn't have a port set?
The client initializes its DatagramSocket via the nullary constructor, which binds the socket to some available port (chosen in an unspecified manner) on the wildcard address. That's quite different from not having a port set -- there is a port set, but it is chosen by the computer, not explicitly specified by the program.
When the server receives a message, it extracts not only the message data, but also the source address and port. It sends its response to that address and port. The client successfully receives it via the same socket with which it sent the original message, because it's still bound to the same port, even if you don't know exactly which one that is.
the first time you send a packet using that socket, an ephemeral port will be allocated. If you need a specific port then you can bind it explicitly which is needed for bootp for instance. But in the most simple case bind is not needed and you get the ephemeral port.
The peer will see this port in the UDP header because in there is both the source and destination port.
As a side note this mechanism is the same for TCP clients. When they do a call to connect() unless the socket was bind to a specific port, an ephemeral port will be allocated by the clients kernel and that one will be used for the lifetime of the connection.
The ephemeral ports are usually in a specific range, there is kindof a pool of ports for UDP and TCP. The kernel usually has a mechanism to take from the pool starting from the beginning and gradually incrementing till the end is reached upon which he'll start from the beginning. Skipping ports that are still in use of course. It is called the ephemeral port range, and it is specific to the os.
how to change/view ephemeral port range in windows machines

what to use for identifying between clients that connect to a server? Java, Android, Sockets

I am working with a Java desktop server and multiple Android clients connected to it. on the server side I need to identify which client has sent me a message by sockets TCP/IP and send a response only to that one client and not the others.
I will store all the sockets of clients in an ArrayList.
first here are two ways that I tried that don't work;
-- the IP address of the client, get this by calling socket.getLocalSocketAddress() in the client and socket.getRemoteSocketAddress() in the server, but they never match. for example i got in the client XXX.XXX.11.17 and in the server XXX.XXX.0.13, they are supposed to be the same for the same connection.
-- the port number, get this by calling getLocalPort() in the client and getPort() in the server, yes this works perfectly and the numbers match so I can use this, HOWEVER there is a possibility that the randomly selected port numbers on two different clients could be the same. not likely but possible. so that means there is no guarentee that they are unique.
what is the alternative that I can use that will work?
I need to identify which client has sent me a message by sockets TCP/IP and send a response only to that one client and not the others.
Send it back down the same socket you received the request from.
If you need a permanent identified for the client, you can use the result of Socket.getRemoteAddress().
getLocalSocketAddress() in the client and getRemoteSocketAddress() in the server [...] are supposed to be the same for the same connection.
No, because you don't know what's in between. Most mobile providers use proxies, NAT and so on. The mobile device thinks it's on a LAN (10.0.0.x or 192.168.x.x addresses) which the provider provides. It's even possible for multiple clients to have the same remote address (as seen from your server).
That being said, you can uniquely identify a client in your server application by the remote IP address and port combined together, given the server listens on one IP, port and protocol. This information is available from socket.getRemoteSocketAddress(), where the returned InetSocketAddress (in case of an internet socket) contains both the remote IP and port (getAddress() and getPort() respectively).
But as indicated by the other answer, you don't really need a way to identify a client. A network client is identified by the socket you receive data on (a socket is an exclusive connection between two nodes), so just send the data back to the socket that you received the request on.
If you do need more bookkeeping data about the connected client, wrap the client socket in a wrapper class that contains additional information.

Is it possible to use a dedicated IP as source IP?

The title may sound a bit odd but I wanted to keep the title short.
So I have here a Socket which connects to some server for testing purpose.
Now when I get the IP from the client socket (server side) it will give me one IP (external one).
But I have another IPv4 dedicated IP, can I use that as source?
If you want a Socket to originate on a particular IP/interface, you should be able to bind() it to the appropriate address before issuing the connect().

Is there a way for a java chat client program to dynamically find the host server?

I want to just execute an instance of client with no parameters other than the port number and have the program find the server listening on that port anywhere on the network.
You can use UDP multi-cast to find the server. You can send a UDP packet to the port and have the server respond with its IP address. (This effectively hard codes a multi-cast address) which is not much better than hardcoding a hostname which can be looked up via DNS.

Categories