发现一个问题
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();
- }
- }
- }
- }