Reputation: 2604
i have a main class with a method that starts a connection with server. in the JFrame class i have a button signup that when pressed it will retrieve the user input and sends it to the server. my problem is that the connection to the server is not working because of a mistake with passing the variables from the main method. i tried declaring in the JFrame class the variables for the connection and assigning them in the constructor to the main class variables but this also did not work.
main class:
public class TCPClient {
Socket clientSocket;
DataOutputStream outToServer;
BufferedReader inFromServer;
String test;
public void start() throws Exception{
clientSocket= new Socket("localhost", 6789);
outToServer= new DataOutputStream(clientSocket.getOutputStream());
inFromServer= new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
test="abc";
}
public static void main(String[] args) throws Exception {
TCPClient c=new TCPClient ();
c.start();
NewJFrame j=new NewJFrame();
j.setVisible(true);
}}
JFrame class:
public class NewJFrame extends javax.swing.JFrame {
JTextField username = new JTextField();
JTextField name = new JTextField();
JTextField Email = new JTextField();
JTextField Address = new JTextField();
JTextField password = new JPasswordField();
BufferedReader inFromUser;
Socket clientSocket;
DataOutputStream outToServer;
BufferedReader inFromServer;
String test;
public NewJFrame() throws Exception {
TCPClient c=new TCPClient();
c.start();
inFromServer=c.inFromServer;
clientSocket=c.clientSocket;
outToServer=c.outToServer;
inFromServer=c.inFromServer;
test=c.test;
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.print(test);
Object[] message = {
"Full Name:", name,
"Email:", Email,
"Address:", Address,
"Username:", username,
"Password:", password
};
int option = JOptionPane.showConfirmDialog(null, message,"Sign in",JOptionPane.OK_CANCEL_OPTION);
try{
System.out.print(test);
outToServer.writeBytes("signup"+ '\n');
outToServer.writeBytes(name.getText() + '\n');
outToServer.writeBytes(Email.getText() + '\n');
outToServer.writeBytes(Address.getText() + '\n');
outToServer.writeBytes(username.getText() + '\n');
outToServer.writeBytes(password.getText() + '\n');
JOptionPane.showMessageDialog(null, inFromServer.readLine());
clientSocket.close();
}
catch(Exception e)
{
}
Upvotes: 0
Views: 380
Reputation: 23629
Since you close the socket connection in your actionPerformed
method, I would suggest doing the opening of the connection there as well.
Reasoning:
Upvotes: 1
Reputation: 692023
The answer is in the question:
a mistake with passing the variables from the main method
Your main method doesn't pass anything to the JFrame constructor. Instead, the constructor creates a new TCP client and starts it again. The constructor should be defined as
public NewJFrame(TCPClient tcpClient) {
...
And the main method should pass the TCPClient, that it has already created and started, to the constructor.
Upvotes: 1