Java Network Programming

Elliotte Harold

Mentioned 3

Java Network Programming, Third Edition, brings you up-to-date with the latest features of Java's network APIS. This book discusses all the changes and additions to networking in JDK 1.4 and 1.5 (now christened J2SE 5). It covers everything from networking fundamentals to remote method invocation (RMI), including chapters on TCP and UDP sockets, server sockets, URLs and URIs, multicasting, and special-purpose APIS such as JavaMail. This book shows you how to use JSSE to write secure networking applications and explains how to use the NIO APIs to write ultra high-performance servers. And it covers Java's support for network proxies, web cookies, and URL caching. Java Network Programming doesn't just explain the APIS: it shows you how to put them to work. This book is full of examples; it contains thousands of lines of working code (all of which are available online), implementing fully functional network clients and servers. Whether you want to write a special-purpose web server, a secure online order taker, a simple multicast agent, or even an email client, you'll find code that you can learn from and borrow. Whether you're an experienced network developer, a new Java programmer, or someone who just wants to see what's possible, you'll find that Java Network Programming, Third Edition is an important part of your library. Once you've started using the Java Networking APIs, the possibilities are only limited by your imagination.

More on Amazon.com

Mentioned in questions and answers.

In order to cover for my (glaring) lack of knowledge in the basics of networking, I'm looking for a book which would ideally cover:

-> 1 or 2 chapters on the transport layer: tcp, udp...

-> 1 or 2 chapters on the application layer: http, dns...

-> rest of the book would be devoted to pratical way of sending data across the wire using Java-related technologies. This would involve discussions about existing products (eg. hessian, protobuf, thrift, tibco...) , performances comparisons, case studies...etc..

Does such a book exist ?

Edit: Thanks for all the answers so far... however most of the books listed focus heavily on the lower levels of the networking stack (ie. tcp/ip, network administration...). This is one-half of the answer only. I'm still eager to hear suggestions about the other half: discussions around the "state of the art" options available to the Java developer to ferry data around, what products/frameworks are available and how do they compare.

For a TCP/IP text (Not Java centric)

For a Java Networking book I would go with this. Most books are very dated and do not cover the newer stuff, this one covers NIO as well as uses generics in the examples.

If you are looking for improving upon basics on networking it would be better if you look at books which cover basics of networking. Once you are comfortable with the basics of networking you can start with the networking section in Java tutorial and explore the appropriate Java libraries. Networking is an area of its own whose understanding is independent of any programming language.

That said, some of the networking books which I have found helpful are :

Internetworking with TCP/IP, Vol 1 by Douglas Comer

TCP/IP Illustrated Vol.1 by W.Richard Stevens

Computer Networks by Andrew.S.Tanenbaum

As a primer on networking in general, I'd recommend TCP/IP Network Administration, Third Edition, by Craig Hunt. This book provides a chapter on the TCP/IP stack, another on Addressing and routing and the remainder of the book covers in reasonable depth most common network services and diagnostic tools.

For a heavyweight reference, get TCP/IP Illustrated, Vol 1: The Protocols, by Richard Stephens, if you become obsessed with networks buy or borrow volumes 2 and 3.

As far as Java specific networking introduction, I'd suggest Java Network Programming, Third Edition, by Elliotte Rusty Harold, this book does take some critiscim but I still believe it's a good introduction and is an approachable read.

It's a general book for Java beginners but the part about networking is very, VERY clear and easy to grasp.

Head First Java, 2nd Edition

I have a server that is wating on clients to send a packet via DatagramSocket. But when in the Client i use the same ports as the Server (so that they can comunicate) i get an Exception in the Client side:

Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:80)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
at java.net.DatagramSocket.bind(DatagramSocket.java:372)
at java.net.DatagramSocket.<init>(DatagramSocket.java:222)
at java.net.DatagramSocket.<init>(DatagramSocket.java:279)
at tp.Repositorio.main(Cliente.java:146)

So in the server side i have a thread that is wating on the packets. Something like this:

