Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TCP - Cliente.java, Willian Luigi (c) CODE.ME
- //Tutorial de conexão TCP: http://codeme.forumeiros.com/t40-conexao-tcpservidor-cliente#245
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- class Usuario extends JFrame {
- JButton conectar = new JButton("Enviar");
- JTextField txbMsg = new JTextField();
- JLabel lbl = new JLabel("");
- public Usuario() {
- super ("Setting up a connection, (C)CODE.ME");
- conectar.setBounds(240, 30, 70, 30);
- txbMsg.setBounds(10, 30, 200, 30);
- lbl.setBounds(15, 60, 200, 15);
- this.getContentPane().setLayout(null);
- this.getContentPane().add(conectar);
- this.getContentPane().add(txbMsg);
- this.getContentPane().add(lbl);
- try {
- eventoBtn(conectar);
- } catch (Exception e) {
- }
- setSize(340, 120);
- setVisible(true);
- setIconImage(new ImageIcon("fav.png").getImage());
- }
- public void eventoBtn(JButton v) {
- ActionListener click = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- lbl.setText(conectar(txbMsg.getText()));
- } catch (Exception o) {
- }
- }
- };
- v.addActionListener(click);
- }
- public static void main(String[] args) {
- new Usuario();
- }
- public String conectar(String msg) throws Exception {
- String resposta;
- Socket client = new Socket("127.0.0.1", 7777);
- DataOutputStream toSv = new DataOutputStream(client.getOutputStream());
- BufferedReader bufferSv = new BufferedReader(new InputStreamReader(client.getInputStream()));
- toSv.writeBytes(msg + '\n');
- resposta = bufferSv.readLine();
- System.out.println("Servidor: " + resposta);
- client.close();
- return "Servidor: " + resposta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment