Chapter 3 Socket Programming Lecturer: Sivadon Chaisiri Mathematics, Statistics and Computer Department Faculty of Science...
Layered Protocols
Metadata in a Messages
Protocols <ul><li>Application = HTTP, FTP, SMTP, NSF, Telnet, SSH, ECHO, …  </li></ul><ul><li>Presentation = SMB, NCP, … <...
TCP/IP four-layer model
IP, TCP, and UDP <ul><li>IP (Internet Protocol) </li></ul><ul><li>TCP (Transmission Control Protocol) </li></ul><ul><li>UD...
Ports <ul><li>A port is a special number  present in the data packet.  </li></ul><ul><li>Ports are typically used to map d...
Ports and Applications
The Client-Server Model
Socket Application <ul><li>A socket is a connection between two hosts (endpoints). </li></ul><ul><li>A socket can perform ...
Endpoint-to-Endpoint Communications
Java and Socket <ul><li>java.net.Socket </li></ul><ul><li>Constructors:  </li></ul><ul><li>Socket(InetAddress address, int...
Java and Socket <ul><li>java.net.ServerSocket </li></ul><ul><li>Constructors:  </li></ul><ul><li>ServerSocket(int port) </...
Socket and ServerSocket
Streams More stream classes InputStream : DataInputStream, ObjectInputStream OutputStream : DataOutputStream, ObjectOutput...
Filters of Streams
Code: Socket Information <ul><li>import java . net . Socket; </li></ul><ul><li>public class  SocketInfo  { </li></ul><ul><...
Steps to develop a socket app <ul><li>Client-Side </li></ul><ul><ul><li>Bind  Socket  object with a specified host&port th...
Code: Time Server <ul><li>import java . io . DataOutputStream; </li></ul><ul><li>import java . net . *; </li></ul><ul><li>...
Code: Time Client <ul><li>import java . io . DataInputStream; </li></ul><ul><li>import java . io . DataOutputStream; </li>...
Code: Hello Server <ul><li>import java . io . *; </li></ul><ul><li>import java . net . *; </li></ul><ul><li>public class  ...
Code: Hello Client <ul><li>import java . io*; </li></ul><ul><li>import java . net . *; </li></ul><ul><li>public class  Hel...
Code: Busy Hello Server <ul><li>import java . io . *; </li></ul><ul><li>import java . net . *; </li></ul><ul><li>public cl...
Code:  Multithread Hello Server <ul><li>import java . io . *; import java . net . *; </li></ul><ul><li>public class  Threa...
Code:  Multithread Hello Server   (Short Form) <ul><li>import java.io.*; import java.net.*; </li></ul><ul><li>public class...
The End Any Questions?
Upcoming SlideShare
Loading in …5
×

Socket Programming

8,053 views

Published on

Socket Programming for Distributed Systems Course (Lecturer: Me)

Published in: Technology
2 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total views
8,053
On SlideShare
0
From Embeds
0
Number of Embeds
133
Actions
Shares
0
Downloads
685
Comments
2
Likes
8
Embeds 0
No embeds

No notes for slide

Socket Programming

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

×