Hallo zusammen,
ich habe ein Problem mit dem Verschicken einer Class über das Netzwerk. Das Problem ist, dass der Empfänger diese nicht lesen kann. Deshalb schreibe ich hier um nach Rat zu fragen.
Normal Variablen kann ich ohne Problem verschicken, das Problem ist nur das Übertragen der Class
Das ist der Server
Das ist der Client
Und das ist die Klasse, die verschickt werden soll. Sie ist be Server und Client gleich.
Ich danke für jede art von hilfe.
ich habe ein Problem mit dem Verschicken einer Class über das Netzwerk. Das Problem ist, dass der Empfänger diese nicht lesen kann. Deshalb schreibe ich hier um nach Rat zu fragen.
Normal Variablen kann ich ohne Problem verschicken, das Problem ist nur das Übertragen der Class
Das ist der Server
Java:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Server {
public static Dimension size = new Dimension(300, 300);
public static int port = 2406;
public static String ip = "";
public static ServerSocket server;
public static int maxPlayers = 5;
public static int playerLoggedin = 0;
public static JPanel content;
public static JList list_clients;
public static DefaultListModel list_clients_model;
//Statelist:
//0 = Nothing
//1 = Client is on
//2 = Client disconnect
//3 = Client kick
public static int[] statelist = new int[maxPlayers];
public static Socket[] socketlist = new Socket[maxPlayers];
public static String[] namelist = new String[maxPlayers];
public static DataPackage[] datalist = new DataPackage[maxPlayers];
public static int maxTimeouts = 10;
public static int[] timeouts = new int[maxPlayers];
public static long[] timesetTimeout = new long[maxPlayers];
public static void disconnectUser(int index){
try{
list_clients_model.addElement("Disconnect: " + socketlist[index].getInetAddress().getHostAddress() + " - " + namelist[index]);
}catch (Exception e) {}
socketlist[index] = null;
namelist[index] = null;
datalist[index] = null;
statelist[index] = 0;
playerLoggedin--;
}
private static Runnable accept = new Runnable()
{
public void run()
{
new Thread(send).start();
new Thread(receive).start();
while (true)
{
try
{
Socket socket = server.accept();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String username = (String) ois.readObject();
boolean accepted = true;
for(int i = 0; i < maxPlayers; i++){
if(username.equals(namelist[i])){
accepted = false;
}
}
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
if(accepted && playerLoggedin < maxPlayers){
oos.writeObject("Welcome To This Server...");
for(int i = 0; i < maxPlayers; i++){
if(statelist[i] == 0){
socketlist[i] = socket;
namelist[i] = username;
datalist[i] = new DataPackage();
datalist[i].username = username;
statelist[i] = 1;
timesetTimeout[i] = System.currentTimeMillis();
list_clients_model.addElement("Connect: " + socket.getInetAddress().getHostAddress() + " - " + datalist[i].username);
playerLoggedin++;
break;
}
}
} else if(accepted && playerLoggedin >= maxPlayers) {
oos.writeObject("Max Players!");
} else {
oos.writeObject("User is already logged in!");
}
}
catch (Exception e) {}
}
}
};
private static Runnable send = new Runnable() {
public void run() {
ObjectOutputStream oos;
while (true) {
for (int i = 0; i < maxPlayers; i++) {
if(statelist[i] != 0) {
int client_state = statelist[i];
try {
oos = new ObjectOutputStream(socketlist[i].getOutputStream());
oos.writeObject(client_state);
oos = new ObjectOutputStream(socketlist[i].getOutputStream());
oos.writeObject(datalist);
timeouts[i] = 0;
} catch (Exception ex) {
if(System.currentTimeMillis() > timesetTimeout[i] + 1000){
timeouts[i]++;
timesetTimeout[i] = System.currentTimeMillis();
try{
list_clients_model.addElement("Timeout: " + socketlist[i].getInetAddress().getHostAddress() + " - " + namelist[i] + " - " + timeouts[i]);
}catch (Exception e) {}
}
if(timeouts[i] > maxTimeouts){
disconnectUser(i);
}
}
if (client_state == 3) // Kicked by Server
{
disconnectUser(i);
}
else if (client_state == 2) // Server Disconnected
{
disconnectUser(i);
}
}
}
}
}
};
private static Runnable receive = new Runnable() {
public void run() {
ObjectInputStream ois;
while (true) {
for (int i = 0; i < maxPlayers; i++) {
if(statelist[i] != 0) {
int receive_state = 1;
try {
ois = new ObjectInputStream(socketlist[i].getInputStream());
receive_state = (Integer) ois.readObject();
ois = new ObjectInputStream(socketlist[i].getInputStream());
DataPackage dp = (DataPackage) ois.readObject();
datalist[i] = dp;
}
catch (Exception ex) { // Client Disconnected (Client Didn't Notify Server About Disconnecting)
//disconnectUser(i);
}
if (receive_state == 2) // Client Disconnected by User
{
statelist[i] = 2;
disconnectUser(i);
}
}
}
}
}
};
public static void main(String args[]) {
JFrame frame = new JFrame();
list_clients_model = new DefaultListModel();
list_clients = new JList(list_clients_model);
content = new JPanel();
content.setLayout(new BorderLayout(1, 1));
content.add(new JScrollPane(list_clients), BorderLayout.CENTER);
content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.setContentPane(content);
frame.setPreferredSize(size);
frame.pack();
frame.setTitle("");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowListener()
{
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
boolean allloggedout = false;
while (!allloggedout)
{
try
{
for (int i = 0; i < maxPlayers; i++)
{
statelist[i] = 2;
}
allloggedout = true;
for (int i = 0; i < maxPlayers; i++)
{
if(socketlist[i] != null){
allloggedout = false;
}
}
}
catch (Exception ex) {}
}
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
frame.setVisible(true);
try
{
ip = InetAddress.getLocalHost().getHostAddress() + ":" + port;
server = new ServerSocket(port, 0, InetAddress.getLocalHost());
new Thread(accept).start();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Alert", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
Das ist der Client
Java:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client
{
public static Dimension size = new Dimension(300, 300);
public static Socket socket;
public static int port = 2406;
public static String ip = "";
public static String username = "";
public static int client_state = 1;
private static Runnable send = new Runnable() {
public void run() {
ObjectOutputStream oos;
while(true){
try {
DataPackage dp = new DataPackage();
dp.x = 0;
dp.y = 0;
dp.dir = 0;
dp.angle = 0;
dp.username = username;
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(client_state);
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(dp);
} catch (Exception ex) {}
}
}
};
private static Runnable receive = new Runnable() {
public void run() {
ObjectInputStream ois;
while(true){
int receive_state = 1;
try {
ois = new ObjectInputStream(socket.getInputStream());
receive_state = (Integer)ois.readObject();
ois = new ObjectInputStream(socket.getInputStream());
DataPackage[] dp = (DataPackage[]) ois.readObject();
} catch (Exception ex) {}
if(receive_state == 2){
JOptionPane.showMessageDialog(null, "Server Disconnected", "Message", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
};
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setPreferredSize(size);
frame.pack();
frame.setTitle("");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowListener()
{
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
client_state = 2;
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try
{
String local;
try
{
local = InetAddress.getLocalHost().getHostAddress() + ":" + port;
}
catch (UnknownHostException ex)
{
local = "Network Error";
}
ip = (String) JOptionPane.showInputDialog(null, "IP: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, local);
port = Integer.parseInt(ip.substring(ip.indexOf(":") + 1));
ip = ip.substring(0, ip.indexOf(":"));
socket = new Socket(ip, port);
username = System.getProperty("user.name");
username = (String) JOptionPane.showInputDialog(null, "Username: ", "Info", JOptionPane.INFORMATION_MESSAGE, null, null, username);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(username);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String response = (String) ois.readObject();
JOptionPane.showMessageDialog(null, response, "Message", JOptionPane.INFORMATION_MESSAGE);
if(response.equalsIgnoreCase("User is already logged in!")||response.equalsIgnoreCase("Max Players!")){
System.exit(0);
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(), "Alert", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
new Thread(send).start();
new Thread(receive).start();
}
}
Und das ist die Klasse, die verschickt werden soll. Sie ist be Server und Client gleich.
Java:
import java.io.Serializable;
public class DataPackage implements Serializable
{
private static final long serialVersionUID = 1L;
public double x = 0;
public double y = 0;
public double dir = 0;
public double angle = 0;
public String username = "";
public DataPackage() {
}
}
Ich danke für jede art von hilfe.