GUI client - server in JAVA
18,367
Here you have the whole thing:
For client:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientSocketFrame extends JFrame implements ActionListener {
JLabel lblName;
JLabel lblAge;
JLabel lblMark;
JTextField txtName;
JTextField txtAge;
JTextField txtMark;
JButton btnProcess;
JTextArea txtS;
public ClientSocketFrame() {
this.setTitle("Simple Sample");
this.setSize(320, 240);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
lblName = new JLabel("Name: ");
lblName.setBounds(10, 10, 90, 21);
add(lblName);
txtName = new JTextField();
txtName.setBounds(105, 10, 90, 21);
add(txtName);
lblAge = new JLabel("Age: ");
lblAge.setBounds(10, 35, 90, 21);
add(lblAge);
txtAge = new JTextField();
txtAge.setBounds(105, 35, 90, 21);
add(txtAge);
lblMark = new JLabel("Mark: ");
lblMark.setBounds(10, 60, 90, 21);
add(lblMark);
txtMark = new JTextField();
txtMark.setBounds(105, 60, 90, 21);
add(txtMark);
btnProcess = new JButton("Process");
btnProcess.setBounds(200, 40, 90, 21);
btnProcess.addActionListener(this);
add(btnProcess);
txtS = new JTextArea();
txtS.setBounds(10, 85, 290, 120);
add(txtS);
this.setVisible(true);
}
public static void main(String[] args) {
new ClientSocketFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnProcess)) {
try {
processInformation();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void processInformation() throws UnknownHostException, IOException {
Socket s = new Socket("localhost", 5000);
ObjectOutputStream p = new ObjectOutputStream(s.getOutputStream());
String name = txtName.getText();
int mark = Integer.parseInt(txtMark.getText());
int age = Integer.parseInt(txtAge.getText());
p.writeObject(new Student(name, age, mark));
p.flush();
// Here we read the details from server
BufferedReader response = new BufferedReader(new InputStreamReader(
s.getInputStream()));
txtS.setText("The server respond: " + response.readLine());
p.close();
response.close();
s.close();
}
}
And for server:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ObjectServer {
public static void main(String[] argv) throws Exception {
ServerSocket s = new ServerSocket(5000);
System.out.println("Server started");
while (true) {
Socket t = s.accept();// wait for client to connect
System.out.println("server connected");
ObjectInputStream b = new ObjectInputStream(t.getInputStream());
Student received = (Student) b.readObject();
PrintWriter output = new PrintWriter(t.getOutputStream(), true);
output.println("Student " + received.getName() + " with age: "
+ received.getAge() + " has been received");
b.close();
output.close();
t.close();
}
}
}
And do not forget your bean or dto:
public class Student implements Serializable {
private String name;
private int age;
private int mark;
public Student(String name, int age, int mark) {
this.name = name;
this.age = age;
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
}
Comments
-
HeKToN almost 2 years
Guys I have tried so many ways to do this but I could not manage to make it work. Basically I have a client(via terminal) and a server asking for name, mark and age and when we input these three the server writes back the details. My question is how to make it complete GUI so writing the details on GUI and receiving them there.
This is the code for the client.
import java.util.*; import java.net.*; import java.io.*; public class Student implements Serializable { String name; int mark; int age; public Student (String n, int a,int ag){ name=n;mark=a;age=ag; } public String toString(){ return "Name:"+name+" Age: "+age+ " Mark:"+mark ; } } class objectClient1{ public static void main(String[] args) throws Exception{ Socket s = new Socket("localhost",5000); ObjectOutputStream p =new ObjectOutputStream(s.getOutputStream()); ObjectInputStream q =new ObjectInputStream(s.getInputStream()); Scanner b = new Scanner(System.in); int c; System.out.println("Student name: "); while(b.hasNext()) { String name=b.nextLine(); System.out.println("Mark: "); int mark=Integer.parseInt(b.nextLine()); System.out.println("Age: "); int age=Integer.parseInt(b.nextLine()); p.writeObject(new Student(name,mark,age)); p.flush(); System.out.println(q.readObject()); } } }
And this is for the server:
import java.io.*; import java.net.*; class objectEchoServer { public static void main(String[] argv) throws Exception {ServerSocket s = new ServerSocket(5000); Socket t = s.accept();//wait for client to connect System.out.println("server connected"); ObjectInputStream b = new ObjectInputStream(t.getInputStream()); ObjectOutputStream q = new ObjectOutputStream(t.getOutputStream()); Object c; while((c=b.readObject())!=null) { q.writeObject(c); } } }
Any help highly appreciated!
-
HeKToN about 11 yearsThank you! I`m going to try it now.
-
Marcelo Tataje about 11 yearsI didn't write a "complete code", just the basic structure, remember you have to set your bounds for the textfields, buttons and textareas. As well as define the layout for the frame and add the components to the frame. Best regards.
-
HeKToN about 11 yearsBefore I go on could you please tell my why it says that txtS symbol is not found? Thank you for the help again.
-
Marcelo Tataje about 11 yearsOh, in the code, if you can see, all the components are declared inside the method init, you must declare them as global variables, so you can use them in the method actionPerformed then. I have edited the code declaring the components as global, that's the way I must have done. Sorry for that.
-
HeKToN about 11 yearsI really lost it again... This is so confusing the whole swing stuff. I fixed that one and now it is saying that the student() symbol can be found which is obvious since it was never implemented. Very confusing
-
Marcelo Tataje about 11 yearsnot really, in a few minutes I will send you a link with the code of the whole stuff running.
-
HeKToN about 11 yearsThis works just perfect and it is exactly what I needed. Excellent job. Thanks a lot!
-
Marcelo Tataje about 11 yearsPlease, mark my answer as the right one to set this question as solved. Best regards.
-
Marcelo Tataje about 11 yearsdo not worry, I'm not helping because of the score, I just wanted to help you since I love coding in Java, but I asked you to mark the question as solved for somebody that is looking for the same answer or has the same problem you had. Best regards, pleased to help you :).