Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class ServerCodeBar {
- private static DataInputStream dataInputStream = null;
- public static void main(String[] args) {
- if (args.length < 2) {
- System.out.println(
- "Provide hostname and port"
- );
- return;
- }
- try {
- InetAddress addr = InetAddress.getByName(args[0]);
- int port = Integer.parseInt(args[1]);
- Socket socket = new Socket(addr, port);
- DataOutputStream dos = new DataOutputStream(
- socket.getOutputStream());
- InputStreamReader in = new InputStreamReader(
- socket.getInputStream());
- PrintWriter pw = new PrintWriter(dos, true);
- System.out.println("Podaj kod kreskowy");
- int n = Integer.parseInt(System.console().readLine());
- dos.writeInt(n);
- pw.flush();
- BufferedReader br = new BufferedReader(in);
- char[] buffer = new char[2048];
- int count = br.read(buffer, 0, 2048);
- long mr;
- mr = br.read(buffer, 0, 2048);
- long ty = br.read(buffer, 0, 2048);
- System.out.println(mr+ty);
- if(n==99){
- int bytes = 2048;
- dataInputStream = new DataInputStream(socket.getInputStream());
- FileOutputStream fileOutputStream = new FileOutputStream("10MB.pdf");
- long size = 10615705;
- byte[] buffor = new byte[2*2048];
- while (size > 10 && (bytes = dataInputStream.read(buffor, 0, (int)Math.min(buffer.length, size))) != -1){
- fileOutputStream.write(buffor,0,bytes);
- size -= bytes;
- }
- fileOutputStream.close();
- System.out.println("Zapisano plik na dysku");
- }else{
- String reply = new String(buffer, 0, count);
- System.out.println("Informacje na temat produktu:");
- System.out.println(reply);
- }
- dos.close();
- socket.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("Client have disconnected");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement