在这里你有整个事情:
对于客户:
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;
}
}