Tcp Ip Programming In Java Pdf

  • Java Tutorial
  • Java Object Oriented
  • Java Advanced
  1. In Socket programming using Java, BSD style Socket to Interface with TCP/IP services is used. BSD Socket Interface provides facilities for Interprocess Communication. BSD Socket Interface supports different domain, the UNIX Domain, the Internet Domain and the NS Domain.
  2. Java - Networking. The java.net package provides support for the two common network protocols − TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. UDP − UDP stands for User Datagram Protocol.
  • Java Useful Resources

Socket programming with Java: TCP and UDP. Socket programming boils down to two systems communicating with one another. Network communication comes in two flavors: Transport Control Protocol (TCP) and User Datagram Protocol (UDP). TCP and UDP are used for different purposes and both have unique constraints.

  • Selected Reading

The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.

The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand.

The java.net package provides support for the two common network protocols −

  • TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.

  • UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

This chapter gives a good understanding on the following two subjects −

  • Socket Programming − This is the most widely used concept in Networking and it has been explained in very detail.

  • URL Processing − This would be covered separately. Click here to learn about URL Processing in Java language.

Socket Programming

Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

When the connection is made, the server creates a socket object on its end of the communication. The client and the server can now communicate by writing to and reading from the socket.

The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them.

The following steps occur when establishing a TCP connection between two computers using sockets −

  • The server instantiates a ServerSocket object, denoting which port number communication is to occur on.

  • The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.

  • After the server is waiting, a client instantiates a Socket object, specifying the server name and the port number to connect to.

  • The constructor of the Socket class attempts to connect the client to the specified server and the port number. If communication is established, the client now has a Socket object capable of communicating with the server.

  • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket.

After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream.

TCP is a two-way communication protocol, hence data can be sent across both streams at the same time. Following are the useful classes providing complete set of methods to implement sockets.

ServerSocket Class Methods

The java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests.

The ServerSocket class has four constructors −

Sr.No.Method & Description
1

public ServerSocket(int port) throws IOException

Attempts to create a server socket bound to the specified port. An exception occurs if the port is already bound by another application.

2

public ServerSocket(int port, int backlog) throws IOException

Similar to the previous constructor, the backlog parameter specifies how many incoming clients to store in a wait queue.

3

public ServerSocket(int port, int backlog, InetAddress address) throws IOException

Similar to the previous constructor, the InetAddress parameter specifies the local IP address to bind to. The InetAddress is used for servers that may have multiple IP addresses, allowing the server to specify which of its IP addresses to accept client requests on.

4

public ServerSocket() throws IOException

Creates an unbound server socket. When using this constructor, use the bind() method when you are ready to bind the server socket.

If the ServerSocket constructor does not throw an exception, it means that your application has successfully bound to the specified port and is ready for client requests.

Following are some of the common methods of the ServerSocket class −

Sr.No.Method & Description
1

public int getLocalPort()

Returns the port that the server socket is listening on. This method is useful if you passed in 0 as the port number in a constructor and let the server find a port for you.

2

public Socket accept() throws IOException

Waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time-out value has been set using the setSoTimeout() method. Otherwise, this method blocks indefinitely.

3

public void setSoTimeout(int timeout)

Sets the time-out value for how long the server socket waits for a client during the accept().

4

public void bind(SocketAddress host, int backlog)

Binds the socket to the specified server and port in the SocketAddress object. Use this method if you have instantiated the ServerSocket using the no-argument constructor.

When the ServerSocket invokes accept(), the method does not return until a client connects. After a client does connect, the ServerSocket creates a new Socket on an unspecified port and returns a reference to this new Socket. A TCP connection now exists between the client and the server, and communication can begin.

Socket Class Methods

The java.net.Socket class represents the socket that both the client and the server use to communicate with each other. The client obtains a Socket object by instantiating one, whereas the server obtains a Socket object from the return value of the accept() method.

The Socket class has five constructors that a client uses to connect to a server −

Sr.No.Method & Description
1

public Socket(String host, int port) throws UnknownHostException, IOException.

This method attempts to connect to the specified server at the specified port. If this constructor does not throw an exception, the connection is successful and the client is connected to the server.

2

public Socket(InetAddress host, int port) throws IOException

This method is identical to the previous constructor, except that the host is denoted by an InetAddress object.

3

public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException.

Connects to the specified host and port, creating a socket on the local host at the specified address and port.

4

public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException.

This method is identical to the previous constructor, except that the host is denoted by an InetAddress object instead of a String.

5

public Socket()

Creates an unconnected socket. Use the connect() method to connect this socket to a server.

When the Socket constructor returns, it does not simply instantiate a Socket object but it actually attempts to connect to the specified server and port.

Some methods of interest in the Socket class are listed here. Notice that both the client and the server have a Socket object, so these methods can be invoked by both the client and the server.

Sr.No.Method & Description
1

public void connect(SocketAddress host, int timeout) throws IOException

