cs 471 571 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS 471/571 PowerPoint Presentation
Download Presentation
CS 471/571

Loading in 2 Seconds...

play fullscreen
1 / 13

CS 471/571 - PowerPoint PPT Presentation


  • 142 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 471/571' - sheng


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 471 571

CS 471/571

Sockets 8

msgcode
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
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 store1
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
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;

}

server1
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);

}

server2
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);

}

server3
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) { }

}

server4
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
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;

}

client1
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());

}

client2
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");

}

client3
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();

}

}