50,526
社区成员
发帖
与我相关
我的任务
分享
package cfsServer;
public class SearchModel implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private String sql;
private String[] paras;
public SearchModel(String sql, String[] paras) {
this.sql = sql;
this.paras = paras;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String[] getParas() {
return paras;
}
public void setParas(String[] paras) {
this.paras = paras;
}
}
package cfsServer;
import java.net.*;
import java.util.logging.Logger;
public class CfsServer {
private final static Logger logger = Logger.getLogger(CfsServer.class.getName());
public CfsServer(){
ServerSocket ss=null;
try {
System.out.println("服务器启动。。");
int count=0;//记录连接的客户端数量
ss=new ServerSocket(9999);
Socket socket=null;
while (true){
socket=ss.accept();
++count;
Thread serverHandleThread=new Thread(new ServerHandleThread(socket));
serverHandleThread.setPriority(4);
serverHandleThread.start();
System.out.println("上线的客户端有" + count + "个!");
InetAddress inetAddress = socket.getInetAddress();
System.out.println("当前客户端的IP地址是:"+inetAddress.getHostAddress());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CfsServer();
}
}
package cfsServer;
import java.net.*;
import java.io.*;
public class ServerHandleThread implements Runnable {
Socket socket=null;
public ServerHandleThread(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
OutputStream os = null;
PrintWriter pw = null;
try {
//先接收客户端的信息
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String type=br.readLine();
System.out.println("客户端发送了"+type);
switch (type){
case "table":
this.inTable(socket);
break;
case "count":
this.inCount(socket);
break;
case "execute":
this.updExecute(socket);
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
if(pw!=null){
pw.close();
}
if(os!=null){
os.close();
}
if(socket!=null){
socket.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void inTable(Socket socket) throws Exception{
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
try {
InputStream is = socket.getInputStream();
ois=new ObjectInputStream(is);
oos=new ObjectOutputStream(socket.getOutputStream());
Object obj=ois.readObject();
System.out.println("接收到客户端发的search对象");
SearchModel sm=(SearchModel)obj;
System.out.println("转换完成");
TableModel tm=new TableModel();
oos.writeObject(tm.addData(sm));
oos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if(oos!=null){
oos.close();
}
if(ois!=null){
ois.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.cfs.model;
import cfsServer.DataModel;
import cfsServer.SearchModel;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class QueryModel {
Socket socket=null;
OutputStream os=null;
ObjectOutputStream oos=null;
ObjectInputStream is=null;
PrintWriter pw=null;
BufferedReader br=null;
private final static Logger logger = Logger.getLogger(QueryModel.class.getName());
//向服务器发送sql并要求返回表格模型
public DataModel addData(String sql,String[] paras){
SearchModel sm=new SearchModel(sql,paras);
DataModel dm=null;
try {
connect();
pw.println("table");
sendObject(sm);
is=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
dm=(DataModel)is.readObject();
System.out.println("接收到数据模型");
} catch (Exception e) {
e.printStackTrace();
}finally {
closeConnect();
}
return dm;
}
//连接服务器,并获得socket
public void connect(){
try {
socket=new Socket("127.0.0.1",9999);
pw=new PrintWriter(socket.getOutputStream(),true);
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
public void closeConnect(){
try {
if (oos!=null){
oos.close();
}
if (os!=null){
os.close();
}
if (is!=null){
is.close();
}
if (br!=null){
br.close();
}
if (pw!=null){
pw.close();
}
if (socket!=null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//发送sql模型
public void sendObject(SearchModel sm){
try {
os=socket.getOutputStream();
oos=new ObjectOutputStream(os);
oos.writeObject(sm);
System.out.println("发送search对象");
socket.shutdownOutput();
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
}
}
}