zoukankan      html  css  js  c++  java
  • 蛋疼时候的一些东西

    写代码写不下去了,于是找些奇怪的东西做做,想到java生疏了好多年了,于是就练习了一下java与AS的通讯。

    现在只进行到建立连接,由于一开始建立连接报错,

    Error #2031: Socket Error. URL:

    还以为是AIR不支持Socket呢。

    去Adobe官网看了一下,老外很多遇到这种问题,如连接

    看了半天他们也没有解决方案,于是觉得改一下端口号也许有些帮助,改了两次把端口号改成5050的时候终于好了。

    分别用FD和Flex对服务端进行了连接,结果如下:

    java代码如下:

    View Code
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.HashMap;
    
    
    public class ServerMian 
    {
        private static ServerSocket serve;
        //管理地址与id
        private static HashMap<String, String> clientMap;
        private static BufferedReader in;
        private static PrintWriter out;
        public static void main(String[] args) throws IOException
        {
            initServe();
        }
        //初始化服务器端口
        private static void initServe() throws IOException
        {
            serve = new ServerSocket(5050);
            serve.setReuseAddress(true);
            
            clientMap = new HashMap<String, String>();
            
            System.out.print("Serve Inited...");
            while(true)
            {
                try{
                    Socket s = serve.accept();
                    String key = s.getInetAddress() + ":"+ s.getPort();
                    //todo
                    if(!clientMap.containsKey(key))
                    {
                        
                    }else
                    {
                        
                    }
                    System.out.println("Recieved connect from"+key);
                    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    out = new PrintWriter(s.getOutputStream(),true);
                    System.out.println(in.readLine());
                }catch(IOException e)
                {
                    System.out.println(e.getStackTrace());
                }
                
            }
        }
    }

    as连接测试代码如下:

    View Code
    private function onCreate():void
                {
                    var s:Socket = new Socket();
                    s.connect("localhost",5050);
                    s.addEventListener(Event.CONNECT, onConnected);
                }
                private function onConnected(e:Event):void
                {
                    trace("server connected");
                }
  • 相关阅读:
    4_5.springboot2.x之Web开发RestfulCRUD操作
    4_4.springboot之Web开发登录和拦截器
    4_3.springboot2.x之默认访问首页和国际化
    4_2.springboot2.x配置之springmvc自动配置
    4_1.springboot2.xWeb开发使用thymeleaf
    03_springboot2.x日志处理
    08_springboot2.x自定义starter
    JavaScript-----对象属性赋值及获取属性值的方式
    Spring探究-----AOP配置详解
    Spring探究-----自动装配Bean详解
  • 原文地址:https://www.cnblogs.com/adoontheway/p/3067279.html
Copyright © 2011-2022 走看看