Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.

22
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.

Welcome to CIS 235

Computer Networks

Fall, 2007

Prof Peterson

CIS 235: Networks Fall, 2007 Western State College

SocketsMost low level network programming is done

using an abstraction called “sockets”. There are lots of socket libraries that we can use to build applications that exploit network connections.

Sockets differ from files in that you have to go through a connection process and the connection may go away.

Sockets differ from streams in that there can be a loss of connection.

We can do either UDP or TCP over sockets – this will depend on the application

CIS 235: Networks Fall, 2007 Western State College

Networks and the OSYou won’t get to program at the hardware level

when you write network code – the operating system (Windows / Linux) wants to control the actual network devices.

The OS will handle the TCP/UDP/IP protocol for you – you’ll never see this level of the system except for a few “tuning parameters” that are available to you.

CIS 235: Networks Fall, 2007 Western State College

TCP SocketsTCP is a connection oriented service – a

program will have to set up / tear down the connection.

A socket is similar to any other data source – data arrives whenever it feels like it (as with terminal input) and the program needs to respond to it.

CIS 235: Networks Fall, 2007 Western State College

TCP Sockets * Client creates a socket to build a connection * Using the socket, the client establishes a

connection with the server * Ultimately we get a connection socket that

provides reliable byte stream service between client and server

* Stream: a flow of characters between two program components.

How have we seen streams implemented?

CIS 235: Networks Fall, 2007 Western State College

Port NumbersMessages arriving at your computer get there

because they are directed to your IP addressBut how do we know which application a

message is associated with?Port numbers are used to identify a specific

software object on a computer that is associated with a particular piece of traffic.

CIS 235: Networks Fall, 2007 Western State College

Static Port NumbersA server needs to be at a known port number.Many ports are pre-allocated to specific services

(25 = SMTP, 80 = www)If a packet arrives at a machine unsolicited it

has to be at a known port

CIS 235: Networks Fall, 2007 Western State College

Dynamic Port NumbersWhen you initiate a connection with another

machine, you need to allocate a port number for the reply message.

Your OS keeps a pool of unused port numbers to use as “reply-to” addresses for connections established to other machines.

You don’t care which port is used – only that no other application can use that port while you are.

CIS 235: Networks Fall, 2007 Western State College

The Mail Room

The operating system is responsible for “sorting the mail”.

That is, incoming messages are sorted by port number to associate them with the proper application.

It is essential for the OS to know about any static port associations and to manage dynamic ports without running out of port numbers.

The number of ports (65K?) limits the number of networked applications you can run at once.

CIS 235: Networks Fall, 2007 Western State College

Example: Socket ProgrammingThe following code demonstrates a TCP client /

Server.These programs use port 6789 to find the

serverThe client sends a string to the server and then

receives a reply consisting of the same string in all caps

Please get on a computer and start netbeans. I’d like 3 servers and 6 clients – copy the appropriate project out of my shared folder into your S folder.

We’ll talk code while it gets going!

CIS 235: Networks Fall, 2007 Western State College

TCP Client/Server Interaction

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

closeconnectionSocket

read reply fromclientSocket

closeclientSocket

Server (running on hostid) Client

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

CIS 235: Networks Fall, 2007 Western State College

TCPClient.java

import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

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

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

Createinput stream

Create client socket,

connect to server

Createoutput stream

attached to socket

CIS 235: Networks Fall, 2007 Western State College

TCPClient.java

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

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

Createinput stream

attached to socket

Send lineto server

Read linefrom server

CIS 235: Networks Fall, 2007 Western State College

TCPServer.javaimport java.io.*; import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

Createwelcoming socket

at port 6789

Wait, on welcomingsocket for contact

by client

Create inputstream, attached

to socket

CIS 235: Networks Fall, 2007 Western State College

TCPServer.java

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } } }

Read in linefrom socket

Create outputstream,

attached to socket

Write out lineto socket

End of while loop,loop back and wait foranother client connection

CIS 235: Networks Fall, 2007 Western State College

TCP SocketsTCP is a connection oriented service – a

program will have to set up / tear down the connection.

A socket is similar to any other data source – data arrives whenever it feels like it (as with terminal input) and the program needs to respond to it.

CIS 235: Networks Fall, 2007 Western State College

TCP Sockets * Client creates a socket to establish a

connection with the server * Using the socket, the client establishes a

connection with the server * Ultimately we get a connection socket that

provides reliable byte stream service between client and server

* Stream: a flow of characters between two program components.

How have we seen streams implemented?

CIS 235: Networks Fall, 2007 Western State College

A TCP Clientimport java.io.*;import java.net.*; class TCPClient {     public static void main(String argv[]) throws

Exception     { String sentence; String modifiedSentence;

        BufferedReader inFromUser =           new BufferedReader(

new InputStreamReader(System.in));         Socket clientSocket = new Socket("hostname",

6789);         DataOutputStream outToServer =

          new DataOutputStream( clientSocket.getOutputStream());

CIS 235: Networks Fall, 2007 Western State College

A TCP Client      BufferedReader inFromServer =

          new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));        sentence = inFromUser.readLine();        outToServer.writeBytes(sentence + '\n');        modifiedSentence =

inFromServer.readLine();        System.out.println( "FROM SERVER: " + modifiedSentence);         clientSocket.close();

                       } }

CIS 235: Networks Fall, 2007 Western State College

The HostnameWe’ll avoid DNS issues and use a raw IP

address.Use the ipconfig utility if you are a server so

you can tell others what your IP address is.

CIS 235: Networks Fall, 2007 Western State College

A TCP Serverimport java.io.*; import java.net.*; class TCPServer {   public static void main(String argv[])

throws Exception     {String clientSentence;      String capitalizedSentence;

      ServerSocket welcomeSocket = new ServerSocket(6789);

  while(true) {   Socket connectionSocket =

welcomeSocket.accept();

CIS 235: Networks Fall, 2007 Western State College

A TCP Server           BufferedReader inFromClient =

           new BufferedReader( new InputStreamReader( connectionSocket.getInputStream()));            DataOutputStream  outToClient =

             new DataOutputStream( connectionSocket.getOutputStream());            clientSentence = inFromClient.readLine();            capitalizedSentence = clientSentence.toUpperCase() + '\n';           

outToClient.writeBytes(capitalizedSentence);         }}}