问题1:用什么流将客户端传来的list集合传输到服务器端
代码如下:
public class Clientimpl implements Client {
private String host = "127.0.0.1";
private int port = 9999;
@Override
public void send(Collection c) throws Exception {
// 设置端口号,建立连接
Socket socket=null;
// 对象流传出list集合对象
ObjectOutputStream oos = null;
try {
socket= new Socket(host, port);
//运用对象流输出list对象到服务器端
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(c);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
oos.close();
}
if (socket!=null) {
socket.close();
}
}
}
}
服务器端:
问题1:实际项目中也需要进行服务的关闭,没有运用到多线程的方式来读取多个客户端发来的list集合。
问题2:开发中要在服务器端实现入库的操作,将list的数据写入到oracle数据库中需要调用一个入库的方法。
public class Serverimpl implements Server {
private int reciverPort=9999;
private int shutdownPort=8888;
private DBStore dbStrore = new DBStoreimpl();
private volatile boolean flag=true;
@Override
public void reciver() throws Exception {
System.out.println("Shutdown 服务器启动成功"+shutdownPort);
startShutdownServer();
System.out.println("Reciver服务器启动成功"+reciverPort);
startReciver();
}
public void startReciver(){
ServerSocket reciverServerSocket=null;
Socket reciveSocket=null;
ObjectInputStream ois=null;
try {
reciverServerSocket= new ServerSocket(reciverPort);
while(flag) {
reciveSocket = reciverServerSocket.accept();
ois = new ObjectInputStream(reciveSocket.getInputStream());
Collection list = (Collection) ois.readObject();
dbStrore.saveDB(list);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(reciverServerSocket!=null) {
try {
reciverServerSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void startShutdownServer() {
new Thread(new Runnable() {
@Override
public void run() {
ServerSocket shutdownserverSocket=null;
Socket shutdownSocket=null;
try {
shutdownserverSocket = new ServerSocket(shutdownPort);
shutdownSocket= shutdownserverSocket.accept();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (shutdownSocket != null) {
try {
shutdownSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (shutdownserverSocket != null) {
try {
shutdownserverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
@Override
public void shutdown() throws Exception {
this.flag=false;
}
}



