黑马程序员技术交流社区

标题: 搞了半天不知道问题出在哪了 来几个大神指点迷津 [打印本页]

作者: 只吃饭不洗碗    时间: 2015-4-28 11:13
标题: 搞了半天不知道问题出在哪了 来几个大神指点迷津
自定义了一个服务端,用浏览器客户端访问,服务端能够读到客户端发的消息,可是客户端却读不到服务器的消息,话说刚开始是可以读到的,后来不知道为什么读不到了,什么也没改。

package com.itheimaexercise.day24;
import java.io.*;
import java.net.*;
public class ServerTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                ServerSocket serverSocket = null;
                try{
                        serverSocket = new ServerSocket(8977);
                }
                catch(IOException e){
                        e.printStackTrace();
                }
               
                Socket clientSocket = null;
                try{
                        clientSocket = serverSocket.accept();
                }
                catch(IOException e){
                        e.printStackTrace();
                }
                String ip = clientSocket.getInetAddress().getHostAddress();
                System.out.println(ip+"......connected");
               
                //看看浏览器客户端访问服务器的时候都说了什么:
                BufferedInputStream bufIn = null;
                try{
                        bufIn = new BufferedInputStream(clientSocket.getInputStream());
                }
                catch(IOException e){
                        e.printStackTrace();
                }
               
                byte[] buf = new byte[1024];
                int len = 0;
                try{
                        while((len=bufIn.read(buf))!=-1){
                                System.out.println(new String(buf,0,len));
                        }
                }
                catch(IOException e){
                        e.printStackTrace();
                }
               
               
               
                BufferedWriter bufOut = null;
                try{
                        bufOut =
                                        new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
                }
                catch(IOException e){
                        e.printStackTrace();
                }
               
                try{
                        bufOut.write("<font color='red' size=6>欢迎你客户端");
                        bufOut.newLine();
                        bufOut.flush();
                }
                catch(IOException e){
                        e.printStackTrace();
                }
               
                finally{
                        try{
                                clientSocket.close();
                        }
                        catch(IOException e){
                                e.printStackTrace();
                        }
                }
               
               
        }

}



作者: 只吃饭不洗碗    时间: 2015-4-28 11:35
没有人来呢? 刚试了下 自己搞明白了,原来又是阻塞了  




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