Buonasera,
mi sto imbattendo nello sviluppo di una semplice chat (uno a uno) in java. Mi serve perchè devo sviluppare un progetto universitario. Ho scritto tutto il codice, ma le due interfacce (che hanno una textarea, una textfield ed un button) non si parlano
. Se qualcuno riuscisse a darmi qualche suggerimento ne sarei grata. Copio il codice qui sotto.
Spero nei vostri suggerimenti.
Vi ringrazio anticipatamente. Buon Pomeriggio a tutti
mi sto imbattendo nello sviluppo di una semplice chat (uno a uno) in java. Mi serve perchè devo sviluppare un progetto universitario. Ho scritto tutto il codice, ma le due interfacce (che hanno una textarea, una textfield ed un button) non si parlano

Codice:
Classe per il server:
import java.awt.EventQueue;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class chat_server {
public JFrame frmServer;
public static JTextField msg_text = new JTextField();;
public static JTextArea msg_area = new JTextArea();
public static JButton msg_send = new JButton("Send");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chat_server window = new chat_server();
window.frmServer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String msgin="";
try {
ss = new ServerSocket(6789); //diamo la porta del server
s= ss.accept();//mi metto in ascolto sulla porta che ho aperto
ss.close();//per fare una connessione uno a uno
din= new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
while(!msgin.equals("exit")) {
msgin=din.readUTF();
msg_area.append("\n"+msgin);;// visualizziamo il messaggio dal client
}
}catch(Exception e) {
e.printStackTrace();
}
}
static ServerSocket ss;
static Socket s;
static DataInputStream din;
static DataOutputStream dout;
/**
* Create the application.
*/
public chat_server() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmServer = new JFrame();
frmServer.setTitle("Server");
frmServer.setBounds(100, 100, 450, 300);
frmServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmServer.getContentPane().setLayout(null);
JTextArea msg_area = new JTextArea();
msg_area.setBounds(12, 12, 400, 150);
frmServer.getContentPane().add(msg_area);
msg_text = new JTextField();
msg_text.setBounds(12, 188, 290, 35);
frmServer.getContentPane().add(msg_text);
msg_text.setColumns(10);
JButton msg_send = new JButton("Send");
msg_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String msgout="";
try {
msgout=msg_text.getText().trim();
dout.writeUTF("Server: "+msgout);//mandiamo il messaggio del server al client
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
msg_send.setFont(new Font("Arial", Font.BOLD, 14));
msg_send.setBounds(325, 188, 95, 35);
frmServer.getContentPane().add(msg_send);
}
}
Classe per il Client:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class chat_client {
public JFrame frmClient;
public static JTextField msg_text = new JTextField();;
public static JTextArea msg_area = new JTextArea();
public static JButton msg_send = new JButton("Send");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chat_client window = new chat_client();
window.frmClient.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String msgin="";
try {
s=new Socket(InetAddress.getLocalHost(),6789);// indirizzo ip locale perche lanciamo client e server dalla stessa macchina
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
while (!msgin.equals("exit")) {
msgin=din.readUTF();
msg_area.append("\n"+msgin);;
}
}catch(Exception e) {
e.printStackTrace();
}
}
static Socket s;
static DataInputStream din;
static DataOutputStream dout;
/**
* Create the application.
*/
public chat_client() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmClient = new JFrame();
frmClient.setTitle("Client");
frmClient.setBounds(100, 100, 450, 300);
frmClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmClient.getContentPane().setLayout(null);
msg_area.setBounds(12, 12, 400, 150);
frmClient.getContentPane().add(msg_area);
msg_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msgout="";
try {
msgout=msg_text.getText().trim();
dout.writeUTF("Client: "+msgout);//mandiamo il messaggio del server al client
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
msg_send.setFont(new Font("Arial", Font.BOLD, 14));
msg_send.setBounds(325, 188, 95, 35);
frmClient.getContentPane().add(msg_send);
msg_text.setBounds(12, 188, 290, 35);
frmClient.getContentPane().add(msg_text);
msg_text.setColumns(10);
}
}
Vi ringrazio anticipatamente. Buon Pomeriggio a tutti

Ultima modifica di un moderatore: