package src;
//listens on a port for connectiong microwaves and then assigns them
//their own thread
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class StationServer extends Thread {
//port that is being listened on
private int port;
//stores the microwaves, their ID serves as an index
static StationConnect[] tracker=new StationConnect[9999];
//server sockets
ServerSocket server=null;
Socket sock=null;
String received;
private int code=0;
StationServer(int port){
//assigns the port
this.port=port;
//starts the thread
start();
}
public void run() {
try {
//starts the server socket
server = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not listen on port "+port);
System.exit(-1);
}
//is constantly listening
while(true){
try {
//detects a new connection
sock=server.accept();
//reads in the sent microwave ID
DataInputStream in = new DataInputStream(
sock.getInputStream());
DataOutputStream out = new DataOutputStream(
sock.getOutputStream());
String usr,pword;
code=in.readByte();
if(code==01){
usr=in.readUTF();
pword=in.readUTF();
int ID=Services.authenticate(usr, pword);
if(ID==-1){
out.writeByte(250);
}
else if(ID==-2){
out.writeByte(251);
}
else{
out.writeByte(255);
tracker[ID]=new StationConnect(ID,sock);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|