+ All Categories
Home > Documents > Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275:...

Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275:...

Date post: 31-May-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
38
1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker University of Delaware Fall 2010
Transcript
Page 1: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

1

CISC 275: Introduction to Software Engineering

Lab 6:Introduction to Java Networking

Charlie Greenbacker

University of DelawareFall 2010

Page 2: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

2

Overview

l Networking reviewl Client-Server model

l Sockets & port numbersl Code walkthrough of turn-based chat program

l Server & client applicationsl Full code available on website

l Sample executionl Lab exercise

Page 3: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

3

Networking reviewl Client-Server model:

l Relationship between two computer programs

l Either on the same computer or separate computers on the same network

l Client program makes service requests to server program

l Data is transmitted back and forth across a network connection

l Server listens for incoming connections, client requests to connect, connection is established and data exchange begins

Page 4: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

4

Networking review

l Socket:l One end of a two-way communication link

between two programs running on a networkl Socket classes represent connection between a

server program and a client programl java.net.ServerSocket: server listens for

incoming connection requestsl java.net.Socket: client sends data to server,

server receives data from client, and vice-versa

Page 5: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

5

Networking review

l Port number:l Combined with IP address, specifies endpoint of

network connection

Page 6: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

6

Code walkthrough (server)

public class ChatServer { public static void main(String[] args) throws IOException { int portNumber = 9876; // each team should choose a // unique number

ServerSocket serverSocket = null; Socket clientSocket = null; PrintWriter socketOut = null; BufferedReader socketIn = null, consoleIn = null; String toClient = "", fromClient = "";

Page 7: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

7

Code walkthrough (server)

// display server IP address for client to input

System.out.println("Server IP address: " + InetAddress.getLocalHost().getHostAddress());

System.out.println("(client must enter this IP address to connect)\n");

Page 8: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

8

Code walkthrough (server)

// listen for inbound connection

try { serverSocket = new ServerSocket(portNumber); System.out.print("Listing for inbound connection on " "port " + portNumber + "... "); } catch (IOException e) { System.err.println("Could not listen on port: " + portNumber + ". error: " + e); System.exit(1); }

Page 9: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

9

Code walkthrough (server)

// accept connection request

try { clientSocket = serverSocket.accept(); System.out.println("Connection established!"); } catch (IOException e) { System.err.println("Accept failed. error: " + e); System.exit(1); }

Page 10: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

10

Code walkthrough (server)

// instantiate I/O objects

socketOut = new PrintWriter(clientSocket.getOutputStream(), true);

socketIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

consoleIn = new BufferedReader(new InputStreamReader(System.in));

Page 11: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

11

Code walkthrough (server)

// loop conversation until "bye" is received while (!fromClient.equalsIgnoreCase("bye")) { System.out.print("you: "); toClient = consoleIn.readLine(); socketOut.println(toClient);

fromClient = socketIn.readLine(); System.out.println("them: " + fromClient); }

Page 12: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

12

Code walkthrough (server)

// close I/O connections socketOut.close(); socketIn.close(); consoleIn.close(); clientSocket.close(); serverSocket.close(); } }

Page 13: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

13

Code walkthrough (client)

public class ChatClient { public static void main(String[] args) throws IOException { int portNumber = 9876; // each team should choose // a unique number

Socket socket = null; PrintWriter socketOut = null; BufferedReader socketIn = null, consoleIn = null; String fromServer = "", toServer = "";

Page 14: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

14

Code walkthrough (client)

// prompt user to input IP address of server System.out.print("Enter server IP address (obtain from " + " server):"); consoleIn = new BufferedReader(new InputStreamReader(System.in)); String serverIP = consoleIn.readLine(); System.out.println("");

Page 15: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

15

Code walkthrough (client)

// connect to server try { System.out.print("Trying to connect to server... "); socket = new Socket(serverIP, portNumber); System.out.println("Connection established!"); } catch (UnknownHostException e) { System.err.println("Don't know about host at: " + serverIP + ". error: " + e); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection" + " at: " + serverIP + ". error: " + e); System.exit(1); }

Page 16: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

16

Code walkthrough (client)

// instantiate I/O objects socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); socketOut = new PrintWriter(socket.getOutputStream(), true);

Page 17: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

17

Code walkthrough (client)

// loop conversation until "bye" is entered while (!toServer.equalsIgnoreCase("bye")) { fromServer = socketIn.readLine(); System.out.println("them: " + fromServer);

System.out.print("you: "); toServer = consoleIn.readLine(); socketOut.println(toServer); }

Page 18: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

18

Code walkthrough (client)

// close I/O connections socketIn.close(); socketOut.close(); consoleIn.close(); socket.close(); } }

Page 19: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

19

Sample executionl Can run both server & client on same machine or

on separate machines (far more interesting)

l Easiest to compile & run from command line instead of from inside Eclipse

l Open two terminal windows if running both on same machine

l Following sample execution will show full process, starting from compilation, in two separate windows displayed in columns

l Server on left, client on right

Page 20: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

20

Sample execution‣ ‣

Page 21: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

21

Sample execution‣ javac *.java ‣

Page 22: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

22

Sample execution‣ javac *.java

Page 23: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

23

Sample execution‣ javac *.java

‣ java ChatServer

Page 24: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

24

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876...

Page 25: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

25

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876...

‣ java ChatClient

Page 26: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

26

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876...

‣ java ChatClientEnter server IP address (obtain from server):

Page 27: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

27

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876...

‣ java ChatClientEnter server IP address (obtain from server):

Page 28: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

28

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876...

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Page 29: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

29

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you:

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

Page 30: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

30

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

Page 31: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

31

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you:

Page 32: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

32

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

Page 33: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

33

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

them: hi

you:

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

Page 34: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

34

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

them: hi

you: a/s/l?

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

Page 35: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

35

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

them: hi

you: a/s/l?

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

them: a/s/l?

you:

Page 36: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

36

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

them: hi

you: a/s/l?

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

them: a/s/l?

you: bye

Page 37: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

37

Sample execution‣ javac *.java

‣ java ChatServerServer IP address: 128.175.13.74

(client must enter this IP address to connect)

Listing for inbound connection on port 9876... Connection established!

you: hello

them: hi

you: a/s/l?

them: bye

[process terminated]

‣ java ChatClientEnter server IP address (obtain from server):

128.175.13.74

Trying to connect to server...Connection established!

them: hello

you: hi

them: a/s/l?

you: bye

[process terminated]

Page 38: Intro to Java Networking - University of Delawareyouse/CISC275/Lab6_Java_Net.pdf1 CISC 275: Introduction to Software Engineering Lab 6: Introduction to Java Networking Charlie Greenbacker

38

Lab exercise (in pairs)l Download & run the code on two computers

l One partner will run server, the other runs client

l Both machines must be on the same networkl Be sure to choose a random high port number

l And modify the code for both partners!l Launch the programs & have a chat

l Compile, start server, start client, enter server IP address into client, exchange messages

l Don't submit – show me output before you leave


Recommended