addr = InetAddress.getByName("localhost");
DatagramSocket socket = new DatagramSocket(5008, addr);
DatagramPacket packet = new DatagramPacket(buff, buff.length);
 while(true)
    {
      s.receive(packet);
// and then it throws another thread to treat the packet...
    }

And on the client side i have something like this:

 InetAddress inet;
 inet = InetAddress.getByName("localhost");
 s_data = new DatagramSocket(5008, inet);

I tried to change the port in both sides but it also gave me this exception. If i change the ports to for example 5003 in the Server and 5004 in the Client, it doesn't give me any exception (of course) but they are unable to connect between each other.

You guys have any ideas in order to solve this?

Thanks.

Edit:

Here's the following Client code (it is called repositorio):

/*
* 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 tp;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diogo
 */
public class Repositorio {
    static int nr_ligacoes;
    static int porto;
    static String endereco;


    Repositorio(int pt, String end)
    {
        this.porto = pt;
        this.endereco = end;
    }

    public static void setNr_ligacoes(int nr_ligacoes) {
        Repositorio.nr_ligacoes = nr_ligacoes;
    }

    public static void setPorto(int porto) {
        Repositorio.porto = porto;
    }

    public static void setEndereco(String endereco) {
        Repositorio.endereco = endereco;
    }



    public File[] getFicheiros()
    {
        File folder = new File("C:\\temp2");

        File[] ficheiros = folder.listFiles();


        for (int i = 0; i < ficheiros.length; i++) {
            if (ficheiros[i].isFile()) {
                System.out.println("File " + ficheiros[i].getName());
            } else if (ficheiros[i].isDirectory()) {
                System.out.println("Directory " + ficheiros[i].getName());
            }
        }

        return ficheiros;
    }

    public int getNr_ligacoes() {
        return nr_ligacoes;
    }


    public int getPorto() {
        return porto;
    }

    public String getEndereco() {
        return endereco;
    }

    public ArrayList<String> getListadeFicheiros()
    {
        ArrayList<String> fich_nome = new ArrayList<>();
        File[] fich = getFicheiros();

        for(int i = 0; i <fich.length ; i++)
            fich_nome.add(fich[i].getName());

        return fich_nome;
    }

    public boolean VerFicheiro(String nome)
    {
        File[] fich = getFicheiros();


        for(int i = 0; i<fich.length;i++)
            if(nome.compareTo(fich[i].getName()) == 0)
                return true;
        return false;
    }




    public static void main(String[] args) throws IOException
    {
        ServerSocket socket_r;
        File localDirectory;
        DatagramSocket s_data;
        DatagramPacket p;


        /* if(args.length != 4){
        System.out.println("Sintaxe: java Repositorio serverTcpPort serverAddress localDirectory");
        return;
        }  */

        localDirectory = new File("C:\\temp2");

        if(!localDirectory.exists()){
            System.out.println("A directoria " + localDirectory + " nao existe!");
            return;
        }

        if(!localDirectory.isDirectory()){
            System.out.println("O caminho " + localDirectory + " nao se refere a uma directoria!");
            return;
        }

        if(!localDirectory.canWrite()){
            System.out.println("Sem permissoes de escrita na directoria " + localDirectory);
            return;
        }

        //Repositorio r = new Repositorio(5002,"localhost");
        InetAddress inet;
        inet = InetAddress.getByName("localhost");
        socket_r = new ServerSocket(5003);

        new lancaRepositorioCliente(socket_r, localDirectory).start();

        s_data = new DatagramSocket(5008, inet);

        new lancarepositorioServidor(s_data, localDirectory).start();


    }




    static class lancarepositorioServidor extends Thread{

        DatagramSocket s;
        File localDirectory;
        ListadeRepositorios listaRep;

        lancarepositorioServidor(DatagramSocket s_data, File local)
        {
            this.localDirectory = local;
            this.s = s_data;
        }

        @Override
        public void run() {
            System.out.println("Estou no lanca to servidor");
            byte[] buf = new byte[10000];
            DatagramPacket pack = new DatagramPacket(buf, 128);

            new repositorioToServidor(pack,s).start();

        }

    }
    static class repositorioToServidor extends Thread{
        DatagramPacket pack;
        DatagramSocket s;

        public repositorioToServidor(DatagramPacket packet, DatagramSocket s) {
            this.pack = packet;
            this.s = s;

        }

        @Override
        public void run() {

            while(true)
            {
                try {
                    sleep(10000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Repositorio.class.getName()).log(Level.SEVERE, null, ex);
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
                ObjectOutputStream oos;
                try {
                    oos = new ObjectOutputStream(bos);
                    oos.writeObject(new Notificacao(nr_ligacoes));

                    DatagramPacket packet = new DatagramPacket(bos.toByteArray(), bos.size());
                    s.send(packet);

                } catch (IOException ex) {
                    Logger.getLogger(Repositorio.class.getName()).log(Level.SEVERE, null, ex);
                }


            }


        }


    }


    static class repositorioToCliente extends Thread{
        Socket s;
        File localDirectory;

        public repositorioToCliente(Socket s, File local) {
            this.s = s;
            this.localDirectory = local;
        }

        public Socket getS() {
            return s;
        }

        @Override
        public void run() {

            System.out.println("Estou no lanca to cliente");


        }


    }


    static class lancaRepositorioCliente extends Thread{
        ServerSocket s;
        File localDirectory;

        public lancaRepositorioCliente(ServerSocket s, File local) {
            this.s = s;
            this.localDirectory = local;
        }

        public ServerSocket getS() {
            return s;
        }

