2

伙计们,我已经尝试了很多方法来做到这一点,但我无法让它发挥作用。基本上我有一个客户端(通过终端)和一个服务器询问姓名、标记和年龄,当我们输入这三个时,服务器会写回详细信息。我的问题是如何使它成为完整的 GUI,以便在 GUI 上编写详细信息并在那里接收它们。

这是客户端的代码。

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());

   }
  }
 }

这是针对服务器的:

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);      

           }
             }
 }

任何帮助都非常感谢!

4

1 回答 1

1

在这里你有整个事情:

对于客户:

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();
    }

}

对于服务器:

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();
        }

    }
}

并且不要忘记您的 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;
    }

}
于 2013-03-06T13:04:56.163 回答