KEMBAR78
Socket Programming | PPT
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 (&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 () ; } } }
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?

Socket Programming

  • 1.
    Chapter 3 SocketProgramming Lecturer: Sivadon Chaisiri Mathematics, Statistics and Computer Department Faculty of Science, Ubon Rajathanee University
  • 2.
  • 3.
    Metadata in aMessages
  • 4.
    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
  • 5.
  • 6.
    IP, TCP, andUDP IP (Internet Protocol) TCP (Transmission Control Protocol) UDP (User Datagram Protocol) = Unreliable communication, no ordering guarantee (e.g., DNS, TFTP, VoIP, …)
  • 7.
    Ports A portis 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)
  • 8.
  • 9.
  • 10.
    Socket Application Asocket 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
  • 11.
  • 12.
    Java and Socketjava.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)
  • 13.
    Java and Socketjava.net.ServerSocket Constructors: ServerSocket(int port) Methods: Socket accept() void close() void setSoTimeout(int timeout)
  • 14.
  • 15.
    Streams More streamclasses InputStream : DataInputStream, ObjectInputStream OutputStream : DataOutputStream, ObjectOutputStream
  • 16.
  • 17.
    Code: Socket Informationimport java . net . Socket; public class SocketInfo { public static void main ( String [] args ) throws Exception { Socket socket = new Socket (&quot; www . sanook . com &quot; , 80 ) ; System . out . println (&quot; Connected to &quot; + socket . getInetAddress () + &quot; on port &quot; + socket . getPort () + &quot; from port &quot; + socket . getLocalPort () + &quot; of &quot; + socket . getLocalAddress ()) ; } }
  • 18.
    Steps to developa 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
  • 19.
    Code: Time Serverimport 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 (&quot; Server is started &quot;) ; while ( true ) { Socket socket = server . accept () ; DataOutputStream dos = new DataOutputStream ( socket . getOutputStream ()) ; String time = new Date (). toString () ; dos . writeUTF ( time ) ; socket . close () ; } } }
  • 20.
    Code: Time Clientimport 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 (&quot; localhost &quot; , 7000 ) ; DataInputStream din = new DataInputStream ( socket . getInputStream ()) ; String time = din . readUTF () ; System . out . println ( time ) ; } }
  • 21.
    Code: Hello Serverimport java . io . *; import java . net . *; public class HelloServer { public static void main ( String [] args ) throws Exception { ServerSocket server = new ServerSocket ( 12345 ) ; System . out . println (&quot; Server is started &quot;) ; 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 (&quot; I see &quot; + name ) ; dos . writeUTF (&quot; Hello &quot; + name ) ; socket . close () ; } } }
  • 22.
    Code: Hello Clientimport java . io*; import java . net . *; public class HelloClient { public static void main ( String [] args ) throws Exception { Socket socket = new Socket (&quot; localhost &quot; , 12345 ) ; DataInputStream din = new DataInputStream ( socket . getInputStream ()) ; DataOutputStream dos = new DataOutputStream ( socket . getOutputStream ()) ; String name = &quot; World &quot; ; if ( args . length > 0 ) name = args [ 0 ] ; dos . writeUTF ( name ) ; String message = din . readUTF () ; System . out . println ( message ) ; } }
  • 23.
    Code: Busy HelloServer 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 (&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 () ; } } }
  • 24.
    Code: MultithreadHello 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 () ; } } }
  • 25.
    Code: MultithreadHello 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(); }}}
  • 26.
    The End AnyQuestions?