1 / 17

CIS 5930-04 – Spring 2001

Learn about the basics of sockets in network programming, including their history, implementation, and usage in Java. This course covers important concepts such as TCP/IP, client-server architecture, and socket calls.

joelr
Download Presentation

CIS 5930-04 – Spring 2001

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming CIS 5930-04 – Spring 2001 http://aspen.csit.fsu.edu/it1spring01 Instructors: Geoffrey Fox , Bryan Carpenter Computational Science and Information Technology Florida State University Acknowledgements: Nancy McCracken Syracuse University dbc@npac.syr.edu

  2. Sockets • Sockets first appeared in BSD UNIX (designed byBill Joy—also one of the originators of Java) circa 1982. • They provided a cross-protocol API for networking. The original implementation, for example, provided access to protocols including: • TCP/IP • Xerox NS • Local UNIX inter-process communication. • Today available in Windows, through the WinSock API. • Sockets directly support a client/server architecture. • They support connection-oriented protocols like TCP, as well as connectionless protocols like UDP. • We will only discuss the connection-oriented case. dbc@npac.syr.edu

  3. BSD Socket Calls Network Server Client socket(): create socket socket() : create socket bind() : name socket connect(): listen() : accept(): accept connection write() : send request read() : get request .. . process request . . . write(): send reply read() : get reply dbc@npac.syr.edu

  4. Port Numbers • The bind() call on the server side establishes a well-known address for the listening socket. • In the case of an TCP/IP socket (the only case we are interested in) the important part of this is the port number. • A port number is an integer between 0 and 64K. • On any given host, only one server socket can be listening on a particular port at a particular time. • In UNIX, port numbers below 1024 can only be used by a privileged user (the super-user). Any user can create a server socket listening on higher ports. • Low port numbers are used by standard services, e.g.: • 23 is the default port number for telnet • 80 is the default port number for HTTP servers dbc@npac.syr.edu

  5. Making a Connection • The client makes a connect() call, specifying the remote host IP address, and the port number on that host for the server socket it wants to connect to. • Meanwhile the server is waiting on an accept() call on the server socket. • When the connection is established, the accept() call completes, returning a reference to a new socket. • Data is exchanged through the socket pair consisting of the client socket, and the new socket returned by the accept() call on the server. • The new socket on the server typically lasts for the duration of a single transaction with the client, although the connection may be kept open over multiple transactions. dbc@npac.syr.edu

  6. Sockets in Java • Using sockets from C is traditionally quite hard. The arguments of the BSD socket functions are complex, presumably in part because the historical need to support multiple protocols. • Luckily the API has been greatly simplified in the Java binding for sockets. • The associated classes are in the package java.net. dbc@npac.syr.edu

  7. Java Sockets from the Client Side • A Java program can open a socket connection in one step using a constructor of the Socket class: Socket t = new Socket(hostName, port) ; Here hostName is a string, such as “sirah.csit.fsu.edu”, and port argument is an integer port number, such as 80. • This Socket constructor subsumes the socket() and connect() calls in the BSD API. • The Socket class has methods getInputStream() and getOutputStream(). These return normal Java stream objects that can be used to exchange data over a connected socket pair. • The connection is bi-directional: both client an server can read and write. dbc@npac.syr.edu

  8. Java Sockets from the Server Side • The BSD operations socket(), bind() and listen() for a server-side socket are subsumed in a constructor for the ServerSocket class: ServerSocket s = new ServerSocket(port) ; Here port is the integer port number, such as 80 (if you are writing a Web server), on which the server will listen. • Next the Java server will call the accept() method and wait for clients to connect to it. accept() returns an ordinary socket, completing the socket-pair for the connection: Socket connection = s.accept() ; • After processing the request, the client goes back to waiting on accept(), for new client requests. • Real servers fork a thread or process to deal with the request, and return immediately to waiting for the next client connection. dbc@npac.syr.edu

  9. A Simple Client import java.io.* ; import java.net.* ; public class TrivialBrowser { public static void main(String [] args) { Socket sock = new Socket(“aspen.csit.fsu.edu”, 80) ; PrintWriter out = new PrintWriter( new OutputStreamWriter(sock.getOutputStream()) ; out.println(“GET /it1spring01/index.html HTTP/1.0”) ; out.println(“”) ; out.flush() ; BufferedReader in = new BufferedReader( new InputStreamReader(sock.getInputStream()) ; while(true) String line = in.readLine ; if(line == null) break ; System.out.println(line) ; } } } dbc@npac.syr.edu

  10. Remarks • This implements a (drastically restricted) Web browser. • If you run this program it will print out the HTML source for the IT1 course Home Page. • It connects to port 80 on aspen (the Web server’s port). • It gets an output stream to write to the socket using getOuputStream(). • It sends an HTTP “GET” request on the stream, specifying the file it1spring01/index.htmlrelative to the server’s document root. • It gets an input stream to read from the socket using getInputStream(). • It copies lines from the socket connection to the console. dbc@npac.syr.edu

  11. A Simple Server public static void main(String [] args) throws Exception { ServerSocket server = new ServerSocket(8080) ; while(true) { Socket sock = server.accept() ; BufferedReader in = new BufferedReader( new InputStreamReader(sock.getInputStream()) ; String header = in.readLine() ; . . . skip over any other lines in request packet . . . String fileName = getFileName(header) ; // Second field of header byte [] bytes = readFile(“.” + fileName) ; // Contents of local file DataOutputStream out = new DateOutputStream(sock.getOutputStream()) ; if( . . . ) { // File exists and is a .html file out.writeBytes(“HTTP/1.0 200 OK\r\n”) ; out.writeBytes(“Content-Length: ” + bytes.length + “\r\n”) ; out.writeBytes(“Content-Type: text/html\r\n\r\n”) ; out.write(bytes) ; } } } dbc@npac.syr.edu

  12. Remarks • This implements a (somewhat restricted) Web server. • It creates a server socket listening to port 8080 on the local host. • It gets a socket connection from a client using the accept() method, and then gets the input stream from the socket using getInputStream(). • We handle only “GET” requests; the second field will be the file name. • It reads the file and writes it to the output stream of the socket, in HTTP. • A real server would spawn a new thread to deal with each transaction. The main loop would return immediately to waiting on accept() . dbc@npac.syr.edu

  13. URL Objects • Instead of explicitly opening a socket connection to a Web server, a client can read information using the higher level URL class. • A constructor takes a URL string and creates a URL object: URL url = new URL(“http://aspen.csit.fsu.edu/it1spring01/”) ; • This constructor may throw a MalformedURLException. dbc@npac.syr.edu

  14. Reading a File Using a URL Object • Now if url is a URL object, the resource can be read by opening a stream on the URL: BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() )) ; • This example creates a character stream that can be read like any other. dbc@npac.syr.edu

  15. URL Connection Objects • A class java.net.URLConnection provides additional functionality on URLs. A URLConnection is created by the openConnection() method: URL url = . . . ; URLConnection connection = url.openConnection() ; • Methods on connection allow to return fields from the HTTP header: String getContentType() int getContentLength() . . . • You can also open an InputStream or OutputStream on a URL connection. The latter is used for HTTP “POST” requests. dbc@npac.syr.edu

  16. Firewalls Server Local Area Network The Internet Client Client Firewall Server Server dbc@npac.syr.edu

  17. Operation of Firewalls • The firewall server forwards or blocks IP packets it receives. • Typically it will allow hosts inside the LAN to make arbitrary connect() calls to servers outside the firewall. So it does not prevent users inside from visiting external Web servers, etc. • However it will block external requests to connect() to hosts inside the firewall. • The firewall configuration can be set up to allow connection requests to specific ports on specific hosts to get through—for example to port 80 on the company Web server. dbc@npac.syr.edu

More Related