Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
11 kB
0
Indexable
Never
 Pastebin
Facebook
priprema op2 safest cryptocurrency exchange

From Stained Earthworm, 1 Day ago, written in Java.
URL https://pastebin.pl/view/524936dc
Embed Show code
Download Paste or View Raw
Hits: 4
      


import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
 
public class App extends JFrame {
 
        JLabel naslov = new JLabel("SRECNO", SwingConstants.CENTER);
        JLabel ime = new JLabel("IME PREZIME BROJ INDEKSA", SwingConstants.CENTER);
        JLabel zahtev = new JLabel("ZAHTEV");
 
        JRadioButton vr = new JRadioButton("Vreme", true);
        JRadioButton po = new JRadioButton("Poruke");
        JLabel tekst = new JLabel("TEKST JE ZA SLANJE, ? JE ZA CITANJE");
        JTextField tf = new JTextField(15);
        JCheckBox el = new JCheckBox("Elvis");
        JCheckBox to = new JCheckBox("Tom");
 
        JButton send = new JButton("DUGME ZA SLANJE ZAHTEVA NA SERVER");
        JLabel odg = new JLabel("ODGOVOR SERVERA", SwingConstants.CENTER);
        JTextArea at = new JTextArea(15,28);
        Font f = new Font("Arial", Font.BOLD, 18);
 
    public App() {
            setTitle("KLIJENT");
            setSize(500,900);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setConponents();
 
            setVisible(true);
        }
        class Nit extends Thread{
            public Nit(){
 
                start();
            }
            public void run() {
                while(true){
                    if(naslov.getText().equals("SRECNO")){
                        naslov.setText("S  R  E  C  N  O");
                    }
                    else{
                        naslov.setText("SRECNO");
                    }
                    try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
 
            }
        }
        public void setConponents() {
            this.setLayout(new GridBagLayout());
 
            GridBagConstraints gbc = new GridBagConstraints();
            int y = 0;
 
 
            gbc.insets = new Insets(10, 0, 10,0);
            gbc.gridx = 0;
            gbc.gridy = y++;
            naslov.setForeground(Color.red);
            naslov.setFont(new Font("Arial", Font.BOLD, 30));
            getContentPane().add(naslov,gbc);
 
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            ime.setFont(f);
            ime.setForeground(Color.yellow);
            ime.setBackground(Color.blue);
            ime.setOpaque(true);
            ime.setPreferredSize(new Dimension(480,30));
            getContentPane().add(ime,gbc);
 
            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.gridx = 0;
            gbc.gridy = y++;
            zahtev.setFont(f);
            zahtev.setForeground(Color.blue);
            zahtev.setBackground(Color.green);
            zahtev.setOpaque(true);
            getContentPane().add(zahtev,gbc);
 
 
            ButtonGroup bg = new ButtonGroup();
 
            bg.add(vr);
            bg.add(po);
 
            gbc.insets = new Insets(0, 0, 0,0);
            gbc.gridx = 0;
            gbc.gridy = y++;
            vr.setFont(f);
            getContentPane().add(vr,gbc);
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            po.setFont(f);
            getContentPane().add(po,gbc);
 
 
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.insets = new Insets(10, 0, 10,0);
            gbc.gridx = 0;
            gbc.gridy = y++;
            tekst.setFont(f);
            getContentPane().add(tekst,gbc);
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            tf.setFont(f);
            getContentPane().add(tf,gbc);
 
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.insets = new Insets(0, 0, 0,0);
            gbc.gridx = 0;
            gbc.gridy = y++;
            el.setFont(f);
            getContentPane().add(el,gbc);
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            to.setFont(f);
            getContentPane().add(to,gbc);
 
            gbc.insets = new Insets(10, 0, 10,0);
            gbc.gridx = 0;
            gbc.gridy = y++;
            send.setFont(f);
            send.setBackground(Color.red);
            send.setForeground(Color.yellow);
            send.setPreferredSize(new Dimension(480,50));
            getContentPane().add(send,gbc);
 
 
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            odg.setForeground(Color.blue);
            odg.setBackground(Color.yellow);
            odg.setOpaque(true);
            odg.setPreferredSize(new Dimension(480,30));
            odg.setFont(f);
            getContentPane().add(odg,gbc);
 
            gbc.gridx = 0;
            gbc.gridy = y++;
            at.setFont(f);
            getContentPane().add(at,gbc);
 
            send.addActionListener(new ActionListener() {
 
                public void actionPerformed(ActionEvent e) {
                    String forServer = "";
                    if(vr.isSelected()){
                        forServer = ":VREME";
                    }
                    else{
                        forServer = ":PORUKE:";
                        forServer += tf.getText();
                        forServer += ":";
 
 
                        if(el.isSelected()){
                            forServer += "Elvis,";
                        }
                        if(to.isSelected()){
                            forServer += "Tom,";
                        }
                    }
 
                    try{
                        String addr = "127.0.0.1";
                        int port = 9000;
                        Socket sock = new Socket(addr, port);
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                        sock.getInputStream()));
                        PrintWriter out = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                sock.getOutputStream())),true);
 
 
                        out.println(forServer);
                        String odg = in.readLine();
                        at.append(odg + "\n");
 
 
                        out.close();
                        in.close();
                        sock.close();
 
 
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
 
                }
            });
 
 
            new Nit();
 
        }
 
        public static void main(String[] args) {
            new App();
 
        }
    }
 
 
 
 
