Socket Programming: Understanding Sockets, Ports, and Communication in Networking, Slides of Java Programming

An introduction to socket programming, explaining what a socket is, its relationship to ports, and the dynamics of sending and receiving messages through sockets. It also covers the basics of creating a simple client and server using java, with examples for importing required packages, connecting to a server, getting i/o streams, sending and receiving messages, and closing the socket.

Typology: Slides

2011/2012

Uploaded on 07/12/2012

ehsan
ehsan ๐Ÿ‡ฎ๐Ÿ‡ณ

4.1

(50)

32 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: Understanding Sockets, Ports, and Communication in Networking and more Slides 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();