The port numbers are divided into three ranges: the Well Known Ports,
the Registered Ports, and the Dynamic and/or Private Ports.
The Well Known Ports are those from 0 through 1023.
DCCP Well Known ports SHOULD NOT be used without IANA registration.
The registration procedure is defined in [RFC4340], Section 19.9.
The Registered Ports are those from 1024 through 49151
DCCP Registered ports SHOULD NOT be used without IANA registration.
The registration procedure is defined in [RFC4340], Section 19.9.
The Dynamic and/or Private Ports are those from 49152 through 65535
[root@sydney etc]# telnet localhost 7
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
[root@sydney etc]# /sbin/chkconfig daytime on
[root@sydney etc]# telnet localhost 15
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
[root@sydney etc]# less services # this let me see what the different services were & find the right number
[root@sydney etc]# telnet localhost 13
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
17 MAR 2004 21:50:15 GMT
Connection closed by foreign host.
[root@sydney etc]# less services
[root@sydney etc]# /sbin/chkconfig echo on
[root@sydney etc]# telnet localhost 7
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hi
hi
golf
golf
^]
import java.net.*;
import java.io.*;
// Contact the daytime server running on hostname.
public class DaytimeClient {
// Contact the server at the appropriate port and set
// the socket attribute for the run() method to use.
public DaytimeClient(String hostname) throws Exception {
// The well-known port of the TCP daytime service.
final int DaytimePort = 13;
try{
socket = new Socket(hostname,DaytimePort);
}
catch(UnknownHostException e){
throw new Exception("Unknown host: "+e.getMessage());
}
catch(IOException e){
throw new Exception("IOException on socket creation: "+e.getMessage());
}
}
// Obtain the time of day from the daytime server.
public void run() {
Socket daytime = getSocket();
try{
// Get the stream used by the server to send data.
InputStream inStream = daytime.getInputStream();
// Wrap the stream in a BufferedReader.
BufferedReader reader = new BufferedReader(
new InputStreamReader(inStream));
// The server will only send a single line.
String response = reader.readLine();
System.out.println(response);
// Close the connection now it is finished with.
daytime.close();
}
catch(IOException e){
System.err.println(e.getMessage());
}
}
protected Socket getSocket(){
return socket;
}
private final Socket socket;
public static void main(String[] args) {
if(args.length == 1){
try{
DaytimeClient client = new DaytimeClient(args[0]);
client.run();
}
catch(Exception e){
System.err.println("Exception: "+e.getMessage());
}
}
else{
System.err.println("You must supply the name of a host to contact.");
}
}
}
/* author david barnes & joanna bryson
*
*/
import java.net.*;
import java.io.*;
// Contact the echo server running on hostname.
class TCPEchoClient {
// Contact the server at the appropriate port and set
// the socket attribute for the run() method to use.
public TCPEchoClient(String hostname) throws Exception {
try{
// The well-known port of the TCP echo service.
final int EchoPort = 7;
socket = new Socket(hostname,EchoPort);
}
catch(UnknownHostException e){
throw new Exception("Unknown host: "+e.getMessage());
}
catch(IOException e){
throw new Exception("IOException on socket creation: "
+e.getMessage());
}
}
protected Socket getSocket(){
return socket;
}
private final Socket socket;
public static void main(String[] args) {
try {
TCPEchoClient eClient = new TCPEchoClient("localhost");
// now you know stdin is really a socket too!
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
String myEchoString;
System.out.print("Welcome to the echo client. The prompt looks like >>."
+ "\n >> ");
// Wrap the input stream in a BufferedReader.
BufferedReader reader = new BufferedReader(
new InputStreamReader(eClient.getSocket().getInputStream()));
// Wrap the output stream in a BufferedWriter.
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(eClient.getSocket().getOutputStream()));
// read a string from stdin & send it to echo
while ((myEchoString=stdin.readLine()) != null) {
System.out.println("Sending: "+myEchoString);
writer.write(myEchoString);
writer.newLine();
// Make sure the data is flushed to the stream.
writer.flush();
// Read the response.
String response = reader.readLine();
System.out.println("The response is: "+response);
System.out.print("\n >> ");
} // while strings
// Close the connection
eClient.getSocket().close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
} // main()
} // class TCPEchoClient
; stick colour in old versions of emacs --
;; Are we running XEmacs or Emacs?
(defvar running-xemacs (string-match "XEmacs\\|Lucid" emacs-version))
;; Turn on font-lock mode for Emacs
(cond ((not running-xemacs)
(global-font-lock-mode t)
))
; -- ( colour stuff from Adam Dziedzic ) --
import java.net.*;
import java.io.*;
// A simple server that accepts a client connection
// and sends back the length of the strings it
// receives from the client.
class LineLengthServer {
public LineLengthServer(int port) throws Exception {
try{
// Listen on the given port.
serverSocket = new ServerSocket(port);
}
catch(BindException e){
throw new Exception("Failed to create a server socket: "+
e.getMessage());
}
}
// Read strings and return their length (as a String)
// to the client.
public void run() throws Exception {
ServerSocket serverSocket = getServerSocket();
Socket clientSocket = null;
try{
System.out.println("Listening for a client on port: "+
serverSocket.getLocalPort());
// Wait for a client to make contact.
clientSocket = serverSocket.accept();
// Contact ...
System.out.println("A client has arrived.");
// Wrap the input stream in a BufferedReader.
BufferedReader reader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
// Wrap the output stream in a BufferedWriter.
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(clientSocket.getOutputStream()));
// Read lines until the client terminates.
String request = reader.readLine();
while(request != null){
// Write the length of the line as a String.
writer.write(String.valueOf(request.length()));
writer.newLine();
writer.flush();
request = reader.readLine();
}
}
catch(IOException e){
throw new Exception("IOException talking to the client: "+
e.getMessage());
}
finally{
if(clientSocket != null){
System.out.println("The client has gone.");
// Close the socket to the client.
clientSocket.close();
}
}
serverSocket.close();
}
protected ServerSocket getServerSocket(){
return serverSocket;
}
// The socket on which the listening is done.
private final ServerSocket serverSocket;
}
// An example of a simple server that reads lines from
// clients and sends back the length of each line
// it receives.
public class LineLengthServerMain {
public static void main(String[] args){
try{
// An arbitrary port number to listen on. This should be
// larger than 1024 for user-written servers.
final int port = 24101;
LineLengthServer server = new LineLengthServer(port);
server.run();
}
catch(Exception e){
System.err.println("Exception: "+e.getMessage());
}
}
}
[joanna@sydney Networks]$ javac LineLengthServerMain.java
[joanna@sydney Networks]$ java LineLengthServer
Exception in thread "main" java.lang.NoSuchMethodError: main
[joanna@sydney Networks]$ java LineLengthServerMain
Listening for a client on port: 24101
^Z
[2]+ Stopped java LineLengthServerMain
[joanna@sydney Networks]$ bg
[2]+ java LineLengthServerMain &
[joanna@sydney Networks]$ telnet localhost 24101
Trying 127.0.0.1...
A client has arrived.
Connected to localhost.
Escape character is '^]'.
hi there
8
have you ever thought
21
this isn't really measuring the line length...
46
Ah ha!
6
^]
telnet> .
?Invalid command
telnet> quit
Connection closed.
The client has gone.
[2]+ Done java LineLengthServerMain
[joanna@sydney Networks]$
public void listen() throws Exception {
ServerSocket serverSocket = getServerSocket();
// Wait up to 30 minutes for new clients.
final int timeToWait = 1*60*1000;
boolean keepWaiting = true;
while(keepWaiting){
try{
// Wait for a client to make contact.
serverSocket.setSoTimeout(timeToWait);
Socket clientSocket = serverSocket.accept();
// Let a separate Thread handle it. QUIZ why are there 2 news?
new Thread(new RandomService(clientSocket)).start();
}
catch(InterruptedIOException e){
// We timed out waiting for a client.
keepWaiting = false;
}
catch(IOException e){
throw new Exception("IOException: "+ e.getMessage());
}
}
// No more clients, so close the socket.
serverSocket.close();
}
The seven layers of the OSI Basic Reference Model are (from bottom to top):
The original Internet protocol specifications defined a four-level model, and protocols designed around it (like TCP) have difficulty fitting neatly into the seven-layer model. Most newer designs use the seven-layer model.
[jjb@jjb op.papers]$ telnet XXXXX 25
Trying 138.38.108.3...
Connected to XXXXXXXXX.ac.uk (138.38.XXX.XXX).
Escape character is '^]'.
220 XXXXXXXXX.ac.uk ESMTP Exim 4.30 Tue, 16 Mar 2004 08:00:12 +0000
HELP
214-Commands supported:
214 AUTH HELO EHLO MAIL RCPT DATA NOOP QUIT RSET HELP
RCPT TO: jjb@cs.bath.ac.uk
503 sender not yet given
MAIL FROM: elvis@graceland.com
250 OK
RCPT TO: jjb@cs.bath.ac.uk
550 RFCs mandate HELO/EHLO before mail may be sent
HELO
501 Syntactically invalid HELO argument(s)
HELP HELO
214-Commands supported:
214 AUTH HELO EHLO MAIL RCPT DATA NOOP QUIT RSET HELP
HELO jjb.cs.bath.ac.uk
250 air.cs.bath.ac.uk Hello jjb.cs.bath.ac.uk [138.38.108.1]
RCPT TO: jjb@cs.bath.ac.uk
503 sender not yet given
MAIL FROM: elvis@kingdom.co
250 OK
RCPT TO: jjb@cs.bath.ac.uk
550-Verification failed for
550-Unrouteable address
550 Sender verify failed
RCPT FROM: joanna@ai.mit.edu
500-unrecognized command
500 Too many syntax or protocol errors
Connection closed by foreign host.
[jjb@jjb op.papers]$ telnet XXXXXX 25
Trying 138.38.108.3...
Connected to XXXXXXXXX.ac.uk (138.38.XXXXXX).
Escape character is '^]'.
220 air.cs.bath.ac.uk ESMTP Exim 4.30 Tue, 16 Mar 2004 08:04:01 +0000
HELO jjb.cs.bath.ac.uk
250 air.cs.bath.ac.uk Hello jjb.cs.bath.ac.uk [138.38.108.1]
RCPT TO: jjb@cs.bath.ac.uk
503 sender not yet given
MAIL FROM: joanna@ai.mit.edu
250 OK
DATA
503 valid RCPT command must precede DATA
RCPT TO: jjb@cs.bath.ac.uk
250 Accepted
DATA
354 Enter message, ending with "." on a line by itself
Hi Dr. Bryson, this is me pretending to be myself at MIT.
.
250 OK id=1B39ZG-0004yz-DU
quit
221 XXXXXXXXXX.ac.uk closing connection
Connection closed by foreign host.
Return-Path:
Received: from air ([unix socket])
by air (Cyrus v2.1.15) with LMTP; Tue, 16 Mar 2004 08:05:15 +0000
X-Sieve: CMU Sieve 2.2
Return-path:
Envelope-to: jjb@cs.bath.ac.uk
Delivery-date: Tue, 16 Mar 2004 08:05:15 +0000
Received: from [138.38.108.1] (helo=jjb.cs.bath.ac.uk)
by air.cs.bath.ac.uk with smtp (Exim 4.30)
id 1B39ZG-0004yz-DU
for jjb@cs.bath.ac.uk; Tue, 16 Mar 2004 08:05:15 +0000
X-Spam-Score: 2.9 (++)
Hi Dr. Bryson, this is me pretending to be myself at MIT.
[joanna@sydney CM10135]$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 localhost.localdomain ESMTP Sendmail 8.12.8/8.12.8; Thu, 18 Mar 2004 14:08:45 GMT
HELO cs.bath.ac.uk
250 localhost.localdomain Hello localhost.localdomain [127.0.0.1], pleased to meet you
/home/ai/joanna % traceroute cs.bath.ac.uk
traceroute to cs.bath.ac.uk (138.38.108.2), 30 hops max, 40 byte packets
1 net-chex (128.52.37.10) 1 ms 1 ms 1 ms
2 anacreon (128.52.0.10) 2 ms 3 ms 1 ms
3 radole (18.24.10.3) 2 ms 76 ms 74 ms
4 B24-RTR-2-LCS.MIT.EDU (18.201.1.1) 70 ms 109 ms 72 ms
5 EXTERNAL-RTR-2-BACKBONE.MIT.EDU (18.168.0.27) 89 ms 105 ms 87 ms
6 MIT-GIGAPOPNE.nox.org (192.5.89.89) 95 ms 84 ms 80 ms
7 192.5.89.10 (192.5.89.10) 91 ms 99 ms 98 ms
8 198.32.11.62 (198.32.11.62) 73 ms 118 ms 115 ms
9 ny.uk1.uk.geant.net (62.40.96.170) 175 ms 109 ms 133 ms
10 janet-gw.uk1.uk.geant.net (62.40.103.150) 129 ms 143 ms 148 ms
11 po3-0.lond-scr3.ja.net (146.97.35.133) 150 ms 134 ms 143 ms
12 po6-0.read-scr.ja.net (146.97.33.13) 173 ms 153 ms 163 ms
13 po2-0.bris-scr.ja.net (146.97.33.49) 141 ms 191 ms 201 ms
14 gi0-1.frenchay-bar.ja.net (146.97.35.82) 191 ms 186 ms *
15 146.97.40.198 (146.97.40.198) 156 ms 167 ms 138 ms
16 bath-1-brisf-1-r1.swern.net.uk (194.82.125.50) 138 ms 158 ms 161 ms
17 bath-gw-1-bath-1.swern.net.uk (194.82.125.198) 143 ms 193 ms *
18 earth.cs.bath.ac.uk (138.38.108.2) 174 ms 153 ms 155 ms
19 earth.cs.bath.ac.uk (138.38.108.2) 166 ms 168 ms 171 ms
20 earth.cs.bath.ac.uk (138.38.108.2) 209 ms 184 ms 184 ms
/home/ai/joanna % traceroute www.hkbu.edu.hk
traceroute to net1.hkbu.edu.hk (158.182.4.1), 30 hops max, 40 byte packets
1 net-chex (128.52.37.10) 1 ms 1 ms 1 ms
2 anacreon (128.52.0.10) 2 ms 1 ms 1 ms
3 radole (18.24.10.3) 2 ms 111 ms 109 ms
4 B24-RTR-2-LCS.MIT.EDU (18.201.1.1) 114 ms 90 ms 118 ms
5 EXTERNAL-RTR-2-BACKBONE.MIT.EDU (18.168.0.27) 118 ms 103 ms 84 ms
6 MIT-GIGAPOPNE.nox.org (192.5.89.89) 91 ms 104 ms 107 ms
7 192.5.89.10 (192.5.89.10) 130 ms 112 ms 126 ms
8 chinng-nycmng.abilene.ucaid.edu (198.32.8.82) 148 ms 162 ms 121 ms
9 iplsng-chinng.abilene.ucaid.edu (198.32.8.77) 123 ms 134 ms 135 ms
10 kscyng-iplsng.abilene.ucaid.edu (198.32.8.81) 146 ms 149 ms 141 ms
11 dnvrng-kscyng.abilene.ucaid.edu (198.32.8.13) 122 ms 150 ms 126 ms
12 snvang-dnvrng.abilene.ucaid.edu (198.32.8.1) 175 ms 137 ms 163 ms
13 losang-snvang.abilene.ucaid.edu (198.32.8.94) 152 ms 153 ms 166 ms
14 tpr2-transpac-la.jp.apan.net (203.181.248.130) 269 ms 276 ms 258 ms
15 taiwan-tpr2.jp.apan.net (203.181.248.153) 290 ms 320 ms 334 ms
16 m160-1-0-0-OC3.tw.ascc.net (140.109.251.42) 308 ms 318 ms 330 ms
17 m20-1-1-0-OC3.hk.ascc.net (140.109.251.45) 360 ms 347 ms 360 ms
18 192.245.196.249 (192.245.196.249) 335 ms 364 ms 383 ms
19 202.40.217.90 (202.40.217.90) 433 ms 457 ms 418 ms
20 202.125.249.5 (202.125.249.5) 385 ms 391 ms 373 ms
21 202.125.249.21 (202.125.249.21) 483 ms 402 ms 453 ms
22 202.125.249.34 (202.125.249.34) 395 ms 382 ms 342 ms
23 158.182.118.73 (158.182.118.73) 478 ms 556 ms 588 ms
24 158.182.118.82 (158.182.118.82) 529 ms 588 ms 620 ms
25 * * *
26 * * *
27 * * *
^C
/home/ai/joanna %
// Print out Internet address information about the local machine.
import java.net.*;
public class PrintInetDetails {
public static void main(String[] args){
if(args.length == 0){
try{
InetAddress myDetails = InetAddress.getLocalHost();
System.out.println("The local host is called: "+
myDetails.getHostName()+
" and its address is: "+
myDetails.getHostAddress());
}
catch(UnknownHostException e){
System.err.println("Unknown host: "+e.getMessage());
}
}
else{
for(int i = 0; i < args.length; i++){
try{
InetAddress details = InetAddress.getByName(args[i]);
System.out.println("The host is called: "+
details.getHostName()+
" and its address is: "+
details.getHostAddress());
}
catch(UnknownHostException e){
System.err.println("Unknown host: "+e.getMessage());
}
}
}
}
}
[jjb@jjb Networks]$ java PrintInetDetails
The local host is called: jjb.cs.bath.ac.uk and its address is: 172.16.33.1
[jjb@jjb Networks]$ java PrintInetDetails horsepower.ai.mit.edu soggy-fibers.ai.mit.edu
The host is called: horsepower.ai.mit.edu and its address is: 128.52.37.26
The host is called: soggy-fibers.ai.mit.edu and its address is: 128.52.32.78
[jjb@jjb Networks]$ java PrintInetDetails wjh.harvard.edu www.google.com
The host is called: wjh.harvard.edu and its address is: 140.247.94.249
The host is called: www.google.com and its address is: 66.102.11.99