Difference between Socket & InetSocketAddress? - java

I searched a lot but I was not able to find a direct difference between the two. When do we use each one when it comes to creating a client socket?

An InetSocketAddress does not manage a Socket in any way.
I think you mean Socket vs DatagramSocket.
Socket is for connections communicating via TCP (reliable).
DatagramSocket is for connections communicating via UDP (unreliable).
Or, if you're referring to SocketAddress vs InetSocketAddress:
SocketAddress is simply the abstract implementation of a Socket Address with no protocol.
InetSocketAddress is an implmentation of SocketAddress, for IP.

It is all in the name... A typical network socket is a connection between two ports on two machines.
The ServerSocket is the one that waits for clients to connect to it.... it 'binds' to a port, and it 'Listens' for connections, and when they happen, it 'accepts' the connection. The result of the accepted connection is a Java Socket. The client that connected (if it is also Java), also has a Java Socket. You now have two sockets connected to each other.
The Socket is described above.
Now, the address is the details about how to find/identify the remote side of the Socket connection.
A SocketAddress is the abstract class for something that can tell Java where to connect to when contacting a server, and it allows the Sockets to identify remote servers/clients once the connection is made.
An InetSocketAddress is a special SocketAddress designed to represent the standard TCP Protocol address, so it thus has methods to set/query the host name, IP address, and Socket of the remote side of the connection (or, in fact the local side too).
So, the (Inet)Socket address is used to establish Socket connections...
Summary:
ServerSocket is a listener that waits for socket connections to be established.
Socket is the communication channel between two systems (one the server, the other the client).
SocketAddress is an abstract class that identifies an address
InetSocketAddress is a class that is specific for the TCP protocol consisting of IP Addresses/host-names, and port numbers. This is used to establish internet/TCPIP sockets.
Reference taken from
https://softwareengineering.stackexchange.com/questions/231150/whats-the-difference-between-socketaddress-and-serversocket-in-java

From the Javadoc for Socket
This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.
and for InetSocketAddress
This class implements an IP Socket Address (IP address + port number)
The address is like the location of your house, the Socket is the road which leads to that house.

A SocketAddress is the abstract class for something that can tell Java where to connect to when contacting a server, and it allows the Sockets to identify remote servers/clients once the connection is made.
An InetSocketAddress is a special SocketAddress designed to represent the standard TCP Protocol address, so it thus has methods to set/query the host name, IP address, and Socket of the remote side of the connection (or, in fact the local side too).

Related

How to accept connections from local network only in Java

I have a ServerSocket object that is listening with accept method. How do I accept connections only coming from the same network and not outside?
If your system has multiple network interfaces and one of them is receiving local connections only while others should be ignored, you can set up a ServerSocket with the IP-address of the netork interface to bind it only to that particular interface:
ServerSocket ssocket = new ServerSocket(1234, 10, InetAddress.getByAddress("192.168.1.1"));
With IPv4 and IPv6 you obviously need two ServerSockets.
If local and remote connections are received from the same network interface, you have to check the remote peer yourself. This can be done in different ways:
You can set a SecurityManager that implements checkAccept and throws a SecurityException if the connection attempts comes from the wrong place. This is a global setting, so with this you can't set up a ServerSocket at some other part of the application that should accept connections from there.
You can check the IP and port of the Socket being returned from ServerSocket to be local and close the socket if it's not instead of continuing with the request
You can create a subclass of ServerSocket overriding the method checkAccept where call super.accept, do the corresponding check as described in option 2 and only return the retrieved connection if it fit to your criterias or otherwise close it and call the super-method again.
Create the ServerSocket via the constructor that accepts a local IP address, and supply 127.0.0.1 as that IP address.

How to change source-ip(tcp) in Java

Is it possible to change TCP header in Java?
If it's possible, is there any [Change Header] method?
Answering the more narrow question from the title of your question ("How to change source ip in Java"), you can bind your socket to a local IP address and/or port before you connect it to a destination.
The IP address you bind to has to be an IP address that your machine has (otherwise, how could packets arrive back at your machine?). You can also take any unused, non-reserved port number to connect from.
Socket socket = new Socket();
socket.bind(new InetSocketAddress(9999));
// or: socket.bind(new InetSocketAddress(InetAddress.getByAddress(...), 9999));
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 80));
More generally, the answer is no, you cannot just randomly change the TCP header. But there are plenty settings that you can do from Java that will affect what goes into the TCP header.
I'm wondering whether you'r thinking about to cheat the server by providing a random source ip. As far as I know, there's no way to do this in java.
And even if you have changed your ip address, i think you can not successfully "hand-shake" with the server, which means you can not establish a TCP connection with the server side.

What does "Creates a socket address from an IP address" mean?

I was reading the javadoc for InetSocketAddress at http://docs.oracle.com/javase/8/docs/api/ and was wondering what the following phrase means:
Creates a socket address from an IP address and a port number.
What I want to do is create a socket on the local box that listens for incoming connections on the same port and begins communication with that client. I was using "localhost"/80 for the host name on my server, but netstat -l doesn't show anything listening on port 80.
Am I going about this the wrong way?
Then you need to create a ServerSocket.
From memmory: It is something like
ServerSocket mySocket =new ServerSocket(1234); // Listen on port 1234 on all network interfaces.
Socket incommingSocket=mySoccket.accept(); // Wait until a client connect. Once a client connect return the socket for the connection.
And then you can call getInputStream/getOutputStream on the incommingSocket object.

Get domain name of incoming connection

I don't find any information on this topic on the internet and asked here. For example I have server with IP 1.1.1.1 and 2.2.2.2 and two domain names pointing to it one.example.com and example2.net, and socke listening on port 1234 for incoming connections.
For example:
C/C++:
listenfd=socket(AF_INET, SOCK_STREAM, 0);
bind(...);
listen(...);
while(...) accept(...);
or Java:
ServerSocket socket = new ServerSocket(1234);
while(...) {
Socket connectionSocket = welcomeSocket.accept();
...
}
When client accepted on my socket I need to know what domain name/IP is used by the client to connect. It may be one.example.com or example2.net and/or IP 1.1.1.1 or 2.2.2.2 (if connected using IP only).
Apache somehow determined ip/domain of incoming reques, and I need to do such thing in pure socket code. C++ (main) or Java (or any other) accepted, I need to know mechaniics of this.
The IP is stored inside the IP packet header and you can read it from there. In order to get the host, you'll probably have to ask a DNS server by sending a request (or use a function which does it for you). You can find examples for both of the problems, even on this site

Java implementation of NAT Traversal / HolePunching TPC

I've been browsing and searching for implementation of a Peer-to-Peer TCP Connection with both clients behind routers, but everything I got was "get more confused"!
My case is "a little bit simplest", since I already KNOW the local and public IP addresses from both sides.
So, at this point I just DON'T NEED TO USE THIRD SERVER to discover those informations.
Client A: (LocalIP="192.168.0.1", PublicIP="a.a.a.a").
This client will be Listening to connection on port "pppp"
serverSocket = new ServerSocket("pppp");
serverSocket.setSoTimeout(timeOut);
socket = ClientA.serverSocket.accept();*
Client B: (LocalIP="10.10.0.1", PublicIP="b.b.b.b")
This client will will try to connect to "Client A" on port "pppp"
SocketAddress sockaddr = new InetSocketAddress("a.a.a.a", "pppp");
socket.connect(sockaddr, timeOut);
Of couse it will not work, so given those informations, how to proceed?

Categories