Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tcpserver;
- import java.awt.Color;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- import java.sql.*;
- import java.util.ArrayList;
- import javax.swing.*;
- public class TCPServer implements Runnable {
- private static final String UserName = "root";
- private static final String Password = "";
- private static final String ConnString = "jdbc:mysql://localhost:3306/football_db?zeroDateTimeBehavior=convertToNull";
- private ChatServerThread clients[] = new ChatServerThread[50];
- private ServerSocket server = null;
- private Thread thread = null;
- private int clientCount = 0;
- Connection conn = null;
- String[][] userId = new String[20][20];
- int userIdCount = -1;
- public TCPServer(int port) {
- try {
- System.out.println("Binding to port " + port + ", please wait ...");
- server = new ServerSocket(port);
- System.out.println("Server started: " + server);
- start();
- } catch (IOException ioe) {
- System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
- }
- }
- @Override
- public void run() {
- while (thread != null) {
- try {
- System.out.println("Waiting for a client ...");
- addThread(server.accept());
- } catch (IOException ioe) {
- System.out.println("Server accept error: " + ioe);
- stop();
- }
- }
- }
- public void start() {
- if (thread == null) {
- thread = new Thread(this);
- thread.start();
- }
- }
- public void stop() {
- if (thread != null) {
- thread.stop();
- thread = null;
- }
- }
- private int findClient(int ID) {
- for (int i = 0; i < clientCount; i++) {
- if (clients[i].getID() == ID) {
- return i;
- }
- }
- return -1;
- }
- public synchronized void handle(int ID, String input) throws ClassNotFoundException {
- //System.out.println("Client " + ID + " sent: " + input);
- if (input.endsWith("user")) {
- String[] msg = input.split(" ");
- String checkName = null;
- String checkPass = null;
- String[] temp;
- temp = checkLogin(msg[0]).split(" ");
- checkName = temp[0];
- checkPass = temp[1];
- if (msg[0].equals("") || !msg[0].equals(checkName) || !msg[1].equals(checkPass)) {
- System.out.println("Login failed");
- clients[findClient(ID)].send("login failed");
- } else {
- System.out.println("Login successful");
- //ai khne
- clients[findClient(ID)].send("login successful");
- onlineUser(msg[0]);
- userIdCount++;
- userId[userIdCount][0] = Integer.toString(ID);
- userId[userIdCount][1] = msg[0];
- System.out.println("userId[" + userIdCount + "][0]: " + userId[userIdCount][0] + " userId[" + userIdCount + "][1]: " + userId[userIdCount][1]);
- }
- } else if (input.equals("register")) {
- clients[findClient(ID)].send("register button pressed.");
- } else if (input.endsWith("registered")) {
- String[] msg = input.split(" ");
- String fname = msg[1];
- String user = msg[2];
- String pass = msg[3];
- registerEntry(fname, user, pass);
- clients[findClient(ID)].send("msg[4]: " + msg[4]);
- } else if (input.startsWith("team")) {
- String[] temp = input.split(" ");
- System.out.println("Alpha team chosen by " + ID);
- switch (temp[1]) {
- case "alpha": {
- System.out.println(">> Team alpha selected");
- //clients[findClient(ID)].send("check");
- String tempUserName;
- tempUserName = getNameById(ID);
- System.out.println(getNameById(ID));
- updateTeam(tempUserName, "alpha");
- break;
- }
- case "beta": {
- System.out.println(">> Team beta selected");
- //clients[findClient(ID)].send("beta selected");
- String tempUserName;
- tempUserName = getNameById(ID);
- System.out.println(getNameById(ID));
- updateTeam(tempUserName, "beta");
- break;
- }
- case "charlie": {
- System.out.println(">> Team charlie selected");
- //clients[findClient(ID)].send("charlie selected");
- String tempUserName;
- tempUserName = getNameById(ID);
- System.out.println(getNameById(ID));
- updateTeam(tempUserName, "charlie");
- break;
- }
- case "delta": {
- System.out.println(">> Team delta selected");
- //clients[findClient(ID)].send("delta selected");
- String tempUserName;
- tempUserName = getNameById(ID);
- System.out.println(getNameById(ID));
- updateTeam(tempUserName, "delta");
- break;
- }
- case "echo": {
- System.out.println(">> Team echo selected");
- //clients[findClient(ID)].send("echo selected");
- String tempUserName;
- tempUserName = getNameById(ID);
- System.out.println(getNameById(ID));
- updateTeam(tempUserName, "echo");
- break;
- }
- default: {
- System.out.println("Default switch selected.");
- break;
- }
- }
- clients[findClient(ID)].send("team selected");
- } else if(input.equals("check")){
- System.out.println("Check hche");
- }
- else if (input.equals("offline")) {
- offlineEveryone();
- } else if (input.equals(".bye")) {
- clients[findClient(ID)].send(".bye");
- remove(ID);
- } else {
- /*for (int i = 0; i < clientCount; i++) {
- clients[i].send(ID + " sent: " + input);
- }*/
- System.out.println("else activated.");
- }
- }
- public synchronized void remove(int ID) {
- int pos = findClient(ID);
- if (pos >= 0) {
- ChatServerThread toTerminate = clients[pos];
- System.out.println("Removing client thread " + ID + " at " + pos);
- if (pos < clientCount - 1) {
- for (int i = pos + 1; i < clientCount; i++) {
- clients[i - 1] = clients[i];
- }
- }
- clientCount--;
- try {
- toTerminate.close();
- } catch (IOException ioe) {
- System.out.println("Error closing thread: " + ioe);
- }
- toTerminate.stop();
- }
- }
- private void addThread(Socket socket) {
- if (clientCount < clients.length) {
- System.out.println("Client accepted: " + socket);
- clients[clientCount] = new ChatServerThread(this, socket);
- try {
- clients[clientCount].open();
- clients[clientCount].start();
- clientCount++;
- } catch (IOException ioe) {
- System.out.println("Error opening thread: " + ioe);
- }
- } else {
- System.out.println("Client refused: maximum " + clients.length + " reached.");
- }
- }
- public static void main(String args[]) {
- TCPServer server = null;
- server = new TCPServer(2000);
- }
- public void registerEntry(String fname, String user, String pass) throws ClassNotFoundException {
- //System.out.println("First Name: " + fname + " User Name:" + user + " Password: " + pass + " Team Name: " + team);
- System.out.println(">>Register hoise from server :D ");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println("Entry hoche");
- Statement st = (Statement) conn.createStatement();
- String insert = "INSERT INTO `users` (`first_name`, `user_name`, `password`) VALUES ('" + fname + "', '" + user + "', '" + pass + "');";
- st.executeUpdate(insert);
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- public String checkLogin(String user) throws ClassNotFoundException {
- String u = null;
- String p = null;
- String ret = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println(">>Connection Established.");
- Statement st = (Statement) conn.createStatement();
- String selQuery = "SELECT `user_name`, `password` FROM `users` WHERE user_name = '" + user + "'";
- ResultSet checkLog = st.executeQuery(selQuery);
- while (checkLog.next()) {
- u = checkLog.getString("user_name");
- p = checkLog.getString("password");
- System.out.println(">>>>Login er jonno client pathaise: Name: " + u + " Password: " + p);
- }
- ret = (u + " " + p);
- System.out.println(">>ret : " + ret);
- } catch (SQLException e) {
- System.err.println(e);
- }
- return ret;
- }
- public void onlineUser(String user) throws ClassNotFoundException {
- //**method for making the user online in DB**
- System.out.println(user + " just came online.");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println(">>Connection Established.");
- Statement st = (Statement) conn.createStatement();
- String upQuery = "UPDATE `users` SET `logged_in`=1 WHERE `user_name` = '" + user + "';";
- int checkLog = st.executeUpdate(upQuery);
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- public void updateTeam(String user, String newTeam) throws ClassNotFoundException {
- //**method for updating team name of that username**
- System.out.println("user: " + user + " newTeam: " + newTeam);
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println(">>Connection Established.");
- Statement st = (Statement) conn.createStatement();
- String upQuery = "UPDATE `users` SET `team_name`='" + newTeam + "' WHERE `user_name` = '" + user + "';";
- int checkLog = st.executeUpdate(upQuery);
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- public void totalOnline() {
- }
- public void offlineEveryone() throws ClassNotFoundException {
- //**Method for offlineing everyone in DB**
- System.out.println("Offlining everyone. >>>>BYE");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println(">>Connection Established.");
- Statement st = (Statement) conn.createStatement();
- String upQuery = "UPDATE `users` SET `logged_in`=0 WHERE `logged_in` = 1;";
- int checkLog = st.executeUpdate(upQuery);
- } catch (SQLException e) {
- System.err.println(e);
- }
- }
- public String getNameById(int ID) {
- String ret = null;
- for (int i = 0; i <= userIdCount; i++) {
- System.out.println("userId[i][0]: " + userId[i][0]);
- if (userId[i][0].equals(Integer.toString(ID))) {
- ret = userId[i][1];
- } else {
- ret = null;
- }
- }
- return ret;
- }
- public int getIdByName(String name) {
- int ret = -1;
- for (int i = 0; i <= userIdCount; i++) {
- if (userId[i][1].equals(name)) {
- ret = Integer.parseInt(userId[i][0]);
- } else {
- ret = -1;
- }
- }
- return ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement