-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientA.java
191 lines (172 loc) · 5.5 KB
/
ClientA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package app;
import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClientA {
JTextField outgoing;
static JTextArea incomming;
JOptionPane errordialog;
DataOutputStream writer;
DataInputStream inputstream;
static BufferedReader br;
static BufferedWriter Wclientd;
Socket soce;
JButton sendbutton;
ByteArrayOutputStream byteArrayOutputStream;
FileWriter fwrite;
BufferedWriter bw;
public void gui() { // GUI SET UP
JFrame frame = new JFrame("BIgBUg Chat Client");
JPanel mainpanel = new JPanel();
outgoing = new JTextField(30);
incomming = new JTextArea(20, 30);
incomming.setLineWrap(true);
incomming.setEditable(true);
JScrollPane Scroll = new JScrollPane(incomming);
Scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
sendbutton = new JButton("Send");
JButton sendimage = new JButton("IMAGE");
JButton recvoice = new JButton("VOICE");
JButton sendpdf = new JButton("PDF");
outgoing.addKeyListener(new KeyButtonListener());
sendimage.addActionListener(new ImageButtonListener());
recvoice.addActionListener(new SendButtonListener());
sendpdf.addActionListener(new SendButtonListener());
sendbutton.addActionListener(new SendButtonListener());
incomming.setBackground(Color.BLUE);
incomming.setForeground(Color.WHITE);
mainpanel.setBackground(Color.ORANGE);
mainpanel.setForeground(Color.WHITE);
mainpanel.add(Scroll);
mainpanel.add(outgoing);
mainpanel.add(sendbutton);
mainpanel.add(sendimage);
mainpanel.add(recvoice);
mainpanel.add(sendpdf);
frame.getContentPane().add(BorderLayout.CENTER, mainpanel);
frame.getContentPane().add(BorderLayout.SOUTH, sendbutton);
frame.setResizable(false);
frame.setSize(400, 440);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setnetwork() // network AND FILE TRANSFERING AND RETRIVING
{
try {
soce = new Socket("127.0.0.1", 4809); // CONNECTING TO SERVER ON IP
// AND PORT ADDRESS
System.out.println("Network Established");
bw = new BufferedWriter(new FileWriter("Client1.txt", true)); // take
// file
Wclientd = new BufferedWriter(new FileWriter("Client1.txt", true));
System.out.println("hello");
writer = new DataOutputStream(soce.getOutputStream()); // send data
// to server
inputstream = new DataInputStream(soce.getInputStream()); // take
// incomming
// data
// from
// server
String sermsgin = inputstream.readUTF(); // conver to string income
// data
Wclientd.write("Server : ");
Wclientd.write(sermsgin); // write data on client.txt file
Wclientd.write("۵");
Wclientd.flush();
incomming.setText(incomming.getText().trim() + "\n Server :" + sermsgin + "\n"); // show
// income
// message
// on
// textarea
} catch (IOException e) {
//
}
}
public class KeyButtonListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { // ON ENTER DATA
// TRANSFERS TO SERER
// AND CLIENT.TXT
try {
String msgsave = outgoing.getText();
bw.write(msgsave); // write data on client.txt file
bw.write("۵");
bw.flush(); // flushes data from ram to file parmetly
System.out.println("Done Client.");
writer.writeUTF(outgoing.getText()); // send input data to
// server
writer.flush(); // flush the input data from ram to server
writer.close(); // closer writer object of datainputstream
incomming.append(msgsave + "\n"); // append the outgoing
// message on text area
// of client
} catch (IOException ex) {
//
}
outgoing.setText("");
outgoing.requestFocus();
}
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}// write and send Text
public class SendButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev) {
try {
writer.writeUTF(outgoing.getText());
writer.flush();
writer.close();
} catch (IOException ex) {
//
}
outgoing.setText("");
outgoing.requestFocus();
}
} // main method
public class ImageButtonListener implements ActionListener {
public void actionPerformed(ActionEvent av) {
/*
* try{ FileOpenService fileOpenService = (FileOpenService)
* ServiceManager.lookup("javax.jnlp.FileOpenService");
* }catch(Exception e){}
*/
}
}
public static void main(String[] args) {
ClientA omar = new ClientA();
omar.gui();
int count = 0;
Boolean ran = true;
try {
br = new BufferedReader(new FileReader("Client1.txt"));
String readclientsenddata = br.readLine();
for (int i = 0; i < readclientsenddata.length(); i++) {
if (readclientsenddata.charAt(i) == '۵') {
incomming.append("\n");
} else
incomming.append(readclientsenddata.charAt(i) + "");
}
} catch (IOException e) {
}
while (ran) {
omar.setnetwork();
count++;
System.out.println(count);
}
}
}