黑马程序员技术交流社区

标题: 自定义服务端,浏览器作为客服端访问出现的问题? [打印本页]

作者: 皓栎    时间: 2014-2-19 21:17
标题: 自定义服务端,浏览器作为客服端访问出现的问题?
/*
1.  客户端:浏览器
     服务端:自定义
*/
class ServerDemo {
        public static void main(String[] args) throws Exception {


                ServerSocket ss = new ServerSocket(11000);
                Socket s = ss.accept();
                System.out.println(s.getInetAddress().getHostAddress());

                PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                out.println("<font color='red' size='7'>客户端你好</font>");

                s.close();
                ss.close();
        }
}

/*
2.  客户端:浏览器
     服务端:自定义(获取给浏览器发送的请求信息)
*/
class ServerDemo2 {
        public static void main(String[] args) throws Exception {


                ServerSocket ss = new ServerSocket(11000);
                Socket s = ss.accept();
                System.out.println(s.getInetAddress().getHostAddress());

                InputStream in = s.getInputStream();
                byte[] buf = new byte[1024];
                int len = in.read(buf);
                System.out.println(new String(buf,0,len));

                PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                out.println("<font color='red' size='5'>客户端你好</font>");

                s.close();
                ss.close();
        }
}

第二个类就比第一个类多一段获取给浏览器发送请求信息的代码。
那为什么在浏览器访问时,一个能访问自定义服务器中的数据,一个不能访问呢?
但看毕老师的视频演示都Ok的,不知道是不是浏览器的原因。















ServerDemo.png (60.51 KB, 下载次数: 7)

ServerDemo编译运行结果:

ServerDemo编译运行结果:

ServerDemo2.png (111.65 KB, 下载次数: 10)

ServerDemo2编译运行结果:

ServerDemo2编译运行结果:





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2