        @Override
        public void run() {
            Socket sClient;

            while(true)
            {
                try {
                    sClient = s.accept();

                    new repositorioToCliente(sClient,localDirectory).start();
                    // vamos aqui receber o ficheiro que é para eliminar/depositar

                } catch (IOException ex) {
                    Logger.getLogger(Repositorio.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }


    }

}

Server Code:

    package tp;

/*
* 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.
*/

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Diogo
 */
public class Servidor{

    protected File localDirectory;
    static String LoginfileName;
    //List <Repositorio> rep = null;
    static ListadeRepositorios listaRep;
    static ArrayList <String> ficheiros = null;

    public Servidor()
    {
        this.ficheiros = new ArrayList<>();
    }

    public void setFicheiros(List <String> a)
    {
        a.stream().forEach((a1) -> {
            ficheiros.add(a1);
        });
    }

    public static void main(String[] args) throws IOException
    {
        Servidor s = new Servidor();
        ListadeRepositorios lista_rep = new ListadeRepositorios();
        int listeningPort1;
        int listeningPort2;
        ServerSocket serverSocket;
        InetAddress addr;
        DatagramSocket socket;

        try {

            //listeningPort = Integer.parseInt(args[0]);
            listeningPort1 = 5001;
            listeningPort2 = 5008;

            if(listeningPort1 <= 0) throw new NumberFormatException("Porto TCP de escuta indicado <= 0 (" + listeningPort1 + ")");


            LoginfileName = "c:/temp/users.txt";

            serverSocket = new ServerSocket(listeningPort1);

            serverSocket.setSoTimeout(1000000000);

            new lancaCliente(serverSocket, LoginfileName).start();

            addr = InetAddress.getByName("localhost");
            socket = new DatagramSocket(listeningPort2, addr);


            new lancaRepositorio(socket).start();


        }catch(NumberFormatException e){
            System.out.println("O porto de escuta deve ser um inteiro positivo.");
        }
    }


    static class lancaCliente extends Thread{

        ServerSocket s;
        String Login;

        public lancaCliente(ServerSocket s, String Login) {

            this.s = s;
            this.Login = Login;
        }

        @Override
        public void run() {
            Socket accept;

            while(true)
            {
                try {

                    System.out.println("Servidor à espera de clientes: ");


                    accept = s.accept();
                    System.out.println("Servidor aceitou Cliente");

                    new atendeCliente(accept, this.Login).start();


                } catch (IOException ex) {
                    Logger.getLogger(lancaCliente.class.getName()).log(Level.SEVERE, null, ex);
                }


            }

        }

    }

    static class atendeCliente extends Thread{

        int porto;
        //List<Repositorio> repositorios;
        List<String> clienteLogado;
        //List<Socket> socketsAbertos;
        List<String> infoFicheiros;
        Socket Scliente;
        List<String> lines;
        String arg0 = null,arg1 = null, arg2=null;
        String[] comando;
        String filename;
        ListadeRepositorios listaRep2;


        public static final int TIMEOUT = 5; //segundos

        atendeCliente(Socket s, String nome)
        {
            this.Scliente = s;
            this.filename = nome;
        }



        protected int processaLogin(String user, String pass) throws IOException
        {
            String separaUser = null, separaPass=null;
            String[] separa;

            Path caminho = Paths.get("C:/temp/users.txt");

            Charset charset = Charset.forName("ISO-8859-1");

            try {
                lines = Files.readAllLines(caminho, charset);

                lines.stream().forEach((line) -> {
                    System.out.println(line);
                });
            } catch (IOException e) {
                System.out.println(e);
            }

            for(int i = 0; i<lines.size();i++)
            {
                separa = lines.get(i).split("\\s+");
                separaUser = separa[0];
                separaPass = separa[1];

                if(user.compareTo(separaUser) == 0 && pass.compareTo(separaPass)==0)
                    return 1;
            }
            return 0;

        }


        @Override
        public void run()
        {
            BufferedReader buf = null;
            PrintWriter escreve = null;
            int estaLogado;
            String mensagem = "";

            try {
                buf = new BufferedReader(new InputStreamReader(Scliente.getInputStream()));
            } catch (IOException ex) {
                Logger.getLogger(atendeCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                escreve = new PrintWriter(Scliente.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(atendeCliente.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                while(true)
                {
                    try {
                        buf = new BufferedReader(new InputStreamReader(Scliente.getInputStream()));
                    } catch (IOException ex) {
                        Logger.getLogger(atendeCliente.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    System.out.println("Estou a espera de uma mensagem do cliente");
                    mensagem = buf.readLine();

                    System.out.println("Recebi: " + mensagem);
                    comando = mensagem.split("\\s+");
                    arg0  = comando[0];
                    arg1  = comando[1];
                    arg2  = comando[2];
                    System.out.println(comando[0] + comando[1] + comando[2]);
                    System.out.println(arg0 + arg1 + arg2);
                    if("login".compareTo(arg0)==0)
                    {
                        estaLogado = processaLogin(arg1, arg2);
                        if (estaLogado == 0)
                        {
                            escreve.println("Nao existe!");
                            escreve.flush();
                        }
                        else
                        {
                            escreve.println("LoginFeito");
                            escreve.flush();
                        }
                    }

                    else if("deposita".compareTo(arg0)==0 || "apaga".compareTo(arg0) == 0)
                    {
                        Repositorio rep;
                        rep = listaRep2.MenosCongest();
                        int porto_rep = rep.getPorto();
                        String end_rep = rep.getEndereco();
                        String porto_s = Integer.toString(porto_rep);
                        String comandoToCliente = end_rep + " " + porto_s;

                        escreve.println(comandoToCliente);
                        escreve.flush();
                    }

                    else if("listaficheiros".compareTo(arg0) == 0)
                    {
                        ObjectOutputStream outB = new ObjectOutputStream(Scliente.getOutputStream());
                        ficheiros.add("f1.txt");
                        ficheiros.add("f2.txt");
                        ficheiros.add("f3.txt");
                        ficheiros.add("f4.txt");

                        //ficheiros_disponiveis = listaRep.getFicheiros();

                        outB.writeObject(ficheiros);
                        outB.flush();
                    }

                    mensagem = "";
                }



            } catch (IOException ex) {
                Logger.getLogger(atendeCliente.class.getName()).log(Level.SEVERE, null, ex);
            }catch (Exception e){
            }

        }

    }


    static class lancaRepositorio extends Thread{

        DatagramSocket ser;
        ListadeRepositorios listaRep;

        public lancaRepositorio(DatagramSocket s)
        {
            this.ser = s;
        }


        @Override
        public void run() {

            byte[] buff = new byte[1024];

            DatagramSocket s = this.ser;
            DatagramPacket packet = new DatagramPacket(buff, buff.length);
            Repositorio r;

            while(true)
            {
                try {
                    System.out.println("Sevidor à espera de repositorios:");
                    s.receive(packet);
                    System.out.println("Sevidor recepbeu repositorios");
                    r = new Repositorio(packet.getPort(),packet.getAddress().getHostAddress());

                    listaRep.addRepositorio(r);
                    new atendeRepositorio(s,r).start();



                } catch (IOException ex) {
                    Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }

    }



    static class atendeRepositorio extends Thread{

        DatagramSocket so;
        Repositorio r;

        public atendeRepositorio(DatagramSocket s, Repositorio r)
        {
            this.so = s;
            this.r = r;
        }

        @Override
        public void run() {
            while(true)
            {
                try {
                    byte[] incomingData = new byte[1024];
                    DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);

                    so.receive(incomingPacket);

                    byte[] data = incomingPacket.getData();

                    ByteArrayInputStream in;
                    in = new ByteArrayInputStream(data);

                    try (ObjectInputStream iStream = new ObjectInputStream(new ByteArrayInputStream(data))) {
                        Notificacao n1 = (Notificacao) iStream.readObject();

                        for(int i = 0;i<listaRep.getListaRepositorios().size();i++)
                        {
                            if(r == listaRep.getListaRepositorios().get(i))
                            {
                                r.setNr_ligacoes(n1.getN_op());
                            }

                        }

                    }


                } catch (IOException ex) {
                    Logger.getLogger(atendeRepositorio.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(atendeRepositorio.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }



    }



}

While going through Java Network Programming Book's Chapter 12. UDP, I found the following about the DatagramSocket :-

All datagram sockets bind to a local port, on which they listen for incoming data and which they place in the header of outgoing datagrams. If you’re writing a client, you don’t care what the local port is, so you call a constructor that lets the system assign an unused port (an anonymous port). This port number is placed in any outgoing data‐ grams and will be used by the server to address any response datagrams.

If you’re writing a server, clients need to know on which port the server is listening for incoming data‐ grams; therefore, when a server constructs a DatagramSocket, it specifies the local port on which it will listen. However, the sockets used by clients and servers are otherwise identical: they differ only in whether they use an anonymous (system-assigned) or a well-known port. There’s no distinction between client sockets and server sockets, as there is with TCP. There’s no such thing as a DatagramServerSocket.

SOLUTION :-

You're probably confused between the two ports. The Client's DatagramSocket should use anonymous port whereas the packet which you're sending to server should be sent to the Server using server's port. Both are different things. This probably won't generate BindException at all.

Simply use the following constructor call in your Cliente.java for creating Client Socket:-

DatagramSocket socket = new DatagramSocket();

And the DatagramPacket destined for server should use the same port number as that of the server :-

DatagramPacket packet = new DatagramPacket(buf, buf.length, 
                            address, 5008);    // server's port number is 5008
socket.send(packet);

I hope this helps and solves your problem...

This question was asked in multiple choice question in an interview.

Network programming can be done in which of the following languages?

a) C
b) C++
c) Java
d) Pascal

I think it is possible to network programming in each of these technology.

Guys anyone know .....what would be the correct answer?

Indeed all of them (just perhaps Pascal is not a good choice), just take a look at these books :

Network Programming in C

C++ Network Programming, Volume I: Mastering Complexity with ACE and Patterns

Java Network Programming, Third Edition

A question with resources available on SO: Sockets in pascal