java socket简单使用

关于点击按钮取不到数据,并返回错误信息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();

        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,892评论 25 709
  • 最近在学习Python看了一篇文章写得不错,是在脚本之家里的,原文如下,很有帮助: 一、网络知识的一些介绍 soc...
    qtruip阅读 7,696评论 0 6
  • 开始上课,全神贯注听讲的孩子们 老师用硬币魔术导入课程主题——认识钱 老师提出有关钱的问题,引发孩子们讨论
    柳硕_3f86阅读 1,618评论 0 0
  • 因为怀孕生儿育儿,算下来,至少3年没什么运动了,最多也就是带儿子散散步,运动量比我家不到两岁的小北都要低很多。 久...
    zz茉雨阅读 3,517评论 0 1