0% found this document useful (0 votes)
72 views6 pages

84 Network

The document discusses various topics relating to computer networking including how the telephone network and internet work, common networking protocols like TCP/IP and HTTP, how email is sent using SMTP, and includes code for a basic Java email client that utilizes the SMTP protocol to send a test email message.

Uploaded by

Day Night
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

84 Network

The document discusses various topics relating to computer networking including how the telephone network and internet work, common networking protocols like TCP/IP and HTTP, how email is sent using SMTP, and includes code for a basic Java email client that utilizes the SMTP protocol to send a test email message.

Uploaded by

Day Night
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture Outline

8 Networking Networking.
!Communicate between different devices.
!Client-server.
!Peer-to-peer.

Networking in Java.
If you have a wireless laptop, download the following programs: !Mail spoofing.
http://www.cs.princeton.edu/introcs/84network/ChatClient.java
!Echo.
http://www.cs.princeton.edu/introcs/84network/In.java !Remote control.
http://www.cs.princeton.edu/introcs/84network/Out.java !Chatroom.

Detours.
! Graphical user interface.
! Threads.

Introducti on to Computer Sci ence Robert Sedgew i ck and Kevi n Wayne http://w w w .cs.Pri nceton.EDU/IntroCS 2

The Telephone Network Internet

Telephone network. Internet.


! Circuit switching: single physical path between sender and receiver ! Global communication network containing million of computers.
is dedicated for duration of communication. ! Computer networks communicate using TCP/IP protocol.
! Has worked for 100 years. ! Provides access to services: email, chat, world wide web, KaZaa.
! Advantage: real-time communication. ! Started by military around 1969 as ARPANET: survivability,
robustness, efficiency.
! Operating system and hardware independent.

Everybody but you grew up without it!

3 4
TCP/IP Protocols

Internet protocol. (IP) Many higher layer application protocols use TCP/IP.
!Rules for moving bits from A to B. !Used by http, smtp, telnet, ftp.
!Divide data into packets (header bits to say where to go, data bits)
and transmit each individually, possibly along different paths. Ex 1: HyperText Transfer Protocol (HTTP).
!No guarantee packets arrive in order, or even arrive. ! Set of rules for transferring files (text, graphics, video).
! Server java.sun.com waits for connection requests.
Transmission control protocol. (TCP) ! Browser establishes connection with server at http://java.sun.com.
!Rules to provide reliable communication between two computers. ! Browser issues GET and POST commands.
tells system to use http protocol to
!Packets arrive, and they arrive in order. ! Server responds with header and body.
get the specified resource
resend over IP until recipient acknowledges

Reference: COS 111 lecture notes. ISO 7-layer model


5 6

SMTP Java Client: SMTP

Ex 2: Simple Mail Transfer Protocol (SMTP). A Java program that uses SMTP.
!Set of rules for sending email.
import java.net.Socket;
!Server smtp.princeton.edu waits for connection requests on port 25. public class Mail {
!User specifies sender, recipient, subject, message body, etc. public static void main(String[] args) throws Exception { open socket
Socket socket = new Socket("smtp.princeton.edu", 25); open socket
In in = new In(socket);
phoenix.Princeton.EDU% telnet smtp.princeton.edu 25
Out out = new Out(socket); mail server port
220 smtpserver1.Princeton.EDU ESMTP Sendmail 8.12.9/8.12.9;
Sun, 7 Dec 2003 09:18:47 -0500 (EST) out.println("HELO localhost"); SMTP protocol
HELO localhost System.out.println(in.readLine());
250 smtpserver1.Princeton.EDU Hello phoenix.Princeton.EDU out.println("MAIL From: AlanTuring@cnn.com"); forged address
[128.112.128.42], pleased to meet you
Warni ng: do not spoof w i thout System.out.println(in.readLine()); read from socket
MAIL From: AlanTuring@cnn.com
250 2.1.0 AlanTuring@cnn.com... Sender ok recei ver's consent out.println("RCPT To: wayne@cs.princeton.edu"); write to socket
RCPT TO: wayne@cs.princeton.edu System.out.println(in.readLine());
250 2.1.5 wayne@cs.princeton.edu... Recipient ok out.println("DATA");
DATA out.println("Subject: Just wanted to say hi");
354 Enter mail, end with "." on a line by itself out.println("Hope you're enjoying COS 126.");
Subject: Just wanted to say hi out.println(".");
Hope you're enjoying COS 126.
System.out.println(in.readLine());
.
250 2.0.0 hB7EIlgn024881 Message accepted for delivery
quit out.close(); close socket
in.close();
socket.close();
}
Open relay. smtp server accessible from anywhere. }
7 8
Client-Server Echo Client

