Socket Programming-Advanced Computer Programminng-Lab Lecture, Lecture notes of Java Programming

This lecture was delivered by Prof. Mudita Tiwari at Cochin University of Science and Technology for Java Programming course. It includes: Networking, Socket, Programming, Communication, Link, Hosts, Ports, Dynamics, Sending, Receiving

Typology: Lecture notes

2011/2012

Uploaded on 07/19/2012

rohit-sharma
rohit-sharma 🇮🇳

4.3

(11)

200 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Networking
Socket programming
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Socket Programming-Advanced Computer Programminng-Lab Lecture and more Lecture notes Java Programming in PDF only on Docsity!

Networking

Socket programming

Socket

 A socket is a endpoint of a two-way communication link between two programmes running on the network

 Socket is a bidirectional communication channel between hosts( a computer on a network can be termed as a host)

Communication link

Socket (endpoints)

Socket’s Dynamics

 A socket is an abstraction of the network similar to the way a file is an abstraction of system’s hard drive.  We store and retrieve data through files from hard drive without knowing the actual dynamics of the hard drive.  Similarly we send and receive data to and from network through socket without actually going into underlying mechanics.

Sending/Receiving Messages

through socket

 We read from or write data to a file using streams.  Similarly to read from or write data to a socket, we use streams 

What is a port

 Transport address to which processes can listen for connection requests  Computer can listen on that specific transport address where some request is being sent.  Local to host: 64k TCP and 64k UDP ports  Well known ports  Below 1024  Standard services

Steps to make a simple client

 Import required packages  Connect/open a socket with server  Get I/O streams of socket  Send/ Receive message  Close socket

Cont…

 Send/ Receive message  pw.println(“hello”);  String recieveMsg=br.readLine();  Close socket  S.close();  Br.close();  Pw.close();

Steps to make a simple server

 Import required packages  Create a Server socket  Wait for incoming connection  Get I/O streams of communication socket  Send/ Receive message  Close socket

 Get I/O streams of socket

 InputStream is=s.getInputStream()  InputStreamReader isr=new InputStreamR(is);  BufferedReader br=new BufferedReader(isr);  OutputStream os=s.getOutputStream()  PrintWriter pw=new PrintWriter(os,true)

Cont…

 Send/ Receive message  pw.println(“hello”);  String recieveMsg=br.readLine();  Close socket  S.close();  Br.close();  Pw.close();