Advertisement
Guest User

Untitled

a guest
Jan 5th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4.  
  5.  
  6. public class ServerCodeBar {
  7. private static DataInputStream dataInputStream = null;
  8. public static void main(String[] args) {
  9. if (args.length < 2) {
  10. System.out.println(
  11. "Provide hostname and port"
  12. );
  13. return;
  14. }
  15. try {
  16. InetAddress addr = InetAddress.getByName(args[0]);
  17. int port = Integer.parseInt(args[1]);
  18. Socket socket = new Socket(addr, port);
  19.  
  20. DataOutputStream dos = new DataOutputStream(
  21. socket.getOutputStream());
  22. InputStreamReader in = new InputStreamReader(
  23. socket.getInputStream());
  24.  
  25. PrintWriter pw = new PrintWriter(dos, true);
  26. System.out.println("Podaj kod kreskowy");
  27. int n = Integer.parseInt(System.console().readLine());
  28. dos.writeInt(n);
  29. pw.flush();
  30. BufferedReader br = new BufferedReader(in);
  31. char[] buffer = new char[2048];
  32. int count = br.read(buffer, 0, 2048);
  33. long mr;
  34. mr = br.read(buffer, 0, 2048);
  35. long ty = br.read(buffer, 0, 2048);
  36. System.out.println(mr+ty);
  37. if(n==99){
  38. int bytes = 2048;
  39. dataInputStream = new DataInputStream(socket.getInputStream());
  40. FileOutputStream fileOutputStream = new FileOutputStream("10MB.pdf");
  41. long size = 10615705;
  42. byte[] buffor = new byte[2*2048];
  43. while (size > 10 && (bytes = dataInputStream.read(buffor, 0, (int)Math.min(buffer.length, size))) != -1){
  44. fileOutputStream.write(buffor,0,bytes);
  45. size -= bytes;
  46. }
  47.  
  48. fileOutputStream.close();
  49. System.out.println("Zapisano plik na dysku");
  50. }else{
  51. String reply = new String(buffer, 0, count);
  52. System.out.println("Informacje na temat produktu:");
  53. System.out.println(reply);
  54. }
  55. dos.close();
  56. socket.close();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. System.out.println("Client have disconnected");
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement