stoneharry

Untitled

Aug 28th, 2012
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. package battlenetemu;
  2.  
  3. import java.io.InputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6.  
  7. /**
  8.  *
  9.  * @author Harry
  10.  */
  11. public class BattleNetEmu
  12. {
  13.  
  14.     public static Boolean shutdown = false;
  15.    
  16.     public static final int AUTH_INFO_REQUEST = 64;
  17.    
  18.     public static void main(String[] args)
  19.     {
  20.         setUpSockets();
  21.     }
  22.    
  23.     public static void setUpSockets()
  24.     {
  25.         try  
  26.         {  
  27.             ServerSocket entrance = new ServerSocket(1119);  
  28.             Socket s = new Socket();  
  29.             // entrance.setSoTimeout(2000);  
  30.             // s.setSoTimeout(100);  
  31.             // System.out.println(s.getSoTimeout());
  32.             while (!shutdown)
  33.             {
  34.                 s = entrance.accept();  
  35.                 InputStream inputData = s.getInputStream();  
  36.    
  37.                 HandlePacket(inputData);
  38.                
  39.                 /*String msg = new String();  
  40.                 int a = 0;  
  41.                 while ( (a = inputData.read()) != -1  )  
  42.                 {  
  43.                     byte i = (byte) a;  
  44.                     String st = new Character((char)i).toString();  
  45.                     msg += st;  
  46.                 }
  47.                 System.out.println(msg+"\n of length "+msg.length());*/
  48.                
  49.                 inputData.close();
  50.             }
  51.             entrance.close();
  52.             s.close();  
  53.  
  54.         }  
  55.         catch(Exception e)  
  56.         {  
  57.             System.out.print(e.toString());  
  58.         }
  59.     }
  60.    
  61.     public static void HandlePacket(InputStream inputData)
  62.     {
  63.         if (!readHeader(inputData))
  64.         {
  65.             System.out.println("ERROR: Failed to read packet header.");
  66.             return;
  67.         }
  68.     }
  69.    
  70.     public static boolean readHeader(InputStream input)
  71.     {
  72.         try
  73.         {
  74.             int ID = input.read();
  75.             int channel = input.read();
  76.             if (channel != 0)
  77.             {
  78.                 System.out.println("ERROR: Cannot handle channel: " + String.valueOf(channel) + " from packet ID: " + String.valueOf(ID));
  79.                 return false;
  80.             }
  81.             if (ID == AUTH_INFO_REQUEST)
  82.             {
  83.                 for (int n = 0; n < 3; n++)
  84.                 {
  85.                     char[] t = new char[4];
  86.                     t[0] = (char)input.read();
  87.                     t[1] = (char)input.read();
  88.                     t[2] = (char)input.read();
  89.                     t[3] = (char)input.read();
  90.                     System.out.println("Got: " + t);
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 System.out.println("ERROR: Got unhandled packet: " + String.valueOf(ID) + " on channel: " + String.valueOf(channel));
  96.                 return false;
  97.             }
  98.         }
  99.         catch (Exception ex)
  100.         {
  101.             ex.printStackTrace();
  102.             return false;
  103.         }
  104.         return true;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment