栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用java实现接收list类和传输list类到服务器端

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用java实现接收list类和传输list类到服务器端

1.客户端的建立

问题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;
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号