Stream socket. Echo client: connect with server, read text from standard input, send
! ADT for two-way communication over TCP/IP. text to server, print whatever server sends back.
read from socket input and write to socket output
import java.net.Socket;
! IP address: identifies computer. public class EchoClient {
! Port number: identifies application. public static void main(String[] args) throws Exception {
! Ex: IP address = 128.112.129.71, Port = 25. Socket socket = new Socket(args[0], 4444); open socket
In stdin = new In();
smtp.princeton.edu smtp In in = new In(socket); server name
mail server email application Out out = new Out(socket);

! Purpose of a Socket is to communicate with another Socket. String s;


while ((s = stdin.readLine()) != null) { read from stdin
out.println(s); send to server
Client-server model. System.out.println(in.readLine()); get from server
! Client = creates socket to communicate with specified server. }

! Server = creates one socket to listen for connection requests; out.close(); close socket
in.close();
creates another socket to communicate with each client. socket.close();
! Ex: client = web browser, server = web server. }
}

9 10

Echo Server Remote Control Server

Echo server: use ServerSocket to listen for connection requests; Remote control server.
connect with a client; read text that client sends; and send it back. !Client sends text commands.
!Server launches: { TiVo recorder, coffee maker, burglar alarm }.
public class EchoServer { !Server acknowledges task completion.
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(4444);
open server socket Trivial modification to Java programs.
while (true) { and listen on port 4444
! Detail: must be running Java coffee maker on Internet.
Socket socket = serverSocket.accept();
In in = new In (clientSocket); listen and wait for
Out out = new Out(clientSocket); client to request
connection
String s;
read data from client
while ((s = in.readLine()) != null)
and echo it back
out.println(s);

out.close();
close input streams
in.close();
and socket
socket.close();
}
}
}

11 12
Multi-Threaded Chatroom Application Graphical User Interface

Chatroom client. Graphical user interface.


! Each clients requests a connection to server. ! Interact with computer using
! Client sends message to server. mouse and keyboard.
! Server broadcasts message to ALL connected clients. ! Buttons, menus, scrollbars,
toolbars, file choosers.
JTextArea

Echo + a few details. ! Ex: Mac, Windows, KDE.


JTextField
! Graphical user interface (event-based programming).
! Server must process several simultaneous connections (threads).
mouse click, keyboard entry
% java ChatClient alice bicycle.cs.princeton.edu Event-based programming.
! Program remains in infinite loop, waiting for event.
! Upon event, program executes some code.

Callback.
! Programmer registers an event to listen for.
! Programmer writes code to process event.

13 14

Graphical User Interface Multi-Threaded Server

enteredText: Un-editable 10-by-32 scrollable text area. Threads of control.


