-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCliente.java
160 lines (139 loc) · 4.49 KB
/
Cliente.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
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import javax.swing.*;
public class Cliente extends JFrame implements ActionListener, KeyListener
{
private static final long serialVersionUID = 1L;
private JTextArea texto;
private JTextField txtMsg;
private JButton btnSend;
private JButton btnSair;
private JLabel lblHistorico;
private JLabel lblMsg;
private JPanel pnlContent;
private Socket socket;
private OutputStream ou ;
private Writer ouw;
private BufferedWriter bfw;
private JTextField txtIP;
private JTextField txtPorta;
private JTextField txtNome;
private String nome;
public Cliente() throws IOException
{
JLabel lblMessage = new JLabel("Entre com seu nome!");
txtNome = new JTextField("");
Object[] texts = {lblMessage,txtNome };
JOptionPane.showMessageDialog(null, texts);
pnlContent = new JPanel();
texto = new JTextArea(10,40);
texto.setEditable(false);
texto.setBackground(new Color(240,240,240));
txtMsg = new JTextField(20);
lblHistorico = new JLabel("Historico");
lblMsg = new JLabel("Mensagem");
btnSend = new JButton("Enviar");
btnSend.setToolTipText("Enviar Mensagem");
btnSend.addActionListener(this);
btnSend.addKeyListener(this);
txtMsg.addKeyListener(this);
JScrollPane scroll = new JScrollPane(texto);
texto.setLineWrap(true);
pnlContent.add(lblHistorico);
pnlContent.add(scroll);
pnlContent.add(lblMsg);
pnlContent.add(txtMsg);
pnlContent.add(btnSend);
pnlContent.setBackground(Color.LIGHT_GRAY);
setTitle(txtNome.getText());
setContentPane(pnlContent);
setLocationRelativeTo(null);
setResizable(false);
setSize(500,320);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//Metodo para realizar a conexão com o servidor
public void conectar() throws IOException
{
this.nome = txtNome.getText();
socket = new Socket("127.0.0.1",12345);
ou = socket.getOutputStream();
ouw = new OutputStreamWriter(ou);
bfw = new BufferedWriter(ouw);
bfw.write(this.nome+"\r\n");
bfw.flush();
}
//Metodo para enviar mensagens ao servidor
public void enviarMensagem(String msg) throws IOException{
bfw.write(this.nome+": "+msg+"\r\n");
bfw.flush();
txtMsg.setText("");
}
//Metodo para escutar mensagens recebidas do servidor
public void escutar() throws IOException{
InputStream in = socket.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader bfr = new BufferedReader(inr);
String msg = "";
while(true){
if(bfr.ready())
{
msg = bfr.readLine();
texto.append(msg+"\r\n");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if(e.getActionCommand().equals(btnSend.getActionCommand()))
{
enviarMensagem(txtMsg.getText());
}
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
try {
enviarMensagem(txtMsg.getText());
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String []args) throws IOException{
Cliente app = new Cliente();
app.conectar();
app.escutar();
}
}