Presentation is loading. Please wait.

Presentation is loading. Please wait.

Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol.

Similar presentations


Presentation on theme: "Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol."— Presentation transcript:

1 Application Network programming Using Java

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

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 Reliable traffic (tcp sockets) wait for incoming connection request connectionSocket = serverSocket.accept() create socket, port= x, for incoming request: serverSocket = socket() create socket, connect to hostid, port= x clientSocket = socket() server (running on hostid ) client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup close connectionSocket read reply from clientSocket close clientSocket

5 Essential Java program TCP - Client Socket clientSocket = new Socket(” ”, >) PrintWriter toServer = new PrintWriter(clientSocket.getOutputStream()) Scanner fromServer = new Scanner(clientSocket.getInputStream()) // do work clientSocket.close()

6 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()

7 Server programming

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

9 Essential Java program TCP - Server ServerSocket serverSocket = new ServerSocket( >) 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()

10 TCP – server Python 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()

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

12 Essential Java program UDP - Client DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPaddress = InetAddress.getByName(” >”) Int port = > // send request data byte[] sendData = > 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()

13 Essential Java program UDP - Server DatagramSocket serverSocket = new DatagramSocket ( >) 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 = > InetAddress IPaddress = recvPacket.getAddress() Int port = recvPacket.getPort() DatagramPacket sendPacket = new DatagramPacket( sendData, sendData.length, IPaddress, port) – serverSocket.send(sendPacket) END LOOP serverSocket.close()

14 UDP – client Python 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()

15 UDP – server Python 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)


Download ppt "Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol."

Similar presentations


Ads by Google