• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Socket Programming
 

Socket Programming

on

  • 8,300 views

Socket Programming for Distributed Systems Course (Lecturer: Me)

Socket Programming for Distributed Systems Course (Lecturer: Me)

Statistics

Views

Total Views
8,300
Views on SlideShare
8,218
Embed Views
82

Actions

Likes
6
Downloads
544
Comments
2

7 Embeds 82

http://www.slideshare.net 32
http://www.ustudy.in 31
http://sed.ucla.edu.ve 13
http://static.slideshare.net 2
http://ustudy.in 2
http://www.lmodules.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • gr8,nyc
    Are you sure you want to
    Your message goes here
    Processing…
  • to goooood
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Socket Programming Socket Programming Presentation Transcript

    • Chapter 3 Socket Programming Lecturer: Sivadon Chaisiri Mathematics, Statistics and Computer Department Faculty of Science, Ubon Rajathanee University
    • Layered Protocols
    • Metadata in a Messages
    • Protocols
      • Application = HTTP, FTP, SMTP, NSF, Telnet, SSH, ECHO, …
      • Presentation = SMB, NCP, …
      • Session = SSH, NetBIOS, RPC, …
      • Transport = TCP, UDP, …
      • Network = IP, ICMP, IPX
      • Data link = Ethernet, Token Ring, ISDN, …
      • Physical = 100BASE-T, 1000BASE-T, 802.11
    • TCP/IP four-layer model
    • IP, TCP, and UDP
      • IP (Internet Protocol)
      • TCP (Transmission Control Protocol)
      • UDP (User Datagram Protocol) = Unreliable communication, no ordering guarantee (e.g., DNS, TFTP, VoIP, …)
    • Ports
      • A port is a special number present in the data packet.
      • Ports are typically used to map data to a particular process running on a computer (i.e., which process associates with the data determining by port number)
      • IANA is responsible for assigning TCP and UDP port numbers to specific used.
        • Well-known ports (0-1023)
        • Registered ports (1024-49151)
        • Dynamic and/or Private ports (49152-65535)
    • Ports and Applications
    • The Client-Server Model
    • Socket Application
      • A socket is a connection between two hosts (endpoints).
      • A socket can perform 7 basic operations.
        • Connect to a remote machine
        • Send data
        • Receive data
        • Close a connection
        • Bind to a port
        • Listen for incoming data
        • Accept connections from remote machines on the bound port
    • Endpoint-to-Endpoint Communications
    • Java and Socket
      • java.net.Socket
      • Constructors:
      • Socket(InetAddress address, int port)
      • Socket(String host, int port)
      • Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
      • Methods:
      • InputStream getInputStream()
      • Out putStream getOutputStream()
      • void close()
      • InetAddress getLocalAddress()
      • int getLocalPort()
      • void setSoTimeout(int timeout)
    • Java and Socket
      • java.net.ServerSocket
      • Constructors:
      • ServerSocket(int port)
      • Methods:
      • Socket accept()
      • void close()
      • void setSoTimeout(int timeout)
    • Socket and ServerSocket
    • Streams More stream classes InputStream : DataInputStream, ObjectInputStream OutputStream : DataOutputStream, ObjectOutputStream
    • Filters of Streams
    • Code: Socket Information
      • import java . net . Socket;
      • public class SocketInfo {
      • public static void main ( String [] args ) throws Exception {
      • Socket socket = new Socket (" www . sanook . com " , 80 ) ;
      • System . out . println (" Connected to " + socket . getInetAddress ()
      • + " on port " + socket . getPort ()
      • + " from port " + socket . getLocalPort ()
      • + " of " + socket . getLocalAddress ()) ;
      • }
      • }
    • Steps to develop a socket app
      • Client-Side
        • Bind Socket object with a specified host&port then connect to the host&port
        • [option] Send data via stream (from getOutputStream)
        • Wait for a response of the host via getInputStream
      • Server-Side
        • Bind ServerSocket object with a specified port
        • Listening for the incoming requesting
        • Accept connection while listened to an incoming contact and get its Socket reference
        • Send/Receive data via streams of the Socket Object
    • Code: Time Server
      • import java . io . DataOutputStream;
      • import java . net . *;
      • import java . util . Date;
      • public class TimeServer {
      • public static void main ( String [] args ) throws Exception {
      • ServerSocket server = new ServerSocket ( 7000 ) ;
      • System . out . println (" Server is started ") ;
      • while ( true ) {
      • Socket socket = server . accept () ;
      • DataOutputStream dos = new DataOutputStream ( socket . getOutputStream ()) ;
      • String time = new Date (). toString () ;
      • dos . writeUTF ( time ) ;
      • socket . close () ;
      • }
      • }
      • }
    • Code: Time Client
      • import java . io . DataInputStream;
      • import java . io . DataOutputStream;
      • import java . net . Socket;
      • public class TimeClient {
      • public static void main ( String [] args ) throws Exception {
      • Socket socket = new Socket (" localhost " , 7000 ) ;
      • DataInputStream din = new
      • DataInputStream ( socket . getInputStream ()) ;
      • String time = din . readUTF () ;
      • System . out . println ( time ) ;
      • }
      • }
    • Code: Hello Server
      • import java . io . *;
      • import java . net . *;
      • public class HelloServer {
      • public static void main ( String [] args ) throws Exception {
      • ServerSocket server = new ServerSocket ( 12345 ) ;
      • System . out . println (" Server is started ") ;
      • while ( true ) {
      • Socket socket = server . accept () ;
      • DataInputStream dis = new
      • DataInputStream ( socket . getInputStream ()) ;
      • DataOutputStream dos = new
      • DataOutputStream ( socket . getOutputStream ()) ;
      • String name = dis . readUTF () ;
      • System . out . println (" I see " + name ) ;
      • dos . writeUTF (" Hello " + name ) ;
      • socket . close () ;
      • }
      • }
      • }
    • Code: Hello Client
      • import java . io*;
      • import java . net . *;
      • public class HelloClient {
      • public static void main ( String [] args ) throws Exception {
      • Socket socket = new Socket (" localhost " , 12345 ) ;
      • DataInputStream din = new
      • DataInputStream ( socket . getInputStream ()) ;
      • DataOutputStream dos = new
      • DataOutputStream ( socket . getOutputStream ()) ;
      • String name = " World " ;
      • if ( args . length > 0 ) name = args [ 0 ] ;
      • dos . writeUTF ( name ) ;
      • String message = din . readUTF () ;
      • System . out . println ( message ) ;
      • }
      • }
    • Code: Busy Hello Server
      • import java . io . *;
      • import java . net . *;
      • public class BusyHelloServer {
      • public static void main ( String [] args ) throws Exception {
      • ServerSocket server = new ServerSocket ( 12345 ) ;
      • while ( true ) {
      • Socket socket = server . accept () ;
      • DataInputStream dis = new
      • DataInputStream ( socket . getInputStream ()) ;
      • DataOutputStream dos = new
      • DataOutputStream ( socket . getOutputStream ()) ;
      • String name = dis . readUTF () ;
      • System . out . println (" I see " + name ) ;
      • for ( int i = 0 ; i < 10 ; i ++) {
      • Thread . sleep ( 1000 ) ;
      • System . out . println (&quot; Delay for &quot; + name + &quot; # &quot; + i ) ;
      • }
      • dos . writeUTF (&quot; Hello &quot; + name ) ;
      • socket . close () ;
      • }
      • }
      • }
    • Code: Multithread Hello Server
      • import java . io . *; import java . net . *;
      • public class ThreadingHelloServer extends Thread {
      • Socket soc;
      • public ThreadingHelloServer ( Socket soc ) { this . soc = soc; }
      • public void run () {
      • try {
      • DataInputStream dis = new
      • DataInputStream ( soc . getInputStream ()) ;
      • DataOutputStream dos = new DataOutputStream ( soc . getOutputStream ()) ;
      • String name = dis . readUTF () ;
      • System . out . println (&quot; I see &quot; + name ) ;
      • for ( int i = 0 ; i < 10 ; i ++) {
      • Thread . sleep ( 1000 ) ;
      • System . out . println (&quot; Delay for &quot; + name + &quot; # &quot; + i ) ;
      • }
      • dos . writeUTF (&quot; Hello &quot; + name ) ;
      • soc . close () ;
      • } catch ( Exception ex ) {ex . printStackTrace () ;}
      • }
      • public static void main ( String [] args ) throws Exception {
      • ServerSocket server = new ServerSocket ( 12345 ) ;
      • while ( true ) {
      • Socket socket = server . accept () ;
      • new ThreadingHelloServer ( socket ) .start () ;
      • }
      • }
      • }
    • Code: Multithread Hello Server (Short Form)
      • import java.io.*; import java.net.*;
      • public class ThreadingHelloServer2 {
      • public static void main(String[] args) throws Exception {
      • ServerSocket server = new ServerSocket(12345);
      • System.out.println(&quot;Server is started&quot;);
      • while(true) {
      • final Socket socket = server.accept();
      • Thread t = new Thread() {
      • public void run() {
      • try {
      • DataInputStream dis = new
      • DataInputStream(socket.getInputStream());
      • DataOutputStream dos = new
      • DataOutputStream(socket.getOutputStream());
      • String name = dis.readUTF();
      • System.out.println(&quot;I see &quot; + name);
      • for (int i = 0 ; i < 10 ; i++) {
      • Thread.sleep(1000);
      • System.out.println(&quot;Delay for &quot; + name + &quot; #&quot; + i);
      • }
      • dos.writeUTF(&quot;Hello &quot; + name); socket.close();
      • } catch(Exception ex) { ex.printStackTrace(); }
      • }};
      • t.start();
      • }}}
    • The End Any Questions?