connection refused exception in socket programming - java

In simple socket programming in java , what ip should be given while making new socket and its on wan
//Server side
ServerSocket ss = new ServerSocket(8888);
System.out.println("\n\n\tWaiting for connection\n");
Socket c = ss.accept();
System.out.println("\n\n\tConnection established\n");
//Client side
Socket c=new Socket("192.16*****",8888);
System.out.println("\n\n\tSuccessfully connected to the server");
//in **** there is complete ip address of my computer .... i.e. IPV4 address (checked
//from ipconfig command on cmd)

By default, a new ServerSocket should bind to all network interfaces.
You should be able to find out what interfaces are being used by running (I assume you're running Windows, since you mentioned ipconfig):
netstat -an |find /i "8888"
If in fact the application is creating a socket and binding to all interfaces, you should see an entry like the following:
TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING
Otherwise, you should be able to get the interface that is being used (it's the first IP address from the left).

Related

Difference between Socket & InetSocketAddress?

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).

Using same address and port for accepting and connecting in Java

(This might have been asked a thousand times, but I do not get it straight.)
Suppose I have the following snippet:
InetAddress localAddress = InetAddress.getByName("192.168.1.10");
int localPort = 65000;
InetAddress targetAddress = InetAddress.getByName("192.168.1.20");
int targetPort = 65000;
// Create a new serversocket
ServerSocket ss = new ServerSocket(localPort, 50, localAddress);
// Wait for an incoming connection...
Socket acceptedSocket = ss.accept();
// Do something with the accepted socket. Possibly in a new thread.
Set up new connection...
Socket socket = new Socket(targetAddress, targetPort, localAddress, localPort);
// Write something to the socket.
Now can I use the same address and port for both accepting an incoming connection and connecting to an address? If it can, then how? If not, then why not? According to this post, ports can be shared, so it shouldn't be a problem.
How does it work?
You can only establish a connection by having the connecting socket use the same address and port. (Ignoring the use of multi-homed servers)
A single connection is a unique combination of both the source address+port and destination address+port, so you can have the same destination if you have a different source.
In other words, can you write server program that that contains client connecting to itself? The answer is yes, surely. All integration tests do this running in-process server and connecting to it.

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

How to repair BindException: Cannot assign requested address?

I need to change the IP address of a host from a client.
I use UDP commands and a MulticastSocket to obtain the IP address of this host (currentIp) and use this IP address to successfully establish a TCP connection.
The command to change this host IP address requires a DatagramSocket since I need to first get the host device MAC address to include in the change IP address command. Once the TCP connection is made I close the MulticastSocket UDP socket so I can open the DatagramSocket but get the following error:
java.net.BindException: Cannot assign requested address: Cannot bind
Is there something I need to do besides close the MulticastSocket socket before trying to get a DatagramSocket socket with the same port number, or is am I missing something else?
DatagramSocket socket;
private boolean ChangeIpAddress(String newIp) {
DatagramSocket socket;
try {
socket = new DatagramSocket(30718, InetAddress.getByName(currentIp));
} catch (SocketException ex) {
...
It appears you are using a hostname with an incorrect IP address. You need to find you etc/hosts or where ever its defines.
In my case, I have changed the listening address to 127.0.0.1 (localhost) and the problem is fixed

Categories