1 / 13

CS 471/571

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.*;

sheng
Télécharger la présentation

CS 471/571

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 471/571 Sockets 8

  2. 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; }

  3. 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; }

  4. 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;} } }

  5. 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; }

  6. 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); }

  7. 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); }

  8. 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) { } }

  9. 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(); } } }

  10. 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; }

  11. 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()); }

  12. 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"); }

  13. 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(); } }

More Related