qq_昼绽_0
2016-11-05 16:46
package com.imooc.thread; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import com.jinxin.entity.File; import com.jinxin.entity.User; import com.jinxin.service.FileService; import com.jinxin.service.UserService; import com.jinxin.util.CommandTransfer; public class ServerThread extends Thread { //和本线程相关的socket Socket socket; public ServerThread (Socket socket){ this.socket =socket; } public void run(){ //3.创建输入流,并读取客户端信息 try { InputStream is = socket.getInputStream(); ObjectInputStream ois = new ObjectInputStream(is);//这是28行 CommandTransfer com=null; try { com = (CommandTransfer)ois.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } operate(socket,com); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void operate(Socket socket ,CommandTransfer com) throws IOException{ if(com.getCmd().equals("select")){ UserService userService = new UserService(); if(userService.ckeckout((User)com.getData())){ socket.shutdownInput(); OutputStream os = socket.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); com.setFlag(true); oos.writeObject(com); oos.flush(); socket.shutdownOutput(); }else{ socket.shutdownInput(); OutputStream os = socket.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); com.setFlag(false); oos.writeObject(com); oos.flush(); socket.shutdownOutput(); } } if(com.getCmd().equals("register")){ UserService userService = new UserService(); if(userService.ckeckout((User)com.getData())){ socket.shutdownInput(); OutputStream os = socket.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); com.setFlag(false); oos.writeObject(com); oos.flush(); socket.shutdownOutput(); }else{ userService.register((User)com.getData()); socket.shutdownInput(); OutputStream os = socket.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); com.setFlag(true); oos.writeObject(com); oos.flush(); socket.shutdownOutput(); } } if(com.getCmd().equals("upload")){ FileService fileService = new FileService(); fileService.upload((File)com.getData()); socket.shutdownInput(); } } }
这是项目地址:http://www.imooc.com/opus/resource?opus_id=3426
先启动服务器,再启动客户端。
请问下是什么原因造成的?卡在这边过不去了,一直没找到原因
求告知怎么解决的?我也遇到这个问题
Java Socket应用---通信是这样练成的
125012 学习 · 590 问题
相似问题