/**
* Brogaming server
* http://brogaming.ru
*/
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.security.MessageDigest;
import java.util.Properties;
public class BrogamingServer extends Thread{
Socket s;
int num;
public BrogamingServer(int num, Socket s) {
// copy data
this.num = num;
this.s = s;
// and run new stream (look function run())
setDaemon(true);
setPriority(NORM_PRIORITY);
start();
}
public static void main(String args[]) {
try {
int i = 0; // счетчик подключений
// create socket on 3128 port
ServerSocket server = new ServerSocket(3128);//, 0, InetAddress.getByName("192.168.1.9"));
System.out.println("server is started");
// listening port
while(true) {
new BrogamingServer(i, server.accept());
i++;
}
}catch(Exception e){System.out.println("init error: " + e);}
}
public void run() {
try {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("client connected");
RecieveDataFromClient();
}
});
t.setDaemon(true);
t.start();
}catch(Exception e){System.out.println("init error: " + e);}
}
private void RecieveDataFromClient() {
try {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
startConnection(is, os);
while(s.isConnected())
{
byte buf[] = new byte[64*1024];
int r = is.read(buf);
if (buf.length == 0 || r == 0)
{
os.write(new String("Buffer is empty").getBytes());
continue;
}
// create String with received data from client
String data = new String(buf, 0, r); //"UTF-8");
if (data == null || data.isEmpty())
{
os.write(new String("Recieved data is empty").getBytes());
continue;
}
System.out.println("Recieved Data: " + data);
os.write(new String("pong").getBytes());
os.flush();
}
}
catch(Exception e) {System.out.println("RecieveDataFromClient: " + e);}
}
private void startConnection(InputStream is, OutputStream os) {
try {
String ss;
readLine(is); // first line, ignore it for now
Properties props = new Properties();
while((ss=readLine(is))!=null && !ss.equals(""))
{
String[] q = ss.split(": ");
props.put(q[0], q[1]);
}
String key = (String) props.get("Sec-WebSocket-Key");
String r = key + "" + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // append "magic" key to it
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(r.getBytes("iso-8859-1"), 0, r.length());
byte[] sha1hash = md.digest();
String returnBase = base64(sha1hash);
String ret = "HTTP/1.1 101 Switching Protocols\r\n";
ret+="Upgrade: websocket\r\n";
ret+="Connection: Upgrade\r\n";
ret+="Sec-WebSocket-Accept: "+returnBase+"\r\n";
ret+="\r\n";
os.write(ret.getBytes());
os.flush();
}catch(Exception e){System.out.println("start connection error: " + e);}
}
private String base64(byte[] input) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
{
Class<?> c = Class.forName("sun.misc.BASE64Encoder");
java.lang.reflect.Method m = c.getMethod("encode", new Class<?>[]{byte[].class});
String s = (String) m.invoke(c.newInstance(), input);
return s;
}
private String readLine(InputStream in) {
try {
String line = "";
int pread;
int read = 0;
while(true)
{
pread = read;
read = in.read();
if(read!=13&&read!=10)
line += (char) read;
if(pread==13&&read==10) break;
}
return line;
}catch(IOException ex){}
return null;
}
}