How can I be a network programmer

Java Standard: Network programming

Java is one of the first programming languages ​​which was developed from the ground up with network support. It was also the applets that helped Java to break through in the programming world.

One of the most important innovations in Java was that a programmer had to make very little effort to make software network-compatible. The "magic" in Java (or specifically the socket class) does the bulk of the work. Anyone who has ever written a small server / client in C can tell you how time-consuming this can be. In contrast, the java.net. * Package offers all the basics you need to write a small network-based program in 5 minutes.

Network programming basics [edit]

This chapter explains the basics of network-based software, protocols, sockets, streams, etc. - Note: There is an overlap with the following chapters.

Sockets for clients [edit]

This chapter describes how a program connects to a server and can exchange data with it.

In most cases, data transfer between the server and the host is based on TCP. The class provides us with such a TCP connection. However, not all ports of an operating system are designed for data transmission on the basis of TCP. In order to know on which ports we could open a TCP connection, we can "tinker" our own little port scanner with Java.

1 import java.net. *; 2 import java.io. *; 3 4 public class PortScanner {5 6 public static void main (String [] args) {7 8 String host = "localhost"; 9 10 if (args.length> 0) {11 host = args [0]; 12} 13 for (int i = 1; i <1024; i ++) {14 try {15 Socket s = new socket (host, i); 16 System.out.println ("There is a server on port" + i + "of" 17 + host); 18} 19 catch (UnknownHostException e) {20 System.err.println (e); 21 break; 22} 23 catch (IOException e) {24 // must not be a server on this port 25} 26} 27} 28}

The program tests all ports in the range 1 - 1024. If a TCP - capable server is "listening" on one of these ports, a corresponding message is output. Line 15 is important in this program. One of the constructors of the Socket class is called here.

Here is the faster option:

1 package PortScanner; 2 3 / * 4 * The port scanner is a good example of the sensible use of threads. 5 * Otherwise you would have to wait for a 6 * timeout for every port to which the connection does not work before continuing. This timeout is not very long, 7 * but long enough if you test 1000 ports. 8 * / 9 10 public class PortscannerMain {11 12 public static void main (String [] args) {13 String host = "localhost"; 14 for (int i = 0; i <1023; i ++) {15 Connthread curr = new Connthread (host, i); 16 thread th = new thread (curr); 17 th.start (); 18 try {19 thread.sleep (1); 20} 21 catch (InterruptedException ex) {} 22} 23} 24}
1 package PortScanner; 2 3 import java.io.IOException; 4 import java.net. *; 5 6 public class Connthread implements Runnable {7 int i; 8 string host; 9 public connthread (string host, int i) {10 this.i = i; 11 this.host = host; 12} 13 14 public void run () {15 try {16 Socket target = new Socket (host, i); 17 System.err.println ("Connected to" + host + "on Port" + i); 18 target.close (); 19} 20 catch (UnknownHostException ex) {21 System.out.println ("Unknown Host" + host); 22} 23 catch (IOException ex) {System.out.println (i);}; 24} 25 26}

Sockets for servers [edit]

This chapter describes how to write a server that "listens" to incoming connections and replies to the requests.