Advertisement
Guest User

server code

a guest
Nov 13th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.92 KB | None | 0 0
  1. //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.
  2.  
  3. java.io.StreamCorruptedException: invalid stream header: 73720027
  4.     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
  5.     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
  6.     at com.gopro2027.lwjgl.server.ServerThread$1$1.run(ServerThread.java:88)
  7.     at java.lang.Thread.run(Thread.java:745)
  8. Could not send data Timeout: 0
  9.  
  10.  
  11. //classes//////////////////////////////////////////////////////////////////////////////////
  12. //a lot of things are inside of the // and /* */ because i tried what you guys said to try.
  13. //Client.java////////////////////////////////////////////////////////////////////////////////
  14. package com.gopro2027.lwjgl.client;
  15.  
  16. import java.io.BufferedOutputStream;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.net.InetAddress;
  21. import java.net.Socket;
  22. import org.lwjgl.util.vector.Vector3f;
  23.  
  24. import com.gopro2027.lwjgl.Camera;
  25. import com.gopro2027.lwjgl.Game;
  26. import com.gopro2027.lwjgl.Start;
  27.  
  28. public class Client {
  29.  
  30.     public static Socket server;
  31.     public static DataInputStream in;
  32.     public static DataOutputStream out;
  33.    
  34.     public Client() {
  35.        
  36.         try {
  37.             server = new Socket(InetAddress.getByName(Start.ip), Start.port);
  38.             System.out.println("connected");
  39.            
  40.             ClientThread.start();
  41.            
  42.            
  43.         } catch (Exception e) {
  44.             System.out.println("Could not start client");
  45.             Game.connected = false;
  46.         }
  47.     }
  48.  
  49.    
  50.     public static void sendTriangle(Vector3f a, Vector3f b, Vector3f c) {
  51.             /*String p = "@@";
  52.             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;
  53.             out = new DataOutputStream(server.getOutputStream());
  54.             out.writeUTF(coords);
  55.             out.flush();*/
  56.         System.out.println("going to send");
  57.             try {
  58.                 /*BufferedOutputStream bis = new BufferedOutputStream(server.getOutputStream());
  59.                 ObjectOutputStream oos = new ObjectOutputStream(bis);*/
  60.                 TriangleSend op = new TriangleSend(a.x,a.y,a.z,b.x,b.y,b.z,c.x,c.y,c.z,1);
  61.                 /*oos.writeObject(op);
  62.                 oos.flush();
  63.                 System.out.println("Send triangle client");*/
  64.                            
  65.                            
  66.                            
  67.                 try {
  68.                     ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(server.getOutputStream()));
  69.                     oos.writeObject(op);
  70.                     oos.flush();
  71.                 } catch (Exception e) {
  72.                     e.printStackTrace();
  73.                 }
  74.             } catch (Exception e) {}
  75.     }
  76. }
  77.  
  78.  
  79.  
  80. //ClientThread.java/////////////////////////////////////////////////////////////////////////////////////////////
  81. package com.gopro2027.lwjgl.server;
  82.  
  83. import java.io.BufferedInputStream;
  84. import java.io.BufferedOutputStream;
  85. import java.io.DataInputStream;
  86. import java.io.DataOutputStream;
  87. import java.io.IOException;
  88. import java.io.ObjectInputStream;
  89. import java.io.ObjectOutputStream;
  90. import java.net.Socket;
  91.  
  92. import com.gopro2027.lwjgl.client.OnlinePlayer;
  93. import com.gopro2027.lwjgl.client.TriangleSend;
  94.  
  95. public class ServerThread{
  96.  
  97.     public static int n = 0;
  98.    
  99.     static TriangleSend[] savedtriangles = new TriangleSend[9999];
  100.    
  101.     static int numberrr = 0;
  102.    
  103.     public static void start() {
  104.        
  105.         new Thread(new Runnable() {
  106.         public void run() {
  107.         while (true) {
  108.             System.out.println(Server.clientnum);
  109.             try {
  110.                 System.out.println("trying to connect a client");
  111.                 final Socket client = Server.server.accept();
  112.                
  113.                 String clientip = Server.server.getInetAddress().getHostAddress();
  114.                
  115.                 System.out.println("Connected client ip " + clientip);
  116.                
  117.                 Server.clients[Server.clientnum] = client;
  118.                
  119.                 Server.clientnum++;
  120.                
  121.                 sendStart(client);
  122.                
  123.                 new Thread(new Runnable() {
  124.                     public void run() {
  125.                         boolean run = true;
  126.                         int timeout = 0;
  127.                         try {
  128.                             Server.ois = new ObjectInputStream(client.getInputStream());
  129.                         } catch (IOException e1) {
  130.                         }
  131.                         while (true) {
  132.                             if (run) {
  133.                                 try {
  134.                                             /*Server.in = new DataInputStream(client.getInputStream());
  135.                                             String input = Server.in.readUTF();
  136.                                             send(input);
  137.                                             timeout = 0;
  138.                                    
  139.                                             savedtriangles[numberrr] = input;
  140.                                             numberrr++;*/
  141.                                     /*BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
  142.                                     Server.ois = new ObjectInputStream(bis);
  143.                                     while(Server.ois.available() != 0) {
  144.                                         try {
  145.                                             OnlinePlayer tmp = (OnlinePlayer)Server.ois.readObject();
  146.                                             sendPlayerCoords(tmp);
  147.                                             Server.ois.close();
  148.                                         } catch (Exception e) {
  149.                                             continue;
  150.                                         }
  151.                                         try {
  152.                                             System.out.println("hey");
  153.                                             try {
  154.                                             TriangleSend ts = (TriangleSend)Server.ois.readObject();
  155.                                             send(ts);
  156.                                             Server.ois.close();
  157.                                             savedtriangles[numberrr] = ts;
  158.                                             numberrr++;
  159.                                             System.out.println("heyeyeyeyeyeyye");
  160.                                             } catch (Exception e) {
  161.                                                 e.printStackTrace();
  162.                                             }
  163.                                         } catch (Exception e) {
  164.                                             continue;
  165.                                         }
  166.                                     }*/
  167.                                    
  168.                                     Server.ois = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
  169.                                     while(Server.ois.available() != 0) {
  170.                                         try {
  171.                                             TriangleSend ts = (TriangleSend)Server.ois.readObject();
  172.                                             send(ts);
  173.                                             Server.ois.close();
  174.                                         } catch (Exception e) {
  175.                                             e.printStackTrace();
  176.                                             continue;
  177.                                         }
  178.                                     }
  179.                                    
  180.                                 } catch(Exception e) {
  181.                                     e.printStackTrace();
  182.                                     System.err.println("Could not send data Timeout: " + timeout);
  183.                                     timeout++;
  184.                                 }      
  185.                                 if (timeout > 100) {
  186.                                     try {
  187.                                         run = false;
  188.                                         client.close();
  189.                                     } catch (IOException e) {}
  190.                                 }
  191.                             }
  192.                         }
  193.                     }
  194.                 }).start();
  195.             } catch(Exception e) {}
  196.         }
  197.         }
  198.         }).start();
  199.     }
  200.    
  201.    
  202.     /*public static void send(TriangleSend coords) {
  203.             for (Socket client : Server.clients) {
  204.                 try {
  205.                     if (client != null) {
  206.                         try {
  207.                             BufferedOutputStream bis = new BufferedOutputStream(client.getOutputStream());
  208.                             ObjectOutputStream oos = new ObjectOutputStream(bis);
  209.                             oos.writeObject(coords);
  210.                             oos.flush();
  211.                             System.out.println("sent data on server side");
  212.                         } catch (Exception e) {}
  213.                     }
  214.                 } catch (NullPointerException e) {
  215.                     continue;
  216.                 }
  217.             }
  218.     }*/
  219.    
  220.    
  221.     public static void send(TriangleSend coords) {
  222.         for (Socket client : Server.clients) {
  223.             if (client != null) {
  224.                 try {
  225.                     ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
  226.                     oos.writeObject(coords);
  227.                     oos.flush();
  228.                 } catch (Exception e) {
  229.                     e.printStackTrace();
  230.                 }
  231.             }
  232.         }
  233.     }
  234.    
  235.     public static void sendPlayerCoords(OnlinePlayer op) {
  236.         for (Socket client : Server.clients) {
  237.             try {
  238.                 if (client != null) {
  239.                         ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
  240.                         oos.writeObject(op);
  241.                         oos.flush();
  242.                 }
  243.             } catch (NullPointerException | IOException e) {
  244.                 continue;
  245.             }
  246.         }
  247.     }
  248.    
  249.     public static void sendStart(Socket client) {
  250.         try {
  251.             Server.in = new DataInputStream(client.getInputStream());
  252.             Server.out = new DataOutputStream(client.getOutputStream());
  253.             for (TriangleSend coords : savedtriangles) {
  254.                 try {
  255.                 if (coords != null) {
  256.                     ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
  257.                     oos.writeObject(coords);
  258.                     oos.flush();
  259.                 }
  260.                 } catch (Exception e) {
  261.                     continue;
  262.                 }
  263.             }
  264.         }catch(Exception e) {
  265.            
  266.         }
  267.     }
  268. }
  269.  
  270. //Server.java/////////////////////////////////////////////////////////////////////////////////////////
  271. package com.gopro2027.lwjgl.server;
  272.  
  273. import java.io.DataInputStream;
  274. import java.io.DataOutputStream;
  275. import java.io.ObjectInputStream;
  276. import java.net.InetAddress;
  277. import java.net.ServerSocket;
  278. import java.net.Socket;
  279.  
  280. import com.gopro2027.lwjgl.Start;
  281.  
  282. public class Server {
  283.  
  284.     public static ServerSocket server;
  285.     public static DataOutputStream out;
  286.     public static DataInputStream in;
  287.     public static ObjectInputStream ois;
  288.    
  289.     public static Socket[] clients = new Socket[99];
  290.     public static int clientnum;
  291.    
  292.     public static String status = "Server Started";
  293.    
  294.     public Server() {
  295.         try {
  296.             server = new ServerSocket(Start.port,4,InetAddress.getByName(Start.ip));
  297.        
  298.             System.out.println("Starting server thread");
  299.        
  300.             ServerThread.start();
  301.        
  302.         } catch (Exception e) {
  303.             System.out.println("could not start");
  304.             status = "Server Failed To Start";
  305.         }
  306.     }
  307.    
  308. }
  309.  
  310.  
  311. //ServerThread.java///////////////////////////////////////////////////////////////////////////////////////
  312. package com.gopro2027.lwjgl.server;
  313.  
  314. import java.io.BufferedInputStream;
  315. import java.io.BufferedOutputStream;
  316. import java.io.DataInputStream;
  317. import java.io.DataOutputStream;
  318. import java.io.IOException;
  319. import java.io.ObjectInputStream;
  320. import java.io.ObjectOutputStream;
  321. import java.net.Socket;
  322.  
  323. import com.gopro2027.lwjgl.client.OnlinePlayer;
  324. import com.gopro2027.lwjgl.client.TriangleSend;
  325.  
  326. public class ServerThread{
  327.  
  328.     public static int n = 0;
  329.    
  330.     static TriangleSend[] savedtriangles = new TriangleSend[9999];
  331.    
  332.     static int numberrr = 0;
  333.    
  334.     public static void start() {
  335.        
  336.         new Thread(new Runnable() {
  337.         public void run() {
  338.         while (true) {
  339.             System.out.println(Server.clientnum);
  340.             try {
  341.                 System.out.println("trying to connect a client");
  342.                 final Socket client = Server.server.accept();
  343.                
  344.                 String clientip = Server.server.getInetAddress().getHostAddress();
  345.                
  346.                 System.out.println("Connected client ip " + clientip);
  347.                
  348.                 Server.clients[Server.clientnum] = client;
  349.                
  350.                 Server.clientnum++;
  351.                
  352.                 sendStart(client);
  353.                
  354.                 new Thread(new Runnable() {
  355.                     public void run() {
  356.                         boolean run = true;
  357.                         int timeout = 0;
  358.                         try {
  359.                             Server.ois = new ObjectInputStream(client.getInputStream());
  360.                         } catch (IOException e1) {
  361.                         }
  362.                         while (true) {
  363.                             if (run) {
  364.                                 try {
  365.                                             /*Server.in = new DataInputStream(client.getInputStream());
  366.                                             String input = Server.in.readUTF();
  367.                                             send(input);
  368.                                             timeout = 0;
  369.                                    
  370.                                             savedtriangles[numberrr] = input;
  371.                                             numberrr++;*/
  372.                                     /*BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
  373.                                     Server.ois = new ObjectInputStream(bis);
  374.                                     while(Server.ois.available() != 0) {
  375.                                         try {
  376.                                             OnlinePlayer tmp = (OnlinePlayer)Server.ois.readObject();
  377.                                             sendPlayerCoords(tmp);
  378.                                             Server.ois.close();
  379.                                         } catch (Exception e) {
  380.                                             continue;
  381.                                         }
  382.                                         try {
  383.                                             System.out.println("hey");
  384.                                             try {
  385.                                             TriangleSend ts = (TriangleSend)Server.ois.readObject();
  386.                                             send(ts);
  387.                                             Server.ois.close();
  388.                                             savedtriangles[numberrr] = ts;
  389.                                             numberrr++;
  390.                                             System.out.println("heyeyeyeyeyeyye");
  391.                                             } catch (Exception e) {
  392.                                                 e.printStackTrace();
  393.                                             }
  394.                                         } catch (Exception e) {
  395.                                             continue;
  396.                                         }
  397.                                     }*/
  398.                                    
  399.                                     Server.ois = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
  400.                                     while(Server.ois.available() != 0) {
  401.                                         try {
  402.                                             TriangleSend ts = (TriangleSend)Server.ois.readObject();
  403.                                             send(ts);
  404.                                             Server.ois.close();
  405.                                         } catch (Exception e) {
  406.                                             e.printStackTrace();
  407.                                             continue;
  408.                                         }
  409.                                     }
  410.                                    
  411.                                 } catch(Exception e) {
  412.                                     e.printStackTrace();
  413.                                     System.err.println("Could not send data Timeout: " + timeout);
  414.                                     timeout++;
  415.                                 }      
  416.                                 if (timeout > 100) {
  417.                                     try {
  418.                                         run = false;
  419.                                         client.close();
  420.                                     } catch (IOException e) {}
  421.                                 }
  422.                             }
  423.                         }
  424.                     }
  425.                 }).start();
  426.             } catch(Exception e) {}
  427.         }
  428.         }
  429.         }).start();
  430.     }
  431.    
  432.    
  433.     /*public static void send(TriangleSend coords) {
  434.             for (Socket client : Server.clients) {
  435.                 try {
  436.                     if (client != null) {
  437.                         try {
  438.                             BufferedOutputStream bis = new BufferedOutputStream(client.getOutputStream());
  439.                             ObjectOutputStream oos = new ObjectOutputStream(bis);
  440.                             oos.writeObject(coords);
  441.                             oos.flush();
  442.                             System.out.println("sent data on server side");
  443.                         } catch (Exception e) {}
  444.                     }
  445.                 } catch (NullPointerException e) {
  446.                     continue;
  447.                 }
  448.             }
  449.     }*/
  450.    
  451.    
  452.     public static void send(TriangleSend coords) {
  453.         for (Socket client : Server.clients) {
  454.             if (client != null) {
  455.                 try {
  456.                     ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
  457.                     oos.writeObject(coords);
  458.                     oos.flush();
  459.                 } catch (Exception e) {
  460.                     e.printStackTrace();
  461.                 }
  462.             }
  463.         }
  464.     }
  465.    
  466.     public static void sendPlayerCoords(OnlinePlayer op) {
  467.         for (Socket client : Server.clients) {
  468.             try {
  469.                 if (client != null) {
  470.                         ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
  471.                         oos.writeObject(op);
  472.                         oos.flush();
  473.                 }
  474.             } catch (NullPointerException | IOException e) {
  475.                 continue;
  476.             }
  477.         }
  478.     }
  479.    
  480.     public static void sendStart(Socket client) {
  481.         try {
  482.             Server.in = new DataInputStream(client.getInputStream());
  483.             Server.out = new DataOutputStream(client.getOutputStream());
  484.             for (TriangleSend coords : savedtriangles) {
  485.                 try {
  486.                 if (coords != null) {
  487.                     ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
  488.                     oos.writeObject(coords);
  489.                     oos.flush();
  490.                 }
  491.                 } catch (Exception e) {
  492.                     continue;
  493.                 }
  494.             }
  495.         }catch(Exception e) {
  496.            
  497.         }
  498.     }
  499. }
  500.  
  501. //TriangleSend.java/////////////////////////////////////////////////////////////////////////////
  502. package com.gopro2027.lwjgl.client;
  503.  
  504. import java.io.Serializable;
  505.  
  506. import org.newdawn.slick.opengl.Texture;
  507.  
  508. import com.gopro2027.lwjgl.Game;
  509.  
  510. public class TriangleSend implements Serializable{
  511.  
  512.     /**
  513.      *
  514.      */
  515.     private static final long serialVersionUID = 5963342927817008203L;
  516.    
  517.     public float x1;
  518.     public float y1;
  519.     public float z1;
  520.     public float x2;
  521.     public float y2;
  522.     public float z2;
  523.     public float x3;
  524.     public float y3;
  525.     public float z3;
  526.     public int texId;
  527.    
  528.     public TriangleSend(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, int tex) {
  529.         this.x1 = x1;
  530.         this.y1 = y1;
  531.         this.z1 = z1;
  532.         this.x2 = x2;
  533.         this.y2 = y2;
  534.         this.z2 = z2;
  535.         this.x3 = x3;
  536.         this.y3 = y3;
  537.         this.z3 = z3;
  538.         this.texId = tex;
  539.     }
  540.    
  541.     public void add() {
  542.         ClientThread.addTriangleTemp(this.x1, this.y1, this.z1, this.x2, this.y2, this.z2, this.x3, this.y3, this.z3, Game.texWood);
  543.     }
  544.  
  545. }
  546.  
  547.  
  548. //OnlinePlayer.java////////////////////////////////////////////////////////////////////////////////////////
  549. package com.gopro2027.lwjgl.client;
  550.  
  551. import org.lwjgl.opengl.GL11;
  552.  
  553. public class OnlinePlayer {
  554.  
  555.     public float x,y,z;
  556.  
  557.     public OnlinePlayer(float x,float y,float z) {
  558.         this.x = x;
  559.         this.y = y;
  560.         this.z = z;
  561.     }
  562.    
  563.     public void render() {
  564.         GL11.glBegin(GL11.GL_QUADS);
  565.             GL11.glColor3f(1, 0, 0);
  566.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  567.             GL11.glVertex3f(this.x+0.5f,this.y,this.z-0.5f);
  568.             GL11.glVertex3f(this.x+0.5f,this.y,this.z+0.5f);
  569.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  570.            
  571.             GL11.glColor3f(1, 0, 0);
  572.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  573.             GL11.glVertex3f(this.x+0.5f,this.y+1,this.z-0.5f);
  574.             GL11.glVertex3f(this.x+0.5f,this.y+1,this.z+0.5f);
  575.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  576.            
  577.             GL11.glColor3f(1, 0, 0);
  578.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  579.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  580.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z+0.5f);
  581.             GL11.glVertex3f(this.x-0.5f,this.y,this.z+0.5f);
  582.            
  583.             GL11.glColor3f(1, 0, 0);
  584.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  585.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  586.             GL11.glVertex3f(this.x+0.5f,this.y+1,this.z+0.5f);
  587.             GL11.glVertex3f(this.x+0.5f,this.y,this.z+0.5f);
  588.            
  589.             GL11.glColor3f(1, 0, 0);
  590.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  591.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  592.             GL11.glVertex3f(this.x+0.5f,this.y+1,this.z-0.5f);
  593.             GL11.glVertex3f(this.x+0.5f,this.y,this.z-0.5f);
  594.            
  595.             GL11.glColor3f(1, 0, 0);
  596.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  597.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  598.             GL11.glVertex3f(this.x-0.5f,this.y+1,this.z-0.5f);
  599.             GL11.glVertex3f(this.x-0.5f,this.y,this.z-0.5f);
  600.         GL11.glEnd();
  601.         GL11.glColor3f(1, 1, 1);
  602.     }
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement