0% found this document useful (0 votes)
126 views

Socket-Programming in Java

Socket programming in Java requires three main components - hardware, a medium, and software. It uses sockets which are endpoints for two-way communication and are defined by a protocol, IP address, and port number. Common protocols are TCP and UDP. To create a client socket, a Socket object is instantiated with an IP address and port number. For a server socket, a ServerSocket object is created to listen on a port for client connections. Sockets perform operations like connecting, sending data, receiving data, and closing connections.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Socket-Programming in Java

Socket programming in Java requires three main components - hardware, a medium, and software. It uses sockets which are endpoints for two-way communication and are defined by a protocol, IP address, and port number. Common protocols are TCP and UDP. To create a client socket, a Socket object is instantiated with an IP address and port number. For a server socket, a ServerSocket object is created to listen on a port for client connections. Sockets perform operations like connecting, sending data, receiving data, and closing connections.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Socket Programming in Java

The First Step of Network Programming

Outline

Introduction A step to socket programming - Protocol - IP address - Port Number Client Socket Server Socket Conclusion

Device Network
.

A device network is an interconnected collection of remote devices.

Basic Concept

Socket programming requires three things viz. Hardware (H/W)-router, repeaters & switch Medium- wire based & wireless

Software (S/W)- web browser, server applications

Socket

Socket is one of end point of two way communication. Socket =Protocol+Ip Address+ Port Number.

Protocol

Protocol Set of rule for communication. Protocol-http,ftp,smtp. TCP-Transmission Control Protocol. Connection Oriented. Trusted protocol. UDP-User Datagram Protocol. Connectionless . UnTrusted Protocol.

Protocol
.
High Level Protocol

Protocol
Low Level/Carrier protocol

TCP

UDP

IP Address

This is 4-byte number . Use: identify a particular device uniquely on the network.
Network Address

IP Address
Node Address

Port Number

It just a logical number. Each application have a port number. The range 0- 1023 is already reserve by high level application.

Interchange The Socket


.

Socket Operations

There are four fundamental operations a socket performs. These are:


1.Connect to a remote machine 2. Send data 3. Receive data 4. Close the connection

Constructing a Clint Socket

Import java.net Create A Socket Class Object. Pass the IPAddress and Port Number as a argument in their Constructor . Create Proper Exception Handler.

Example Clint Socket


Import java.net.*; Socket MyClient; try {
MyClient = new Socket("Machine name",PortNumber);

} catch (IOException e) { System.out.println(e); }

Constructing Server Socket

When implementing a server you need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. Import java.net Create anObject of ServerSocket class. Give a port number to their constructor. Call the accept() method by their reference.

Example Server Socket


import java.net.*; ServerSocket MyService; try {
MyServerice = new ServerSocket(PortNumber); Socket S = MyService.accept();

} catch (IOException e) { System.out.println(e); }

Closing The Sockets

You should always close the output and input stream before you close the socket. output.close(); input.close(); MyClient.close(); serviceSocket.close(); MyService.close();

Conclusion

Socket programming implementation is given by more technologies. Java7 technology providing high performance network stream.

Thank you

You might also like