This method connects the socket to the specified host. This method is needed only when you instantiate the Socket using the no-argument constructor.

2

public InetAddress getInetAddress()

This method returns the address of the other computer that this socket is connected to.

3

public int getPort()

Returns the port the socket is bound to on the remote machine.

4

public int getLocalPort()

Returns the port the socket is bound to on the local machine.

5

public SocketAddress getRemoteSocketAddress()

Returns the address of the remote socket.

6

public InputStream getInputStream() throws IOException

Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.

7

public OutputStream getOutputStream() throws IOException

Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket.

8

public void close() throws IOException

Closes the socket, which makes this Socket object no longer capable of connecting again to any server.

InetAddress Class Methods

This class represents an Internet Protocol (IP) address. Here are following usefull methods which you would need while doing socket programming −

Sr.No.Method & Description
1

static InetAddress getByAddress(byte[] addr)

Returns an InetAddress object given the raw IP address.

2

static InetAddress getByAddress(String host, byte[] addr)

Creates an InetAddress based on the provided host name and IP address.

3

static InetAddress getByName(String host)

Determines the IP address of a host, given the host's name.

4

String getHostAddress()

Returns the IP address string in textual presentation.

5

String getHostName()

Gets the host name for this IP address.

6

static InetAddress InetAddress getLocalHost()

Returns the local host.

7

String toString()

Converts this IP address to a String.

Socket Client Example

The following GreetingClient is a client program that connects to a server by using a socket and sends a greeting, and then waits for a response.

Example

Socket Server Example

The following GreetingServer program is an example of a server application that uses the Socket class to listen for clients on a port number specified by a command-line argument −

Example

Programming

Compile the client and the server and then start the server as follows −

Check the client program as follows −

Output

Update April 17, 2017: Two students asked me for subtitles. I removed the auto-generated subtitles and I added right subtitles for every lesson. Also, sound quality has been improved for some lessons. Thanks for your suggestion! Now, every student can follow this course more easily.

Update September 17, 2016: Some of my students asked me for a chat application. So
I created the application (GUI included) and I added it in chapter 9.
Now, you can download the application, run it and view the source code. Enjoy!

Update August 4, 2016: Added: quizzes for learning reinforcement.

---------------------------------------------------------------

Student Testimonials!

★★★★★

The best course ever on udemy , it teaches student not only the how to build network application but also discuss all programming topics like streams , thread that involved on building network applications ,the teacher explains while he is coding every step in details , and explain why he prefered this method over that method . Mr. Catalin provides detail answers for any question in quick time . Take this course and you will master building network applications

Rashed

★★★★★

Professional and up to the point. It tells the beginners every single step about Java Network Programming which is very nice. It makes the difficult things look easy.

Sobhan N.

★★★★★

Excelente curso, esta muy bien explicado y la sintaxys del codigo es muy clara, solo me gustaria ver streaming de audio en tiempo real.

Juan


Course Description

With over 45 videos this online Java training course is designed to provide a solid foundation in Java network programming and network communication.

---------------------------------------------------------------

Learn something new

Network programming is one field which everybody uses but is still considered an advanced topic.

---------------------------------------------------------------

OpenSource Code

Allsource code shown in this course is also available for download.Students can create their own projects usingthe downloaded java files.

Tcp Ip Programming In Java Pdf

---------------------------------------------------------------

Combinetheory with practice!

Eachchapter has an intro video tocover basic theory needed to understand the rest of the materialcovered.

In the first chapter you will learn the concept of network communication from the very basic fundamental level that needs to be understood by everyone.

Two other chapters are included in this course for those programmer who haven’t worked yet with java threads or java IO. These two are very important topics when you want to build a network application and I want to ensure that all students have the basics before we start to code Client – Server applications.

In the Socket (Java Socket programming) section you will start to write simple TCP servers or TCP clients. Also, you will learn how to create a multi-user or a multithreaded server and we are going to test each written application. We will connect multiple TCP clients to a multithreaded server and we will observe the TCP server behavior and how it makes to respond to multiple clients at the same time.

In this chapter I will cover some other important network programming topics:

  • Scan a remote host for open ports

  • Get network interface hardware information

  • Validate an IP address in different ways

  • Ping a remote host address using java code

  • Browse internet using java code.

  • Web scrapping – get what data you want from a web page

Java Programming Language Pdf Download

UDP chapter focus on the difference between the most used Transport Layer protocols: TCP and UDP. We will write together an UDP server and an UDP client. The interesting part of this chapter comes after we will run the code. The next chapters present some useful applications:

  • Send an email

  • Transfer files between a server and a client using TCP sockets

  • A small web browser.

The code is explained in details, line by line. All the needed theory is presented in the application intro and we will test each application after the source code is explained.


So let’s start the interesting journey now!

Cheers,

Tcp Ip Programming In C

Catalin Baba

Java Programming Tutorial Pdf



  • The course is aimed for anyone who wants to learn network programming
  • If you have never written code, this course is probably not for you.