Download presentation
Presentation is loading. Please wait.
Published byMaryann Rogers Modified over 9 years ago
1
Performance measurements for inter-process communication
2
Files Write a text file import java.io.BufferedWriter; import java.io.FileReader; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; private void WriteFile(String message) { File file = new File("filename.txt"); try { if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(message); bw.close(); } catch (java.io.IOException exc) { } Read from a text file private String ReadFile() { BufferedReader br = null; String sCurrentLine = ""; try { br = new BufferedReader(new FileReader("filename.txt")); sCurrentLine = br.readLine(); } catch (java.io.IOException exc) { } return sCurrentLine; }
3
Sockets (sender) Define the socket: senderSocket = new Socket("127.0.0.1", 9999); os = new DataOutputStream(senderSocket.getOutputStream()); is = new BufferedReader(new InputStreamReader(senderSocket.getInputStream())); Write: os.writeBytes(codedMessage + "\n"); Read from socket: while (true){ ack = is.readLine(); if (ack != null){ if (ack.compareTo("a") == 0){ break; }
4
Sockets (receiver) Define the socket: receiverSocket = new ServerSocket(9999); senderSocket = receiverSocket.accept(); System.out.println("Sender is connected."); // create input and output streams to communicate with the sender is = new BufferedReader(new InputStreamReader(senderSocket.getInputStream())); os = new DataOutputStream(senderSocket.getOutputStream()); Read from socket: try { while(true){ codedMessage = is.readLine(); if (codedMessage != null) break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Write to socket: try { os.writeBytes("a\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.