Network Programming in Java Basics:Socket programming using TCP/IP
Networking in java ..Socket programming in Java
1.Socket -What is a socket ? Socket is a connection point like a light bulb socket where you can fit in a connection ..or a light bulb for example .So in computer world , a socket is a connection point to send and receive data .
2.For the client to connect to the server ...it has to know the IP address and the port number of the socket . Again let's remember socket is a opening for connection like socket for your charger connection . Whenever you create a socket you bound it with a port number .
Two types of connection is possible here :-
1.TCP/IP - Its connection oriented .A dedicated virtual link is establish between them and is secure and orderly like a phone call.
2.UDP- less used .. No dedicated link is established ..the packet move with the address and reach the destination like moving through a forest in a confused manner .. No dedicated road or path ..also the receiver ..does expects any packet or something ..less secure ..not orderly
Now ,the process of a simple communication between server and client .
1.Client creates a socket using socket class with IP address and port number of the server and server socket .
2.We write to the socket using appropriate methods of DataOutputStream
3.close the DataOutputStream close the socket . Put try catch over the whole thing .More precisely you can close the stream and socket connections in the finally block to make sure they get closed in Amy situation if some error occurs ..better way of doing things .
4.Create a socket using server socket class
5. Listen to the socket
6.Read the input using DataInputStream class and appropriate methods
7.Print the message received
8. Close the server socket
9.Place the code inside try catch and ideally close the connections in the finally block to ensure that valuable resources get closed in any situation of mishaps .
Awsome explanation.Easy to understand and explain.Thank you for this concept