typedText: User types one line of text and hits enter. ! "Illusion" that several things are happening at once in a program.
actionPerformed: Callback when user hits enter. similar idea used by OS to execute several programs at once
parallel computers make illusion a reality
public class ChatClient extends JFrame implements ActionListener { ! Timesharing: CPU processes each thread one for a fraction of a
...
private JTextArea enteredText = new JTextArea(10, 32); second before moving on to the next one.
listen for user
private JTextField typedText = new JTextField(32);
entering input
public ChatClient(String screenName, String hostName) { Multi-threaded server.
... !Server listens for connection requests in one thread.
typedText.addActionListener(this); register event
Container c = getContentPane(); !Server handles communication with each client in a separate thread.
c.add(enteredText, BorderLayout.CENTER);
!Makes server scalable can accept requests, independent of speed
c.add(typedText, BorderLayout.SOUTH);
pack(); in handling them.
show(); layout placement of typed text
}

public void actionPerformed(ActionEvent e) { callback


out.println(typedText.getText()); Java has built-in support for threads.
typedText.setText("");
}

15 16
Java Multi-Threaded Server: ChatServer Listener Thread: Broadcast Messages to Clients

Chat server. Server constantly monitors messages sent from clients.


! The listener thread broadcasts messages to clients.
! Each client thread communicates with one client. public class ClientListener extends Thread {
private ArrayList clients;
...
import java.net.Socket;
import java.net.ServerSocket; public void run() { method invoked when thread is started
import java.util.ArrayList; while (true) {
for (int i = 0; i < connections.size(); i++) {
public class ChatServer {
public static void main(String[] args) throws Exception { Client ith = (Client) clients.get(i);
ArrayList clients = new ArrayList(); if (!ith.isAlive()) clients.remove(i);
ServerSocket serverSocket = new ServerSocket(4444); String message = ith.getMessage(); check client i
ClientListener listener = new ClientListener(clients);
listener.start(); start thread to broadcast message if (message != null) {
for (int j = 0; j < clients.size(); j++) {
while (true) { Client jth = (Client) clients.get(j);
Socket socket = serverSocket.accept(); wait for client jth.println(message);
Client client = new Client(socket); connection request
}
} broadcast to everyone
clients.add(client); }
client.start(); sleep(100);
} }
} start separate thread to communicate }
} with each client }

17 18

Synchronization Synchronization in Java

Gotcha: two threads simultaneously accessing the same object. Server maintains a separate thread for each client connection.
! ith connection thread puts next message into client i's buffer. public class Client extends Thread {
! listener thread gets next message from client i's buffer. private String buffer; message received from client
...
public Client(Socket socket) { ... }

public void run() { only this thread gets held up if no incoming message
Dining philosophers problem. String s;
! To eat, philosopher needs both adjacent chopsticks. while ((s = in.readLine()) != null) { setMessage(s); }
}
! If each philosopher grabs chopstick to right, then
waits for chopstick to left, then everyone starves. public synchronized String getMessage() { executes in
if (buffer == null) return null; one thread
! Deadlocking = bane of programming with threads. String temp = buffer;
buffer = null;
notifyAll(); tell setMessage to stop waiting
Java has built-in support for synchronization. return temp;
}
! Indicate blocks of code that can't be simultaneously accessed.
! No need unless you are using more than one thread. public synchronized void setMessage(String s) { executes in
if (buffer != null) another thread
wait();
buffer = s; if current message not yet broadcast,
} then wait to receive next message
}
19 20
Peer-to-Peer Networking Peer-to-Peer Networking

Peer-to-peer networking. Peer-to-peer networking.


!All clients communicate and share resources as equals. !All clients communicate and share resources as equals.
!Ex: Napster, Kazaa, ICQ, Gnutella, Morpheus, Limewire, Gnucleus, !Ex: Napster, Kazaa, ICQ, Gnutella, Morpheus, Limewire, Gnucleus,
Skype, Jabber, eMule, BitTorrent, SoulSeek, . . . Skype, Jabber, eMule, BitTorrent, SoulSeek, . . .

Centralized (e.g., original Napster) Decentralized (e.g., Gnutella).


!Central server keeps index of all files and where they are. User queries neighbors to find file, neighbors query their neighbors.
!

!User queries server to find file, the connects directly with host. No central point of control or failure.
!

!No central bottleneck. Massively scalable.


!

Scalable.
Z
!
Y Napster Server Farm o ...?
Wh
...? s file
ho Who ha
W X does! A? X
I have song A
.?
W ho ..
Wh
Transfer o ...?
song A
Who has song A? Wh
oh
a
Check Yand Z A? s file

Y
X
Reference: http://di gi tal5.ece.tntech.edu/hexb/690f03/lectures.htm Reference: http://di gi tal5.ece.tntech.edu/hexb/690f03/lectures.htm
21 22

Summary

Circuit switching vs. packet switching.


!Circuit switching for real-time communication.
!Packet switching is cheaper per bit.
!Never underestimate the bandwidth of a 747 filled with DVDs!

Client-server vs peer-to-peer.
! Client-server for browsing web.
! Peer-to-peer for file-sharing.

Where to learn more? COS 318, COS 461.

23

You might also like