1 / 19

Socket programming with UDP and TCP

Socket programming with UDP and TCP. Socket Programming with TCP. Connection oriented Handshaking procedure Reliable byte-stream. TCP-client in Java. import java.io*; import java.net.*; Class TCPClient { public static void main (String argv[]) throws Exception { String sentence;

travis
Download Presentation

Socket programming with UDP and TCP

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. Socket programming with UDP and TCP

  2. Socket Programming with TCP • Connection oriented • Handshaking procedure • Reliable byte-stream

  3. TCP-client in 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); DataOutpuStream outToServer = new DataOutputStream( clientSocket.getOutputStream()); 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(); } }

  4. TCP-client in Java import java.io*; import java.net.*; • Imports needed packages Class TCPClient { public static void main (String argv[]) throws Exception { • Standard Java initiation

  5. TCP-client in Java String sentence; String modifiedSentence; • Declares two string objects BufferedReader inFromUser = new BufferedReader( new InputStreamReader(system.in)) • Creates a stream that handels input from the user

  6. TCP-client in Java Socket clientSocket = new Socket(”hostname”, 6789) ; • Initiate a TCP-connection with the ”hostname” through port 6789 • Client performes a DNS lookup to obtain host IP. DataOutpuStream outToServer = new DataOutputStream( clientSocket.getOutputStream()) • Creates a stream that handels output to server

  7. TCP-client in Java BufferedReader inFromServer = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); • Creates a stream that handels input from server sentence = inFromUser.readLine() • Puts the input from user into string object

  8. TCP-client in Java outToServer.writeBytes(sentence + ’\n’); • Transform sentence to bytes & sends to server modifiedSentence = inFromServer.readLine(); • Puts input from server into modified sentence System.out.println(”FROM SERVER: ” + modifiedSentence); clientSocket.close(); } } • Prints modifiedSentence and closes the connection

  9. TCP-server in 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 connenctionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream())); DataOutpuStream outToClient = new DataOutputStream( connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + ’\n’; outToClient.writeBytes(capitalizedSentence); } } }

  10. TCP-server in Java ServerSocket welcomeSocket = new ServerSocket (6789); • Creates a welcomeSocket that handels connection-attempts from port 6789 Socket connenctionSocket = welcomeSocket.accept(); • Creates a new socket

  11. TCP-server in Java capitalizedSentence = clientSentence.toUpperCase() + ’\n’; • This command is the essence of the application.

  12. Socket Programming with UDP • Connectionless • No reliable datatransfer

  13. UDP-client in Java import java.io.*; import java.net.*; class UDPClient { public static void main(String args[ ]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName(”hostname”); byte[ ] sendData = new byte[1024]; byte[ ] recieveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes (); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modefiedSentence = new String(receivePacket.getData()); System.out.println(”FROM SERVER:” + modifiedSentence); clientSocket.close(); } }

  14. UDP-client in Java DatagramSocket clientSocket = new DatagramSocket(); • This line does not initiate a TCP connection InetAddress IPAddress = InetAddress.getByName(”hostname”); • Uses DNS lookup to find the IP-address for ”hostname”

  15. UDP-client in Java byte[ ] sendData = new byte[1024]; byte[ ] recieveData = new byte[1024]; • Arrays that contains bytes that will be sent and recieved DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); • Creates a packages that containes travel information

  16. UDP-client in Java clientSocket.send(sendPacket); • Sends the package through the client socket DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); • Creates a placeholder for the package clientSocket.receive(receivePacket); • Rececives the package from the server

  17. UDP-client in Java String modefiedSentence = new String(receivePacket.getData()); • Extracts the data from the package and puts it in a string package clientSocket.close(); • Closes the client socket

  18. UDP-server in Java import java.io.*; import java.net.*; class UDPClient { public static void main(String args[ ]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[ ] receiveData = new byte[1024]; byte[ ] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String catitalizedSentence = sentence.toUpperCase(); sendData = capetalizedSentence.getBytes (); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.send(sendPacket); } } }

  19. UDP-server in Java DatagramSocket serverSocket = new DatagramSocket(9876); • Creates a socket at port 9876 that data passes through String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); • Extracts the data and client information from the package

More Related