Download presentation
Presentation is loading. Please wait.
1
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson
2
CIS 235: Networks Fall, 2007 Western State College Sockets Most 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
3
CIS 235: Networks Fall, 2007 Western State College Networks and the OS You 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.
4
CIS 235: Networks Fall, 2007 Western State College TCP Sockets TCP 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.
5
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?
6
CIS 235: Networks Fall, 2007 Western State College Port Numbers Messages arriving at your computer get there because they are directed to your IP address But 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.
7
CIS 235: Networks Fall, 2007 Western State College Static Port Numbers A 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
8
CIS 235: Networks Fall, 2007 Western State College Dynamic Port Numbers When 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.
9
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.
10
CIS 235: Networks Fall, 2007 Western State College Example: Socket Programming The following code demonstrates a TCP client / Server. These programs use port 6789 to find the server The 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!
11
CIS 235: Networks Fall, 2007 Western State College TCP Client/Server Interaction wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup
12
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()); Create input stream Create client socket, connect to server Create output stream attached to socket
13
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(); } Create input stream attached to socket Send line to server Read line from server
14
CIS 235: Networks Fall, 2007 Western State College TCPServer.java import 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())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket
15
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 line from socket Create output stream, attached to socket Write out line to socket End of while loop, loop back and wait for another client connection
16
CIS 235: Networks Fall, 2007 Western State College TCP Sockets TCP 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.
17
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?
18
CIS 235: Networks Fall, 2007 Western State College A TCP Client 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());
19
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(); } }
20
CIS 235: Networks Fall, 2007 Western State College The Hostname We’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.
21
CIS 235: Networks Fall, 2007 Western State College A TCP Server import 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();
22
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); }}}
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.