2

みんな私はこれを行うために非常に多くの方法を試しましたが、それを機能させることができませんでした。基本的に私はクライアント(ターミナル経由)とサーバーが名前、マーク、年齢を尋ねており、これら3つを入力すると、サーバーが詳細を書き戻します。私の質問は、それを完全な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 に答える