Socket Programming and Multi Threading PDF

Title Socket Programming and Multi Threading
Author Razwan Abdullah
Course Java Application Development 
Institution Humber College
Pages 3
File Size 118.5 KB
File Type PDF
Total Downloads 6
Total Views 137

Summary

kjjkjk...


Description

Razwan Abdullah ID#N01390655 Assignment on Socket Programming and Multi Threading

Socket Programming The JAVA API provides classes for creating sockets for facilitating communication between two processes over the internet. The following steps occur when establishing a 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. 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 –

1. public ServerSocket(int port) throws IOException2. public ServerSocket(int port, int backlog) throws IOException 3. public ServerSocket(int port, int backlog, InetAddress address) throws IOException 4. public ServerSocket() throws IOException Some of the common methods of the ServerSocket class are: public int getLocalPort(), public Socket accept() throws IOException. 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 – 1. public Socket(String host, int port) throws UnknownHostException, IOException. 2. public Socket(InetAddress host, int port) throws IOException 3. public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException. 4. public Socket(InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException. 5. public Socket()

Multi threading Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. Threads can be created by using two mechanisms: 1. Extending the Thread class 2. Implementing the Runnable Interface Thread creation by extending the Thread class We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object. Thread creation by implementing the Runnable Interface We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object. Life Cycle of a Thread A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

Following are the stages of the life cycle − 

New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.



Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.



Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.



Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.



Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.

References used to complete the assignment: 1. https://www.tutorialspoint.com/java/java_multithreading.htm 2. https://www.geeksforgeeks.org/multithreading-in-java/ 3. Multithreading and Socket Programming notes and lecture videos from Humber ITC5201 Database Programming Using Java 4. https://www.edureka.co/blog/socket-programming-in-java/ 5. https://docs.oracle.com/javase/tutorial/networking/sockets/index.html 6. https://www.baeldung.com/a-guide-to-java-sockets...


Similar Free PDFs