我正在创建一个java服务器和一个java客户端。我需要从服务器发送到客户端,反之亦然,一个对象,然后是一个对象数组。我如何才能做到这一点?我需要序列化对象类吗?
这是服务器:
import java.io.*;
import java.net.*;
public class Server extends Thread {
private final ServerSocket Server;
public static void main(String[] args) throws Exception {
new Server();
}
public Server() throws Exception {
Server = new ServerSocket(3000);
System.out.println("Server started on port 3000.");
this.start();
}
@Override
public void run() {
while (true) {
try {
System.out.println("Server is listening to new connections...");
Socket client = Server.accept();
System.out.println("Connection accepted from: " + client.getInetAddress());
Connect c = new Connect(client);
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
}
class Connect extends Thread {
private Socket client = null;
BufferedReader in = null;
PrintStream out = null;
public Connect(Socket clientSocket) {
client = clientSocket;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintStream(client.getOutputStream(), true);
} catch (IOException mainException) {
try {
client.close();
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
this.start();
}
@Override
public void run() {
try {
out.close();
in.close();
client.close();
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
}
这是我的客户:
import java.io.*;
import java.net.*;
public class Client {
String remoteAddress = "localhost";
BufferedReader in = null;
PrintStream out = null;
Socket socket = null;
String message = null;
String username = null;
String password = null;
public Client(String username, String password) {
this.username = username;
this.password = password;
}
public String connect() {
try {
// begin a new client connection
socket = new Socket(remoteAddress, 3000);
// open I-O channels
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream(), true);
} catch (Exception exception) {
return false;
System.out.println(exception.getMessage());
}
return "ERROR";
}
public boolean disconnect() throws IOException {
// close flushes I-O with the server
out.close();
in.close();
return true;
}
}
相反,这是一个类:
class Contact {
private String name;
private String surname;
private String telephone;
private String birthday;
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getTelephone() {
return telephone;
}
public String getBirthday() {
return birthday;
}
public void setName(String value) {
name = value;
}
public void setSurname(String value) {
surname = value;
}
public void setTelephone(String value) {
telephone = value;
}
public void setBirthday(String value) {
birthday = value;
}
}
目前,只有服务器可以向客户端发送数据(对象、数组或仅对象),但我正在考虑让两者都能做到这一点。另外,发送一个对象(就像上面的类),一个相同对象的数组和一个不同对象的数组(我不能用经典数组获得它,对吗?我可以使用ArrayList
吗?)
谢谢。
发布于 2014-10-07 21:10:18
那java.io.ObjectOutputStream呢?尝试此http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
编辑:以下是包含在-slightly modified类的javadoc中的示例,如注释中所示:
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
oos.writeInt(12345);
oos.writeObject("Today");
oos.writeObject(new Date());
oos.close();
在另一边应该有一个java.io.ObjectInputStream。
发布于 2014-10-07 21:16:01
是的,你应该为此使用序列化。在这种情况下,您可以使用ObjectOutpuStream和writeObject()方法。因此,管理它非常简单,无需考虑计算位数等问题。http://www.tutorialspoint.com/java/java_serialization.htm
https://stackoverflow.com/questions/26245306
复制相似问题