Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //error when recieving the data and doing the send method to send back to the client. this is where it used to only generate an error when the client disconnects.
- java.io.StreamCorruptedException: invalid stream header: 73720027
- at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
- at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
- at com.gopro2027.lwjgl.server.ServerThread$1$1.run(ServerThread.java:88)
- at java.lang.Thread.run(Thread.java:745)
- Could not send data Timeout: 0
- //classes//////////////////////////////////////////////////////////////////////////////////
- //a lot of things are inside of the // and /* */ because i tried what you guys said to try.
- //Client.java////////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.client;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.ObjectOutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import org.lwjgl.util.vector.Vector3f;
- import com.gopro2027.lwjgl.Camera;
- import com.gopro2027.lwjgl.Game;
- import com.gopro2027.lwjgl.Start;
- public class Client {
- public static Socket server;
- public static DataInputStream in;
- public static DataOutputStream out;
- public Client() {
- try {
- server = new Socket(InetAddress.getByName(Start.ip), Start.port);
- System.out.println("connected");
- ClientThread.start();
- } catch (Exception e) {
- System.out.println("Could not start client");
- Game.connected = false;
- }
- }
- public static void sendTriangle(Vector3f a, Vector3f b, Vector3f c) {
- /*String p = "@@";
- String coords = a.x+p+a.y+p+a.z+p+b.x+p+b.y+p+b.z+p+c.x+p+c.y+p+c.z;
- out = new DataOutputStream(server.getOutputStream());
- out.writeUTF(coords);
- out.flush();*/
- System.out.println("going to send");
- try {
- /*BufferedOutputStream bis = new BufferedOutputStream(server.getOutputStream());
- ObjectOutputStream oos = new ObjectOutputStream(bis);*/
- TriangleSend op = new TriangleSend(a.x,a.y,a.z,b.x,b.y,b.z,c.x,c.y,c.z,1);
- /*oos.writeObject(op);
- oos.flush();
- System.out.println("Send triangle client");*/
- try {
- ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(server.getOutputStream()));
- oos.writeObject(op);
- oos.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (Exception e) {}
- }
- }
- //ClientThread.java/////////////////////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.server;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import com.gopro2027.lwjgl.client.OnlinePlayer;
- import com.gopro2027.lwjgl.client.TriangleSend;
- public class ServerThread{
- public static int n = 0;
- static TriangleSend[] savedtriangles = new TriangleSend[9999];
- static int numberrr = 0;
- public static void start() {
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- System.out.println(Server.clientnum);
- try {
- System.out.println("trying to connect a client");
- final Socket client = Server.server.accept();
- String clientip = Server.server.getInetAddress().getHostAddress();
- System.out.println("Connected client ip " + clientip);
- Server.clients[Server.clientnum] = client;
- Server.clientnum++;
- sendStart(client);
- new Thread(new Runnable() {
- public void run() {
- boolean run = true;
- int timeout = 0;
- try {
- Server.ois = new ObjectInputStream(client.getInputStream());
- } catch (IOException e1) {
- }
- while (true) {
- if (run) {
- try {
- /*Server.in = new DataInputStream(client.getInputStream());
- String input = Server.in.readUTF();
- send(input);
- timeout = 0;
- savedtriangles[numberrr] = input;
- numberrr++;*/
- /*BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
- Server.ois = new ObjectInputStream(bis);
- while(Server.ois.available() != 0) {
- try {
- OnlinePlayer tmp = (OnlinePlayer)Server.ois.readObject();
- sendPlayerCoords(tmp);
- Server.ois.close();
- } catch (Exception e) {
- continue;
- }
- try {
- System.out.println("hey");
- try {
- TriangleSend ts = (TriangleSend)Server.ois.readObject();
- send(ts);
- Server.ois.close();
- savedtriangles[numberrr] = ts;
- numberrr++;
- System.out.println("heyeyeyeyeyeyye");
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (Exception e) {
- continue;
- }
- }*/
- Server.ois = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
- while(Server.ois.available() != 0) {
- try {
- TriangleSend ts = (TriangleSend)Server.ois.readObject();
- send(ts);
- Server.ois.close();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- System.err.println("Could not send data Timeout: " + timeout);
- timeout++;
- }
- if (timeout > 100) {
- try {
- run = false;
- client.close();
- } catch (IOException e) {}
- }
- }
- }
- }
- }).start();
- } catch(Exception e) {}
- }
- }
- }).start();
- }
- /*public static void send(TriangleSend coords) {
- for (Socket client : Server.clients) {
- try {
- if (client != null) {
- try {
- BufferedOutputStream bis = new BufferedOutputStream(client.getOutputStream());
- ObjectOutputStream oos = new ObjectOutputStream(bis);
- oos.writeObject(coords);
- oos.flush();
- System.out.println("sent data on server side");
- } catch (Exception e) {}
- }
- } catch (NullPointerException e) {
- continue;
- }
- }
- }*/
- public static void send(TriangleSend coords) {
- for (Socket client : Server.clients) {
- if (client != null) {
- try {
- ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
- oos.writeObject(coords);
- oos.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void sendPlayerCoords(OnlinePlayer op) {
- for (Socket client : Server.clients) {
- try {
- if (client != null) {
- ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
- oos.writeObject(op);
- oos.flush();
- }
- } catch (NullPointerException | IOException e) {
- continue;
- }
- }
- }
- public static void sendStart(Socket client) {
- try {
- Server.in = new DataInputStream(client.getInputStream());
- Server.out = new DataOutputStream(client.getOutputStream());
- for (TriangleSend coords : savedtriangles) {
- try {
- if (coords != null) {
- ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
- oos.writeObject(coords);
- oos.flush();
- }
- } catch (Exception e) {
- continue;
- }
- }
- }catch(Exception e) {
- }
- }
- }
- //Server.java/////////////////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.server;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.ObjectInputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import com.gopro2027.lwjgl.Start;
- public class Server {
- public static ServerSocket server;
- public static DataOutputStream out;
- public static DataInputStream in;
- public static ObjectInputStream ois;
- public static Socket[] clients = new Socket[99];
- public static int clientnum;
- public static String status = "Server Started";
- public Server() {
- try {
- server = new ServerSocket(Start.port,4,InetAddress.getByName(Start.ip));
- System.out.println("Starting server thread");
- ServerThread.start();
- } catch (Exception e) {
- System.out.println("could not start");
- status = "Server Failed To Start";
- }
- }
- }
- //ServerThread.java///////////////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.server;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import com.gopro2027.lwjgl.client.OnlinePlayer;
- import com.gopro2027.lwjgl.client.TriangleSend;
- public class ServerThread{
- public static int n = 0;
- static TriangleSend[] savedtriangles = new TriangleSend[9999];
- static int numberrr = 0;
- public static void start() {
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- System.out.println(Server.clientnum);
- try {
- System.out.println("trying to connect a client");
- final Socket client = Server.server.accept();
- String clientip = Server.server.getInetAddress().getHostAddress();
- System.out.println("Connected client ip " + clientip);
- Server.clients[Server.clientnum] = client;
- Server.clientnum++;
- sendStart(client);
- new Thread(new Runnable() {
- public void run() {
- boolean run = true;
- int timeout = 0;
- try {
- Server.ois = new ObjectInputStream(client.getInputStream());
- } catch (IOException e1) {
- }
- while (true) {
- if (run) {
- try {
- /*Server.in = new DataInputStream(client.getInputStream());
- String input = Server.in.readUTF();
- send(input);
- timeout = 0;
- savedtriangles[numberrr] = input;
- numberrr++;*/
- /*BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
- Server.ois = new ObjectInputStream(bis);
- while(Server.ois.available() != 0) {
- try {
- OnlinePlayer tmp = (OnlinePlayer)Server.ois.readObject();
- sendPlayerCoords(tmp);
- Server.ois.close();
- } catch (Exception e) {
- continue;
- }
- try {
- System.out.println("hey");
- try {
- TriangleSend ts = (TriangleSend)Server.ois.readObject();
- send(ts);
- Server.ois.close();
- savedtriangles[numberrr] = ts;
- numberrr++;
- System.out.println("heyeyeyeyeyeyye");
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (Exception e) {
- continue;
- }
- }*/
- Server.ois = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
- while(Server.ois.available() != 0) {
- try {
- TriangleSend ts = (TriangleSend)Server.ois.readObject();
- send(ts);
- Server.ois.close();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- System.err.println("Could not send data Timeout: " + timeout);
- timeout++;
- }
- if (timeout > 100) {
- try {
- run = false;
- client.close();
- } catch (IOException e) {}
- }
- }
- }
- }
- }).start();
- } catch(Exception e) {}
- }
- }
- }).start();
- }
- /*public static void send(TriangleSend coords) {
- for (Socket client : Server.clients) {
- try {
- if (client != null) {
- try {
- BufferedOutputStream bis = new BufferedOutputStream(client.getOutputStream());
- ObjectOutputStream oos = new ObjectOutputStream(bis);
- oos.writeObject(coords);
- oos.flush();
- System.out.println("sent data on server side");
- } catch (Exception e) {}
- }
- } catch (NullPointerException e) {
- continue;
- }
- }
- }*/
- public static void send(TriangleSend coords) {
- for (Socket client : Server.clients) {
- if (client != null) {
- try {
- ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
- oos.writeObject(coords);
- oos.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void sendPlayerCoords(OnlinePlayer op) {
- for (Socket client : Server.clients) {
- try {
- if (client != null) {
- ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
- oos.writeObject(op);
- oos.flush();
- }
- } catch (NullPointerException | IOException e) {
- continue;
- }
- }
- }
- public static void sendStart(Socket client) {
- try {
- Server.in = new DataInputStream(client.getInputStream());
- Server.out = new DataOutputStream(client.getOutputStream());
- for (TriangleSend coords : savedtriangles) {
- try {
- if (coords != null) {
- ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
- oos.writeObject(coords);
- oos.flush();
- }
- } catch (Exception e) {
- continue;
- }
- }
- }catch(Exception e) {
- }
- }
- }
- //TriangleSend.java/////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.client;
- import java.io.Serializable;
- import org.newdawn.slick.opengl.Texture;
- import com.gopro2027.lwjgl.Game;
- public class TriangleSend implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = 5963342927817008203L;
- public float x1;
- public float y1;
- public float z1;
- public float x2;
- public float y2;
- public float z2;
- public float x3;
- public float y3;
- public float z3;
- public int texId;
- public TriangleSend(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, int tex) {
- this.x1 = x1;
- this.y1 = y1;
- this.z1 = z1;
- this.x2 = x2;
- this.y2 = y2;
- this.z2 = z2;
- this.x3 = x3;
- this.y3 = y3;
- this.z3 = z3;
- this.texId = tex;
- }
- public void add() {
- ClientThread.addTriangleTemp(this.x1, this.y1, this.z1, this.x2, this.y2, this.z2, this.x3, this.y3, this.z3, Game.texWood);
- }
- }
- //OnlinePlayer.java////////////////////////////////////////////////////////////////////////////////////////
- package com.gopro2027.lwjgl.client;
- import org.lwjgl.opengl.GL11;
- public class OnlinePlayer {
- public float x,y,z;
- public OnlinePlayer(float x,float y,float z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public void render() {
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y,this.z+0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y+1,this.z+0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z+0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z+0.5f);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y+1,this.z+0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y,this.z+0.5f);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x+0.5f,this.y,this.z-0.5f);
- GL11.glColor3f(1, 0, 0);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
- GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
- GL11.glEnd();
- GL11.glColor3f(1, 1, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement