Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networks Sockets and Streams. TCP/IP in action server ports 13 17 80 …65535 lower port numbers (1..1023) are reserved port echo7 time13 ftp20 telnet23.

Similar presentations


Presentation on theme: "Networks Sockets and Streams. TCP/IP in action server ports 13 17 80 …65535 lower port numbers (1..1023) are reserved port echo7 time13 ftp20 telnet23."— Presentation transcript:

1 Networks Sockets and Streams

2 TCP/IP in action server ports 13 17 80 …65535 lower port numbers (1..1023) are reserved port echo7 time13 ftp20 telnet23 finger79 http80 pop3110

3 Socket = server + port server = www.seas.smu.edu ports 13 17 80 …65535 client Socket http://www.seas.smu.edu

4 The Java Socket Class

5 Java Socket Class server ports 13 17 3180 …65535 client Socket instance

6 Sockets create Streams server ports 13 17 3180 client Socket instance InputStream OutputStream

7 class Socket Socket is the Java representation of a TCP network connection Using Socket, a client can create a stream- based communication channel with a remote host To communicate, a socket connection to the remote host must be established - specifying the address and the port There must be a server program actively listening on the port or the connection will fail -- throw an Exception

8 Socket constructor Socket (String host, int port) throws IOException –port must be by name or text IP address –port must be 1-65535

9 java.net.Socket Constructor: Socket (String host, int port) Socket s = new Socket(“www.sun.com”, 80); in = s.getInputStream(); int k = in.read(); anInputStream (returns bytes) returns 1st byte of web page s in world of TCP/IP

10 Socket methods Socket (String host, int port) –constructor InputStream getInputStream () –gets InputStream for reading OutputStream getOutputStream() –get OutputStream for writing

11 java.net.Socket Constructor: Socket (String host, int port) Socket s = new Socket(“112.62.12.0”, 80); s in world of TCP/IP out can use IP address written as a string the socket instance acts as a stream “factory” -- design pattern

12 more Socket methods synchronized void close () –closes the socket void setSoTimeout (int timeout) –Socket will block for only this amt of time - - then InterruptedException is raised

13 “localhost” “127.0.0.1”

14 SocketTest.java import java.io.*; import java.net.*; class SocketTest { public static void main(String[ ] args) { try { Socket t = new Socket(”nova.seas.smu.edu", 13); BufferedReader is = new BufferedReader (new InputStreamReader(t.getInputStream())); (Continued  )

15 boolean more = true; while (more) { String str = is.readLine(); if (str == null) more = false; else System.out.println(str); } } (Continued  )

16 catch(IOException e) { System.out.println("Error" + e); } }

17 Socket t = new Socket(”nova.seas.smu.edu", 13); BufferedReader is = new BufferedReader (new InputStreamReader( t.getInputStream( ) ) );


Download ppt "Networks Sockets and Streams. TCP/IP in action server ports 13 17 80 …65535 lower port numbers (1..1023) are reserved port echo7 time13 ftp20 telnet23."

Similar presentations


Ads by Google