java GUI客户端 - JAVA中的服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15247752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GUI client - server in JAVA
提问by HeKToN
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.
伙计们,我尝试了很多方法来做到这一点,但我无法让它发挥作用。基本上我有一个客户端(通过终端)和一个服务器询问姓名、标记和年龄,当我们输入这三个时,服务器写回详细信息。我的问题是如何使它完整的 GUI,以便在 GUI 上编写详细信息并在那里接收它们。
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!
任何帮助高度赞赏!
采纳答案by Marcelo Tataje
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:
并且不要忘记您的 bean 或 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;
}
}