Multithread client-server app

Little c/s app with an ugly AWT gui. But it's a nice multithread/socket/awt exercise.

This application simply adds two numbers. The operation itself needs to be performed on the server, which accepts two assemblies and delivers the result to client. The client component consists of two input fields, the startup key and the field to show the result of the operation. The server component needs to receive data from the client, to parse them and perform calculations, and finally deliver the solution to the client. It's done within a single project. 



Main.java

1:  /*  
2:   * Djordje Gavrilovic  
3:   * Mar 23, 2017  
4:   */  
5:  package ajpa1;  
6:    
7:  public class Main {  
8:    public static void main(String[] args) {  
9:      Server s = new Server();  
10:      Klijent k = new Klijent();  
11:        
12:      s.start();  
13:      k.start();  
14:        
15:    }  
16:      
17:  }  

Klijent.java

1:  package ajpa1;  
2:    
3:  import java.io.IOException;  
4:  import java.net.*;  
5:  import java.io.*;  
6:  import java.awt.*;  
7:  import java.awt.event.*;  
8:    
9:  public class Klijent extends Thread{  
10:    static Frame f;  
11:    static Label l1;  
12:    static Label l2;  
13:    static TextField tf1;  
14:    static TextField tf2;  
15:    static TextField tfr;  
16:    static Button b;  
17:    static String rezultat;  
18:      
19:    @Override  
20:    public void run(){  
21:      //GUI  
22:      GridBagLayout lay = new GridBagLayout();  
23:      GridBagConstraints gbc = new GridBagConstraints();  
24:      f = new Frame("Klijent-Server");  
25:      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
26:      Dimension frameSize = f.getSize();  
27:      int x = (screenSize.width - frameSize.width) / 2;  
28:      int y = (screenSize.height - frameSize.height) / 2;  
29:      f.setLocation(x-250, y-200);  
30:      f.setSize(500,400);  
31:      f.setBackground(Color.darkGray);  
32:      f.addWindowListener(new WindowAdapter () {  
33:        public void windowClosing(WindowEvent e) {  
34:          System.exit(0);  
35:        }  
36:      });  
37:      f.setLayout(lay);  
38:      f.setVisible(true);  
39:        
40:      l1 = new Label("Prvi broj:");  
41:      l1.setForeground(Color.white);  
42:      gbc.gridx = 2;  
43:      gbc.gridy = 1;  
44:      f.add(l1,gbc);  
45:        
46:      tf1 = new TextField();  
47:      gbc.fill = GridBagConstraints.HORIZONTAL;  
48:      gbc.gridx = 2;  
49:      gbc.gridy = 2;  
50:      gbc.insets = new Insets(0,0,0,0);  
51:      f.add(tf1,gbc);  
52:        
53:      l2 = new Label("Drugi broj:");  
54:      l2.setForeground(Color.white);  
55:      gbc.gridx = 5;  
56:      gbc.gridy = 1;  
57:      gbc.gridwidth = 1;  
58:      f.add(l2, gbc);  
59:        
60:      tf2 = new TextField();  
61:      gbc.fill = GridBagConstraints.HORIZONTAL;  
62:      gbc.gridx = 5;  
63:      gbc.gridy = 2;  
64:      gbc.insets = new Insets(0,0,0,0);  
65:      f.add(tf2, gbc);  
66:        
67:      tfr = new TextField();  
68:      tfr.setBackground(Color.darkGray);  
69:      tfr.setForeground(Color.yellow);  
70:      gbc.fill = GridBagConstraints.HORIZONTAL;  
71:      gbc.gridx = 2;  
72:      gbc.gridy = 4;  
73:      gbc.gridwidth = 4;  
74:      gbc.insets = new Insets(5,0,0,0);  
75:      f.add(tfr, gbc);  
76:        
77:      b = new Button("Izracunaj");  
78:      b.addActionListener(new ActionListener () {  
79:        @Override  
80:        public void actionPerformed(ActionEvent ae) {  
81:          try( Socket soc = new Socket("localhost",1050);  
82:            BufferedOutputStream bos = new BufferedOutputStream (soc.getOutputStream());) {  
83:          
84:            bos.write((tf1.getText()).getBytes());  
85:            bos.write(("o").getBytes());  
86:            bos.write((tf2.getText()).getBytes());  
87:            bos.flush();  
88:          }  
89:          catch (IOException e){}  
90:        }  
91:      });  
92:      gbc.gridx = 3;  
93:      gbc.gridy = 8;  
94:      gbc.gridwidth = 2;  
95:      gbc.insets = new Insets(20,0,0,0);  
96:      f.add(b, gbc);  
97:        
98:      while(true){  
99:        try ( ServerSocket sS = new ServerSocket(1051);  
100:           Socket soc2 = sS.accept();  
101:           BufferedReader bis = new BufferedReader( new InputStreamReader(soc2.getInputStream()));){  
102:          
103:            tfr.setText(bis.readLine());  
104:        }  
105:        catch (IOException ex) {}  
106:      }  
107:    }    
108:  }  
109:    

Server.java

1:  package ajpa1;  
2:    
3:  import java.io.*;  
4:  import java.net.*;  
5:    
6:  public class Server extends Thread{  
7:    static String rezultat;  
8:      
9:    @Override  
10:    public void run() {  
11:      while (true) {  
12:        try (ServerSocket ser = new ServerSocket(1050);  
13:           Socket soc = ser.accept();  
14:           BufferedReader bis = new BufferedReader( new InputStreamReader (soc.getInputStream()));) {  
15:          
16:             String read = bis.readLine();  
17:            
18:             double broj1 = Double.parseDouble(read.split("o")[0]);  
19:             double broj2 = Double.parseDouble(read.split("o")[1]);  
20:             rezultat = String.valueOf(broj1 + broj2);  
21:        }  
22:        catch (IOException e) {}  
23:        
24:        try (Socket soc1 = new Socket("localhost",1051);  
25:           BufferedOutputStream bos = new BufferedOutputStream (soc1.getOutputStream());) {  
26:            bos.write(rezultat.getBytes());  
27:        }  
28:        catch (IOException e) {}  
29:      }  
30:    }  
31:  }  



Comments

Post a Comment