Socket中PrintStream,PrintWriter的print无法被readUTF获取

发现一个问题

Socket编程中要慎用

PrintStream和PrintWriter,首先创建时要注意加上 true,自动flush,否则数据无法发送出去,

其次它们的println()要用它们的readLine来读取,如果你用用DataInputStream.readUTF读取将导致阻塞,一直都读取不到数据,见下面代码就是。

package com.chat.server.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException
    {
        InputStream is = null;
        DataInputStream dis = null;
        OutputStream os = null;
       // DataOutputStream dos = null;
        PrintStream pos = null;
        ServerSocket ss = new ServerSocket(10001);
        try
        {
            while(true)
            {
                Socket soc = ss.accept();
                is = soc.getInputStream();
                dis = new DataInputStream(is);
                os = soc.getOutputStream();
               // dos = new DataOutputStream(os);
                pos = new PrintStream(os,true);
                try
                {
                    InetAddress clientAddr = soc.getInetAddress();
                    String str = "欢迎客户" + clientAddr + "到访,该客户占用端口"
                            + soc.getPort() ;
                    pos.print(str+"\r\n\n");
                    //pos.flush();
                    //dos.writeUTF(str);
                   // dos.flush();
                    str = dis.readUTF();
                    while (!"quit".equals(str))
                    {
                        System.out.println("client said:" + str);
                        str = dis.readUTF();
                    }
                    System.out.println(clientAddr + " leave");
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    is.close();
                    os.close();
                    soc.close();
                }
            }
        }
        finally
        {
            if(ss != null){
                ss.close();
            }
        }

    }

}

 

 

package com.chat.server.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class Client
{

    public static void initArray(byte[] arr)
    {
        Arrays.fill(arr, (byte)0);
    }
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException
    {
        InputStream is = null;
        DataInputStream dis = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        Socket soc = new Socket();
        InetAddress address = InetAddress.getLocalHost();
        InetSocketAddress endpoint = new InetSocketAddress(address,10001);
        soc.connect(endpoint);
        byte bmsg[] = new byte[1024];
        initArray(bmsg);
        String msg = null;
        try
        {
            is = soc.getInputStream();
            dis = new DataInputStream(is);
            os = soc.getOutputStream();
            dos = new DataOutputStream(os);
            String str = dis.readLine();
           //String str = dis.readUTF();
            System.out.println("Server said:"+ str);
            int num = System.in.read(bmsg);
            msg = new String(bmsg,0,num);
            msg = msg.substring(0,msg.length() -2);
            while(!"quit".equals(msg))
            {
                dos.writeUTF(msg);
                dos.flush();
                num = System.in.read(bmsg);
                msg = new String(bmsg,0,num);
                msg = msg.substring(0,msg.length() -2);
            }
            dos.writeUTF(msg);
            dos.flush();
        }
        finally
        {
            if(soc != null)
            {
                soc.close();
            }
        }
        

    }

}

 如果Client中的dis.readLine改成dis.readUTF,则会在dis.readUTF一直阻塞。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值