Unformatted text preview:

Socket programmingSocketsLanguages and PlatformsSocket Programming is EasyDecisionsSocket programming with TCPPseudo code TCP serverPseudo code TCP clientClient/server socket interaction: TCP (Java)Example: Java server (TCP)Example: Java server (TCP), contExample: Java client (TCP)Example: Java client (TCP), cont.Socket programming with UDPPseudo code UDP serverPseudo code UDP clientClient/server socket interaction: UDPExample: Java client (UDP)Example: Java client (UDP), cont.Example: Java server (UDP)Example: Java server (UDP), contServer vs ClientConcurrent ServersBacklogPseudo code concurrent TCP serverReal Internet Traffic AnalysisOn to the transport layer…Transport service requirements of common appsInternet apps: their protocols and transport protocols2: Application Layer 1Socket programmingSocket APIintroduced in BSD4.1 UNIX, 1981Sockets are explicitly created, used, released by applicationsclient/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented a host-local, application-created/owned, OS-controlled interface (a “door”) into whichapplication process can both send and receive messages to/from another (remote or local) application processsocketGoal: learn how to build client/server application that communicate using sockets2: Application Layer 2SocketsSocket: a door between application process and end-end-transport protocol (UCP or TCP)processkernelbuffers,variablessocketcontrolled byapplicationdevelopercontrolled byoperatingsystemhost orserverprocesskernelbuffers,variablessocketcontrolled byapplicationdevelopercontrolled byoperatingsystemhost orserverinternet2: Application Layer 3Languages and PlatformsSocket API is available for many languages on many platforms:C, Java, Perl, Python,… *nix, Windows,…Socket Programs written in any language and running on any platform can communicate with each other!2: Application Layer 4Socket Programming is EasyCreate socket much like you open a fileOnce open, you can read from it and write to itOperating System hides most of the details2: Application Layer 5DecisionsBefore you go to write socket code, decideDo you want a TCP-style reliable, full duplex, connection oriented channel? Or do you want a UDP-style, unreliable, message oriented channel?Will the code you are writing be the client or the server? •Client: you assume that there is a process already running on another machines that you need to connect to.•Server: you will just start up and wait to be contacted2: Application Layer 6Socket programming with TCPClient must contact serverserver process must first be runningserver must have created socket (door) that welcomes client’s contactClient contacts server by:creating client-local TCP socketspecifying IP address, port number of server processWhen client creates socket: client TCP establishes connection to server TCPWhen contacted by client, server TCP creates new socket for server process to communicate with clientFrees up incoming portallows server to talk with multiple clientsTCP provides reliable, in-order transfer of bytes (“pipe”) between client and serverapplication viewpoint2: Application Layer 7Pseudo code TCP serverCreate socket (doorbellSocket)Bind socket to a specific port where clients can contact you Register with the kernel your willingness to listen that on socket for client to contact you (if didn’t bind, listen would choose an ephemeral port)LoopAccept new connection (connectSocket)Read and Write Data Into connectSocket to Communicate with clientClose connectSocketClose doorbellSocket2: Application Layer 8Pseudo code TCP clientCreate socket, connectSocketDo an active connect specifying the IP address and port number of server LoopRead and Write Data Into connectSocket to Communicate with serverClose connectSocket2: Application Layer 9Client/server socket interaction: TCP (Java)wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()create socket,port=x, forincoming request:welcomeSocket = ServerSocket()create socket,connect to hostid, port=xclientSocket = Socket()closeconnectionSocketread reply fromclientSocketcloseclientSocketServer (running on hostid)Clientsend request usingclientSocketread request fromconnectionSocketwrite reply toconnectionSocketTCP connection setup2: Application Layer 10Example: Java server (TCP)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())); Createwelcoming socketat port 6789Wait, on welcomingsocket for contactby clientCreate inputstream, attached to socket2: Application Layer 11Example: Java server (TCP), cont DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } } Read in linefrom socketCreate outputstream, attached to socketWrite out lineto socketEnd of while loop,loop back and wait foranother client connection2: Application Layer 12Example: Java client (TCP)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 streamCreate client socket, connect to serverCreateoutput streamattached to socket2: Application Layer 13Example: Java client (TCP), cont. 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 streamattached to socketSend lineto serverRead linefrom server2: Application


View Full Document

CORNELL CS 5190 - Socket programming

Download Socket programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Socket programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Socket programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?