Download
application n.
Skip this Video
Loading SlideShow in 5 Seconds..
Application PowerPoint Presentation
Download Presentation
Application

Application

82 Views Download Presentation
Download Presentation

Application

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Application Network programming Using Java

  2. Socket programming socket: door between application process and end-end-transport protocol application application socket controlled by app developer process process transport transport controlled by OS network network link Internet link physical physical Application Layer

  3. Network Program Overview • Reliable traffic (tcp sockets) • Client side (Socket) • Server side (ServerSocket) • Unreliable traffic (UDP ‘socket’s) • Client side (DatagramSocket) • Server side (DatagramSocket)

  4. TCP connection setup create socket, connect to hostid, port=x create socket, port=x, for incoming request: clientSocket = socket() serverSocket = socket() wait for incoming connection request connectionSocket = serverSocket.accept() send request using clientSocket read reply from clientSocket close connectionSocket close clientSocket Reliable traffic (tcp sockets) client server (running onhostid) read request from connectionSocket write reply to connectionSocket

  5. Essential Java programTCP - Client • Socket clientSocket = new Socket(”<<IP-address = host>”, <<portNumber>>) • PrintWriter toServer = new PrintWriter(clientSocket.getOutputStream()) • Scanner fromServer = new Scanner(clientSocket.getInputStream()) • // do work • clientSocket.close()

  6. Essential Java programTCP - Server • ServerSocket serverSocket = new ServerSocket(<<portNumber>>) • LOOP • Socket clientSocket = serverSocket.accept() • PrintWriter toClient = new PrintWriter(clientSocket.getOutputStream()); • Scanner fromClient = new Scanner(clientSocket.getInputStream()); • // do work • clientSocket.close() • END LOOP • serverSocket.close()

  7. TCP – client Python from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM)clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close()

  8. TCP – serverPython from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close()

  9. create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram viaclientSocket read datagram from serverSocket write reply to serverSocket specifying client address, port number read datagram from clientSocket close clientSocket Client/server socket interaction: UDP client server (running on serverIP) create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) Application 2-9

  10. Essential Java programUDP - Client • DatagramSocket clientSocket = new DatagramSocket(); • InetAddress IPaddress = InetAddress.getByName(”<<IP-adr host>>”) • Int port = <<portNumber>> • // send request data • byte[] sendData = <<Some Data As Bytes>> • DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPaddress, port) • clientSocket.send(sendPacket) • // receive response data • byte[] recvBuffer = new byte[1024] • DatagramPacket recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length) • clientSocket.receive(recvPacket) • clientSocket.close()

  11. Essential Java programUDP - Server • DatagramSocket serverSocket = new DatagramSocket (<<portNumber>>) • LOOP • // receive request data • byte[] recvBuffer = new byte[1024] • DatagramPacket recvPacket = new DatagramPacket(recvBuffer, recvBuffer.length) • serverSocket.receive(recvPacket) • // do work • // send response data • byte[] sendData = <<Some Data As Bytes>> • InetAddress IPaddress = recvPacket.getAddress() • Int port = recvPacket.getPort() • DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPaddress, port) • serverSocket.send(sendPacket) • END LOOP • serverSocket.close()

  12. UDP – clientPython from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close()

  13. UDP – serverPython from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress)