简易的QQ我想问下为什么在qqclient中writeUTF的字符
在server端可以用readUTF读取
而client端的readUTF可以读取server端的writeUTF
有点没有憋过来劲请各位大虾详细讲解
Clientimport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;
import java.net.*;public class QQClient
{
public static void main(String [] args) {
EventQueue.invokeLater(new Runnable(){
public void run (){
new QQFrame();
}
});
}
}
class QQFrame extends JFrame
{
static int i = 0;
private JTextArea output = null;
private JTextField input  = null;

private DataInputStream in = null;
private DataOutputStream out = null;

private Jion  j = null;

public QQFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension ds = kit.getScreenSize();
int x = ds.width;
int y = ds.height;

setLocation(x / 4, y / 4);
output = new JTextArea(x/64,y/32); JScrollPane jp = new JScrollPane(output); input = new JTextField();
output.setEnabled(false);

input.addActionListener(new OutputActionListener());
add(jp,BorderLayout.CENTER);
add(input,BorderLayout.SOUTH); addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event){
setVisible(false);

if(j != null)
j.close(); //在退出的时候把所有的线程和应用程序结束

System.exit(0);
}
});

pack();
setVisible(true);


j = new Jion();//开始一个客户端的工作
j.start();
i++;
}
private class OutputActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event){

String str  = "";

try
{
str = input.getText();
out.writeUTF(str+"\n");//把字符写入输出流由Server端读取并写给每个客户端,这里的输出是给别的程序读入的。。
//比如这里的输出的时给Server中的读取的,即这里的writeUTF对应的是Server中的readUTF
out.flush();
}
catch (IOException e)
{
System.out.println("读/写错误.");
}
input.setText("");
}
} private class Jion
{

private Socket s = null;
private boolean b = false;
private FlushTextArea f = null;
public Jion(){}

public void start()
{
try
{
s = new Socket("127.0.0.1",6669);  //连接服务器
in = new DataInputStream(s.getInputStream());
out = new DataOutputStream(s.getOutputStream());
b = true ;

f = new FlushTextArea();
Thread t = new Thread(f);
t.start(); }
catch (IOException e)
{
System.out.println("未连接上服务器");
}
}
public void close()
{

try
{
out.close();
s.close();
}
catch (Exception e)
{
;
}
} private class FlushTextArea implements Runnable //因为readUTF是一个堵塞的方法必须用一个线程,来监听此相应。
{
public void run()
{
try
{
String str = null; while(b){
str = in.readUTF(); //读取Server端写入的字符
output.setText(output.getText()+""+str+new Date()+"\n"); //在程序被读入Server端的时候,由Server端写入到本客户端,而不是由此客户端直接写入到本客户端 }
System.out.println(str+""+b);
}
catch (Exception e)
{
;
}
}
} }
}
Server端
QQServer
import java.net.*;
import java.io.*;
import java.util.*;public class QQServer
{
private boolean jionServer = false;
private ArrayList<Thread> arr = null;
private ArrayList<Jion>   arrj = null;
private HashMap<Jion,Thread> hs = null;
public static void main(String [] args)
{
new QQServer().start(); 
}
public QQServer(){}
private void start()
{
ServerSocket ss = null;
arr = new ArrayList<Thread>();
arrj = new ArrayList<Jion>();
hs = new HashMap<Jion,Thread>();
try
{
ss = new ServerSocket(6669);
jionServer = true; while (jionServer)
{
Jion j = new Jion(ss.accept());
Thread t = new Thread(j);
arrj.add(j);
arr.add(t);
hs.put(j,t);
t.start();
System.out.println("succeed/successful"); }
}
catch (IOException e)
{
System.out.println("端口被占用.");
}
finally
{
try
{
Iterator<Thread> it = arr.iterator();
while(it.hasNext())
{
Thread t = it.next();
t.interrupt();
it.remove();
}
ss.close();
}
catch (IOException e)
{
System.out.println("关闭错误.");
}
}
}

private class Jion implements Runnable
{
private DataInputStream in = null;
private DataOutputStream out = null;
private boolean clientServer = false; public Jion(Socket ss)
{
if(ss != null)
{
try
{

in = new DataInputStream(ss.getInputStream());
out = new DataOutputStream(ss.getOutputStream());

clientServer = true;
}
catch (IOException e)
{
;
}

}
}
public void close()
{
try
{
in.close();
out.close();
}
catch (IOException e)
{
;
}
}
public void send(String str)
{
try
{
System.out.println(str);
out.writeUTF(str);   //把字符串写入到客户端的输出流
}
catch (Exception e)
{
arrj.remove(this);
Thread t = hs.remove(this);
arr.remove(t);
t.interrupt();
}
}
public void run()
{

try
{
while(clientServer)
{



String str = in.readUTF();//读取冲客户端写入的字符串。

for(int i = 0;i < arrj.size();i++)
{

//String str = in.readUTF();//写在这里的时候会出错。
Jion j = arrj.get(i);
j.send(str);
} out.flush(); }
close();
}
catch (IOException e)
{
System.out.println("wrong!");
}
}
}
}

解决方案 »

  1.   

    通常服务器端的readUTF读的是客户端的输出流,服务器在接受到每个客户端的请求时都建立一个通道(流),(你程序中这么写的 Jion j = new Jion(ss.accept()); hs.put(j,t));服务器给客户端发消息(即writeUTF)时将信息通过这个通道发给客户端,在client端可以用readUTF读取通道的输出流信息
    你程序的下列代码就是这个意思
    server端
     in = new DataInputStream(ss.getInputStream());//ss.getInputStream())得到的是客户端发送的输出流
     out = new DataOutputStream(ss.getOutputStream());
     String str = in.readUTF();client端的
     out.writeUTF(str+"\n");
     是写入输出通道以上是初学者的一些薄解 有什么问题 还请多多指教
      

  2.   

    谢谢...我自己琢磨了好久..
    但是为什么InputStream读取的事一个输出流...inputstream不是一个输入流么