What is java socket programming ?
Java is a premier language for network programming. Socket Programming is one of the most important areas of Java programming language, especially for those programmers, who are working in client/server based applications. The java.net package contains classes and interfaces that provide a powerful infrastructure for networking in Java. Java networking API makes it easy to communicate via TCP/IP sockets. Sockets allow the programmer to treat a network connection as just another stream onto which bytes can be written and from which bytes can be read. A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. It can perform seven basic operations: Connect to a remote machine Send data Receive data Close a connection Bind to a port Listen for incoming data Accept connections from remote machines on the bound port The Socket class sits on top of a platform-dependent implementation, hiding the details of any particular system from your Java program.
Java’s Socket class is used by both clients and servers. The Socket class is part of the java.net.* package. Java programs normally use client sockets in the following fashion:
The program creates a new socket object with a constructor. The socket attempts to connect to the remote host. Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other. This connection is full-duplex. Both hosts can send and receive data simultaneously. What the data means depends on the protocol. Different commands are sent to an FTP server than to an HTTP server.
When the transmission of data is complete, one or both sides close the connection. Some protocols, such as HTTP, require the connection to be closed after each request is serviced.
Others, such as FTP, allow multiple requests to be processed in a single connection. The ServerSocket class contains everything needed to write servers in Java. It has constructors that create new ServerSocket objects, methods that listen for connections on a specified port and methods that configure the various server socket options. In Java, the basic life cycle of a server program is: A new ServerSocket is created on a particular port using a ServerSocket( ) constructor. The ServerSocket listens for incoming connection attempts on that port using its accept( ) method. accept( ) blocks until a client attempts to make a connection, at which point accept() returns a Socket object connecting the client and the server. Depending on the type of server, either the Socket’s getInputStream() method, getOutputStream() method, or both are called to get input and output streams that communicate with the client. The server and the client interact according to an agreed-upon protocol until it is time to close the connection. The server, the client, or both close the connection. The server returns to step 2 and waits for the next connection. Socket programming is used for developing client-server applications in Java. In client-server architecture, the server provides some service and the client can use this service to get the desired output or result. Sockets also allow two or more computers to communicate with each other over the network using TCP/IP protocol.
Local Port Scanner
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package socket; import java.io.*; import java.net.*; /** * * @author catalin */ public class LocalPortScanner { public static void main(String [] args) { int port = 1; while(port <= 65535) { try { ServerSocket server = new ServerSocket(port); } catch(IOException e) { System.out.println("Port " + port + " is open!"); } port++; } } }
Learn Java Network Programing Click here