Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication12;
- import com.mysql.jdbc.*;
- import java.net.*;
- import java.io.*;
- import java.sql.SQLException;
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import sun.misc.CEFormatException;
- public class ClientThread extends Thread {
- public static String msg;
- Server source;
- Socket connection;
- InputStream inStream;
- DataInputStream inDataStream;
- OutputStream outStream;
- DataOutputStream outDataStream;
- String message;
- String userInput;
- PrintWriter printWriter = null;
- BufferedReader bufferedReader = null;
- ClientThread ( Socket socket, Server s, int id) {
- super ();
- connection = socket;
- source = s;
- this.id=id;
- } // end constructor
- int id;//=-1;
- String name;
- private void printMessage(String message)
- {
- System.out.println(message);
- }
- public void run () {
- String stringIn, stringOut;
- boolean again = true;
- InetAddress inet = connection.getInetAddress ();
- String origin = inet.getHostName ();
- int originport = connection.getPort ();
- System.out.println("Adding Client: "+origin+":"+originport);
- while(true)
- try {
- outStream = connection.getOutputStream ();
- outDataStream = new DataOutputStream ( outStream );
- inStream = connection.getInputStream ();
- inDataStream = new DataInputStream ( inStream );
- message = inDataStream.readUTF ();
- if (message.contains("name"))
- {
- name = message.substring(5);
- id=source.registerPlayer(name);
- System.out.println("Player "+name+" id: " +id );
- outDataStream.writeUTF ( "Player "+name+" id: "+id );
- if(source.game==null)
- { if((source.p[0]!=null)&&(source.p[1]!=null))
- source.startGame();
- }
- }
- else if (message.contains("login"))
- {
- String lgn;
- String pass;
- //lgn = message.substring(6);
- int c=0;
- for(int k = 0; k < message.length(); k++)
- {if (message.charAt(k)==';')
- { c = k;
- break;
- }
- }
- lgn = message.substring(0,c);
- pass = message.substring(c+1);
- lgn=lgn.substring(6);
- pass=pass.substring(9);
- PreparedStatement pstat = null;
- ResultSet rs = null;
- String url = "jdbc:mysql://localhost:3306/users";
- try {
- Connection con = (Connection) DriverManager.getConnection(
- url,"root", "password");
- //Statement stmt;
- //stmt = (Statement) con.createStatement();
- pstat = (PreparedStatement) con.prepareStatement("select login,password from users where login='"+ lgn + "' and password = '"+pass+ "'");
- //pstat.setString(1,lgn);
- //pstat.setString(2,pass);
- rs = pstat.executeQuery();
- if(!rs.next() && rs.getRow() == 0) {
- outDataStream.writeUTF ("Wrong login and/or password.");
- source.t[id]=null;
- try {
- this.finalize();
- } catch (Throwable ex) {
- Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else{
- id=source.registerPlayer(lgn);
- System.out.println("Player "+lgn+" id: " +id );
- outDataStream.writeUTF ( "Player "+lgn+" id: "+id );
- if(source.game==null)
- { if((source.p[0]!=null)&&(source.p[1]!=null))
- source.startGame();
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else
- if (message.contains("all"))
- {
- String msg;
- if (message.length()>3)
- msg = message.substring(4);
- else msg=" ";
- if(this.name!=null)
- if(this.id!=-1)
- msg+=" From: "+name+" id:"+id;
- outDataStream.writeUTF (msg);
- source.sendAll(msg);
- }
- else
- if (message.contains("board"))
- {
- String board;
- board = message.substring(6);
- source.game.board.setTable(board);
- source.game.pass=0;
- int id2;
- if(id==0)
- id2=1;
- else
- id2=0;
- OutputStream outStream = source.t[id2].connection.getOutputStream ();
- DataOutputStream outDataStream = new DataOutputStream ( outStream );
- outDataStream.writeUTF (message);
- String msg="play";
- outDataStream.writeUTF (msg);
- outDataStream.writeUTF (msg);
- source.sendAll("gra "+ source.p[id2].name);
- }
- else if (message.contains("pass"))
- {
- source.game.pass++;
- if(source.game.pass==2)
- { source.sendAll("Koniec gry. Nikt nie wygrał");
- }
- else{
- int id2;
- if(id==0)
- id2=1;
- else
- id2=0;
- OutputStream outStream = source.t[id2].connection.getOutputStream ();
- DataOutputStream outDataStream = new DataOutputStream ( outStream );
- outDataStream.writeUTF (message);
- String msg="play";
- outDataStream.writeUTF (msg);
- source.sendAll("gra "+ source.p[id2].name);
- }
- }
- else if (message.contains("win"))
- { String winner;
- winner = message.substring(4);
- source.sendAll("Koniec gry. Wygrywa "+ source.p[Integer.parseInt(winner)].name);
- }
- else
- {
- System.out.println(" Message, below, received" );
- System.out.println( message );
- // outDataStream.writeUTF ( message+" server" );
- // System.out.println( " Message returned to client");
- source.sendAll(msg);
- System.out.println( " Message sent to all");
- }
- } // end try
- catch ( EOFException except ) {
- System.out.println( " Connection closed by Client\n\n");
- try {
- source.t[id]=null;
- source.p[id]=null;
- int id2;
- if(id==0)
- id2=1;
- else
- id2=0;
- if(source.t[id2]!=null)
- {OutputStream outStream = source.t[id2].connection.getOutputStream ();
- DataOutputStream outDataStream = new DataOutputStream ( outStream );
- outDataStream.writeUTF ("Koniec. Przeciwnik zakończył grę");
- }
- source.game = null;
- connection.close ();
- return;
- }
- catch ( IOException e ) {
- e.printStackTrace ();
- return;
- } // end IOException
- } // end catch EOFException
- catch ( IOException e ) {
- System.out.println( " Connection closed abormally\n" );
- e.printStackTrace ();
- return;
- } // end catch IOException
- } // end run
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement