130 likes | 291 Views
CS 471/571. Sockets 8. MsgCode. public class MsgCodes { final static int QUIT = 0; final static int GETNAME = 1; final static int INSERT = 2; final static int DELETE = 3; final static int FAIL = 0; final static int SUCCEED = 1; }. Data Store. import java.sql.*;
E N D
CS 471/571 Sockets 8
MsgCode public class MsgCodes { final static int QUIT = 0; final static int GETNAME = 1; final static int INSERT = 2; final static int DELETE = 3; final static int FAIL = 0; final static int SUCCEED = 1; }
Data Store import java.sql.*; public class DataStore { Connection con; public DataStore(String db) { try { con = DriverManager.getConnection("jdbc:sqlite:"+db); } catch (SQLException e) {System.out.println(e.getMessage());} } public synchronized String getName(int id) { try { Statement s = con.createStatement(); ResultSet r = s.executeQuery("select Name from Users where uid = " +id); if (r.next()) return r.getString(1); } catch (SQLException e) {System.out.println(e.getMessage());} return null; }
Data Store public synchronized boolean insert(int id, String name) { try { Statement s = con.createStatement(); s.executeUpdate("insert into Users values ("+id+", '"+name+"')"); return true; } catch (SQLException e) {return false;} } public synchronized boolean delete(int id) { try { Statement s = con.createStatement(); s.executeUpdate("delete from Users where uid = "+id); return true; } catch (SQLException e) {return false;} } }
Server import java.net.*; import java.util.*; public class Server implements Runnable { Socket client; DataStore data; public Server(Socket s, DataStore d) { client = s; data = d; }
Server public void run() { try { BufferedReader inSock = new BufferedReader( new InputStreamReader(client.getInputStream())); PrintStream outSock = new PrintStream( client.getOutputStream()); Scanner line = new Scanner(inSock.readLine()); int oper; while ((oper = line.nextInt()) != MsgCodes.QUIT) { if (oper == MsgCodes.GETNAME) { int id = line.nextInt(); String name = data.getName(id); if (name == null) outSock.println(""+MsgCodes.FAIL); else outSock.println(""+MsgCodes.SUCCEED+' '+name); }
Server else if (oper == MsgCodes.INSERT ) { int id = line.nextInt(); String name = line.next(); boolean rcode = data.insert(id, name); if (rcode) outSock.println(""+MsgCodes.SUCCEED); else outSock.println(""+MsgCodes.FAIL); }
Server else { int id = line.nextInt(); boolean rcode = data.delete(id); if (rcode) outSock.println(""+MsgCodes.SUCCEED); else outSock.println(""+MsgCodes.FAIL); } line = new Scanner(inSock.readLine()); } client.close(); } catch (Exception e) { } }
Server public static void main(String[] args) throws Exception { DataStore d = new DataStore("names2.db"); ServerSocket mySock = new ServerSocket(33000); while (true) { Socket client = mySock.accept(); Server s = new Server(client, d); Thread t = new Thread(s); t.start(); } } }
Client import java.io.*; import java.net.*; import java.util.*; public class Client { public static void main(String[] args) throws Exception { Socket mySock = new Socket("localhost", 33000); BufferedReader inSock = new BufferedReader( new InputStreamReader(mySock.getInputStream())); PrintStream outSock = new PrintStream( mySock.getOutputStream()); Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int oper = in.nextInt(); if (oper == MsgCodes.QUIT) { outSock.println(""+MsgCodes.QUIT); break; }
Client else if (oper == MsgCodes.GETNAME ){ int id = in.nextInt(); outSock.println(""+MsgCodes.GETNAME+' '+id); Scanner ret = new Scanner(inSock.readLine()); int rcode = ret.nextInt(); if (rcode == MsgCodes.FAIL) System.out.println(""+id+" not found"); else System.out.println(""+id+"(Name): "+ret.next()); }
Client int id = in.nextInt(); String name = in.next(); outSock.println(""+MsgCodes.INSERT+' '+id+' '+name); Scanner ret = new Scanner(inSock.readLine()); int rcode = ret.nextInt(); if (rcode == MsgCodes.FAIL) System.out.println("row not added"); else System.out.println("row added"); }
Client else { int id = in.nextInt(); outSock.println(""+MsgCodes.DELETE+' '+id); Scanner ret = new Scanner(inSock.readLine()); int rcode = ret.nextInt(); if (rcode == MsgCodes.FAIL) System.out.println("row not deleted"); else System.out.println("row deleted"); } } mySock.close(); } }