通过Java套接字发送对象
我有一个班名球员通过Java套接字发送对象,java,sockets,exception,Java,Sockets,Exception,我有一个班名球员 public JLabel imagen; public String Nombre; public Player(int x, int y, int width, int height, Icon icono, String name){ imagen = Player(x, y, width, height, icono); Nombre = name; } public JLabel Player(int x, int y, int width, in
public JLabel imagen;
public String Nombre;
public Player(int x, int y, int width, int height, Icon icono, String name){
imagen = Player(x, y, width, height, icono);
Nombre = name;
}
public JLabel Player(int x, int y, int width, int height, Icon icono){
JLabel imagen = new JLabel(icono);
imagen.setLocation(x, y);
imagen.setSize(width, height);
return imagen;
}
(用于创建新玩家)
我还有一个客户机类:
public class Cliente implements Runnable {
String host;
int puerto;
Player mensaje;
public Cliente(int purto, Player mensaje, String host){
this.puerto = purto;
this.mensaje = mensaje;
this.host = host;
}
@Override
public void run() {
DataOutputStream out;
try {
Socket sc = new Socket(host, puerto);
out = new DataOutputStream(sc.getOutputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject(mensaje);
sc.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
我在使用objectOutputStream,但它说
“java.io.NotSerializableException:objects.Player”
我想把我的播放器发送到服务器,但上面写着例外
如果需要,这里还有服务器类
public class Servidor extends Observable implements Runnable {
int puerto;
public Servidor(int puerto) {
this.puerto = puerto;
}
@Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
DataInputStream in;
try {
servidor = new ServerSocket(puerto);
System.out.println("server started");
while (true) {
sc = servidor.accept();
in = new DataInputStream(sc.getInputStream());
ObjectInputStream input = new ObjectInputStream(in);
Player players = null;
try {
players = (Player) input.readObject();
System.out.println(players.Nombre);
} catch (ClassNotFoundException ex) {
}
this.setChanged();
this.notifyObservers(players);
this.clearChanged();
sc.close();
}
} catch (IOException ex) {
}
}
}
如果需要,这里还有将请求发送到客户机类的代码行
Cliente c = new Cliente(5000, new Player(x, y, width, height, icon, "name of the player"), "the ip");
Thread t = new Thread(c);
t.start();
看起来您忘记了使Player对象可序列化,因此代码抛出了
java.io.NotSerializableException
如果需要通过网络发送某个对象,则该对象需要可序列化
序列化是获取一个对象的内存数据结构的过程
对象并将其编码为
字节。然后可以将此编码版本保存到磁盘,并通过
网络连接,或以其他方式与收件人通信。(来自
(维基百科网站)
我已经更新了代码
Player.java
import java.io.Serializable;
import javax.swing.Icon;
import javax.swing.JLabel;
public class Player implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public JLabel imagen;
public String Nombre;
public Player(int x, int y, int width, int height, Icon icono, String name) {
imagen = Player(x, y, width, height, icono);
Nombre = name;
}
public JLabel Player(int x, int y, int width, int height, Icon icono) {
JLabel imagen = new JLabel(icono);
imagen.setLocation(x, y);
imagen.setSize(width, height);
return imagen;
}
}
Cliente.java
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Cliente implements Runnable {
String host;
int puerto;
Player mensaje;
public Cliente(int purto, Player mensaje, String host) {
this.puerto = purto;
this.mensaje = mensaje;
this.host = host;
}
// @Override
public void run() {
DataOutputStream out;
try {
Socket sc = new Socket(host, puerto);
out = new DataOutputStream(sc.getOutputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject(mensaje);
sc.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void main(String[] args) {
Cliente c = new Cliente(5000, new Player(1, 2, 3, 4, null,
"Holis Studios"), "localhost");
Thread t = new Thread(c);
t.start();
}
}
Servidor.java
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Observable;
public class Servidor extends Observable implements Runnable {
int puerto;
public Servidor(int puerto) {
this.puerto = puerto;
}
// @Override
public void run() {
ServerSocket servidor = null;
Socket sc = null;
DataInputStream in;
try {
servidor = new ServerSocket(puerto);
System.out.println("server started");
while (true) {
sc = servidor.accept();
in = new DataInputStream(sc.getInputStream());
ObjectInputStream input = new ObjectInputStream(in);
Player players = null;
try {
players = (Player) input.readObject();
System.out.println(players.Nombre);
} catch (ClassNotFoundException ex) {
}
this.setChanged();
this.notifyObservers(players);
this.clearChanged();
sc.close();
}
} catch (IOException ex) {
}
}
public static void main(String[] args) {
Servidor server = new Servidor(5000);
Thread t = new Thread(server);
t.start();
}
}
编写守则:
javac.exe -cp . Player.java
javac.exe -cp . Servidor.java
javac.exe -cp . Cliente.java
运行:
Servidor控制台上显示的输出:
server started
Holis Studios
server started
Holis Studios