Socket Interview questions
Should I use ServerSocket or DatagramSocket in my applications?
DatagramSocket allows a server to accept UDP packets, whereas ServerSocket allows an application to accept TCP connections. It depends on the protocol you’re trying to implement. If you’re creating a new protocol, and the choice is up to you, here’s a few tips :
DatagramSockets communciate using UDP packets. These packets don’t guarantee delivery - you’ll need to handle missing packets in your client/server
ServerSockets communicate using TCP connections. TCP guarantees delivery, so all you need to do is have your applications read and write using a socket’s InputStream and OutputStream.
How do I get the IP address of a machine from its hostname?
The InetAddress class is able to resolve IP addresses for you. Obtain an instance of InetAddress for the machine, and call the getHostAddress() method, which returns a string in the xxx.xxx.xxx.xxx address form.
InetAddress inet =
InetAddress.getByName(”www.davidreilly.com”);
System.out.println (”IP : ” + inet.getHostAddress());
How do I perform a hostname lookup for an IP address?
The InetAddress class contains a method that can return the domain name of an IP address. You need to obtain an InetAddress class, and then call its getHostName() method.
This will return the hostname for that IP address. Depending on the platform, a partial or a fully qualified hostname may be returned.
InetAddress inet = InetAddress.getByName(”209.204.220.121?);
System.out.println (”Host: ” + inet.getHostName());
How can I find out who is accessing my server?
If you’re using a DatagramSocket, every packet that you receive will contain the address and port from which it was sent.
DatagramPacket packet = null;
// Receive next packet
myDatagramSocket.receive ( packet );
// Print address + port
System.out.println (”Packet from : ” +
packet.getAddress().getHostAddress() + ‘:’ +
packet.getPort());
If you’re using a ServerSocket, then every socket connection you accept will contain similar information. The Socket class has a getInetAddress() and getPort() method which will allow you to find the same information.
Socket mySock = myServerSocket.accept();
// Print address + port
System.out.println (”Connection from : ” +
mySock.getInetAddress().getHostAddress() + ‘:’ +
mySock.getPort());
How can I find out the current IP address for my machine?
The InetAddress has a static method called getLocalHost() which will return the current address of the local machine. You can then use the getHostAddress() method to get the IP address.
InetAddress local = InetAddress.getLocalHost();
// Print address
System.out.println (”Local IP : ” + local.getHostAddress());
Why can’t my applet connect via sockets, or bind to a local port?
Applets are subject to heavy security constraints when executing under the control of a browser. Applets are unable to access the local file-system, to bind to local ports, or to
connect to a computer via sockets other than the computer from which the applet is loaded. While it may seem to be an annoyance for developers, there are many good reasons why such tight constraints are placed on applets. Applets could bind to well known ports, and service network clients without authorization or consent. Applets executing within firewalls could obtain privileged information, and then send it across the network. Applets could even be infected by viruses, such as the Java strange brew strain. Applets might become infected without an applet author’s knowledge and then send information back that might leave hosts vulnerable to attack.
Signed applets may be allowed greater freedom by browsers than unsigned applets, which could be an option. In cases where an applet must be capable of network communication, HTTP can be used as a communication mechanism. An applet could communicate via java.net.URLConnection with a CGI script, or a Java servlet. This has an added advantage - applets that use the URLConnection will be able to communicate through a firewall.
What are socket options, and why should I use them?
Socket options give developers greater control over how sockets behave. Most socket behavior is controlled by the operating system, not Java itself, but as of JDK1.1, you can control several socket options, including SO_TIMEOUT, SO_LINGER, TCP_NODELAY, SO_RCVBUF and SO_SNDBUF.
These are advanced options, and many programmers may want to ignore them. That’s OK, but be aware of their existence for the future. You might like to specify a timeout for read operations, to control the amount of time a connection will linger for before a reset is sent, whether Nagle’s algorithm is enabled/disabled, or the send and receive buffers for datagram sockets.
When my client connects to my server, why does no data come out?
This is a common problem, made more difficult by the fact that the fault may lie in either the client, or the server, or both.The first step is to try and isolate the cause of the problem, by checking whether the server is responding correctly.
If you’re writing a TCP service, then you can telnet to the port the server uses, and check to see if it is responding to data. If so, then the fault is more than likely in the client, and if not, you’ve found your problem. A debugger can be very helpful in tracking down the precise location of server errors. You could try jdb, which comes with JDK, or use an IDE’s debugger like Visual J++ or Borland JBuilder.
If your fault looks like it is in the client, then it can often be caused by buffered I/O. If you’re using a buffered stream, or a writer (such as PrintWriter), you may need to manually flush the
data. Otherwise, it will be queued up but not sent, causing both client and server to stall. The problem can even be intermittent, as the buffer will flush sometimes (when it becomes full) but not other times.
What is the cause of a NoRouteToHost exception?
Usually this means that there isn’t an active Internet connection through which a socket connection may take place, or that there is a nasty little firewall in the way. Firewalls are the bane of users and developers alike - while useful for security, they make legitimate networking software harder to support.
Your best option is to try using a SOCKS proxy, or to use a different protocol, like HTTP. If you still have firewall problems, you can manually specify a HTTP proxy server (see section 2.4)
This is a common problem, made more difficult by the fact that the fault may lie in either the client, or the server, or both.The first step is to try and isolate the cause of the problem, by checking whether the server is responding correctly.











