关于点击按钮取不到数据,并返回错误信息500.
调试方法,将方法全部注释,一个一个调试,并在方法内插入断点;
最终将错误定位在service
仔细查看后发现是service实现层没有使用注册装配到spring中导致在control中使用自动装配失败;
下午了解了使用套接字实现的客户端;
客户端主要实现socket的连接和断开。
当客户端与服务端建立连接,就用打开一条传输通道,socket有专门用来收发数据的方法,我们只需要将发送数据转换为二进制数据即可发送,接受数据将数据转成string即可。
一个客户端只能建立一条通道,但服务器可以与多台客户端建立连接。
一般当应用关闭时断开socket连接。
关于用户或者客户端的数据接收,使用监听方法,等待数据后显示;
socket的本质就是一个java封装的数据对象,可以用来进行网络传输
简单案例
服务端
public class MySocketServer {
public static void main(String[] args) throws Exception {
//启动服务
final ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("localhost",8888));
System.out.println("server starting");
//阻塞接受数据,单开线程处理数据
while (true) {
Socket socket = serverSocket.accept();
new Thread(new SocketService(socket)).start();
}
}
}
服务端业务处理
public class SocketService implements Runnable{
public interface SellService {
Integer sell(Integer money);
}
public static class SellServiceImpl implements sellAction.SellService {
public Integer sell(Integer money) {
return money;
}
}
private Socket socket;
InputStream inputStream = null;
OutputStream outputStream = null;
public SocketService(Socket socket) {
this.socket = socket;
}
public void run(){
System.out.println("server线程启动");
try {
inputStream=socket.getInputStream();
outputStream=socket.getOutputStream();
//按照协议读取数据
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
final String message = bufferedReader.readLine();
System.out.println("server recieve"+message);
//如果值不为空,完成对应业务逻辑
if (!message.isEmpty()) {
final String[] splits = message.split(",");
String serviceName = splits[0].substring(splits[0].indexOf(":")+1);
String[] params = splits[1].substring(splits[1].indexOf("[")+1,splits[1].indexOf("]")).split(",");
System.out.println(SellServiceImpl.class);
System.out.println(serviceName);
final Class<?> aClass = Class.forName(serviceName);
final Object o = aClass.newInstance();
final Method sell = aClass.getMethod("sell",Integer.class);
final Integer result = (Integer)sell.invoke(o, Integer.parseInt(params[0]));
//按照协议返回值
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.println(result);
printWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端
public class MySocketClient {
public static void main(String[] args) throws Exception {
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream=null;
try {
//连接socket socket本质就是协议字符串
socket = new Socket("localhost",8888);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
//按照协议发送数据
final PrintStream printStream = new PrintStream(outputStream);
printStream.println("{service:day05.SocketService$SellServiceImpl,params:[10]}");
printStream.flush();
//按照协议读取返回值
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
final String result = bufferedReader.readLine();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}finally {
inputStream.close();
outputStream.close();
socket.close();
}
}
}