///////////////////////////////////////////////////////////////////////////////
import javax.swing.*;
import java.awt.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends JFrame {
 
    public static String zaElvis = new String();
    public static String zaToma  = new String();
    public static JTextArea tfs = new JTextArea();
    public Server(){
        setTitle("SERVER");
        setSize(500,900);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        getContentPane().add(tfs);
        tfs.setFont(new Font("Arial", Font.BOLD, 18));
        tfs.append("Server je pokrenut\n");
        setVisible(true);
    }
    public static void main(String[] args)
    {
        new Server();
        try{
            ServerSocket ss = new ServerSocket(9000);
 
            while(true){
                Socket s = ss.accept();
                ServerThread st = new ServerThread(s);
            }
 
        }catch (Exception e){
            e.printStackTrace();
        }
 
    }
}
/////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class ServerThread extends Thread{
    private BufferedReader in;
    private PrintWriter out;
    private Socket sock;
 
    public ServerThread(Socket sock){
        this.sock = sock;
 
        try {
            in = new BufferedReader( // inicijalizuj ulazni stream
                    new InputStreamReader(
                            sock.getInputStream()));
            out = new PrintWriter( // inicijalizuj izlazni stream
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    sock.getOutputStream())), true);
        } catch (Exception ex) { ex.printStackTrace(); }
        start();
 
    }
    public void run() {
        String request = null;
        try {
            request = in.readLine();
            System.out.println(request);
            String odServera = "";
 
 
            if(request.equals(":VREME")){
                Server.tfs.append("Klijentov zahtev\n");
                Server.tfs.append(request+ "\n");
                odServera += "VREME ";
                SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                odServera += formatter.format(date);
 
            }
            else{
                Server.tfs.append("Klijentov zahtev\n");
                Server.tfs.append(request+ "\n");
                String[] poruke = request.split(":", 5);
                String[] imena = poruke[3].split(",", 3);
                if(poruke[2].equals("?")){
                    if(imena[0].equals("Elvis")){
                        odServera += "PORUKA JE BILA ";
                        odServera += Server.zaElvis;
                    }
                    if(imena[1].equals("Tom")){
                        odServera += "PORUKA JE BILA ";
                        odServera += Server.zaToma;
                    }
                    if(imena[0].equals("Tom")){
                        odServera += "PORUKA JE BILA ";
                        odServera += Server.zaToma;
                    }
                }
                else{
                    odServera = "PORUKA JE PREDATA";
                    if(imena[1].equals("Tom")){
                        Server.zaToma = poruke[2];
                    }
                    if(imena[0].equals("Tom")){
                        Server.zaToma = poruke[2];
                    }
                    if(imena[0].equals("Elvis")){
                        Server.zaElvis = poruke[2];
                    }
                }
            }
            Server.tfs.append("[odgovor]"+odServera + "\n");
            out.println(odServera);
            sock.close();
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
}