/*我在这个程序中编写了一个服务器端和一个客户端,服务器端和客户端各建立了一个对话窗口,当往各自的文本区域输入时,就会显示到对方的控制台。但结果却无法显示到控制台,还抛出异常。请哪位高人指点一二,小弟不胜感激
*/
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

class jianpan extends JFrame{

public jianpan(){
super("keyboard action");
Container c=getContentPane();
c.setLayout(new FlowLayout());
JTextField s=new JTextField(20);
s.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e){
try{
ServerSocket server=null;
server=new ServerSocket(5000);
Socket ss=server.accept();
DataOutputStream dataout=new DataOutputStream(ss.getOutputStream());
dataout.writeUTF("hello"+e.getKeyChar());
}
catch(Exception x){
x.printStackTrace();
}

}

});
c.add(s);
}
}

public class Jsever{
public static void main(String[] args){
try{
ServerSocket server=null;
server=new ServerSocket(5000);
jianpan aaa=new jianpan();
aaa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aaa.setSize(400,400);
aaa.setVisible(true);
while(true){
Socket ss=server.accept();
System.out.println("服务器端接收到来自客户端的链接");
DataOutputStream dataout=new DataOutputStream(ss.getOutputStream());
DataInputStream datain=new DataInputStream(ss.getInputStream());
dataout.writeUTF("hello,welcome to my computer");
String duihua=new String("datain.readUTF()");
for(;true;){
while(duihua!="datain.readUTF()"){
System.out.println("Jclient"+datain.readUTF());
duihua="datain.readUTF()";
}
}
}
}
catch(Exception e){
System.out.println("发生异常:"+e);
e.printStackTrace();
}
}
}

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class jianpan extends JFrame{

public jianpan(){
super("keyboard action");
Container c=getContentPane();
c.setLayout(new FlowLayout());
JTextField s=new JTextField(20);
s.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
try{
Socket sx=new Socket("localhost",5000);
DataOutputStream dataout=new DataOutputStream(sx.getOutputStream());
dataout.writeUTF("hi"+e.getKeyChar());
}
catch(Exception x)
{x.printStackTrace();
}
}});
c.add(s);
}
}



public class Jclient{
public static void main(String[] args){
try{
Socket s=new Socket("localhost",5000);
DataInputStream datain=new DataInputStream(s.getInputStream());
jianpan abc=new jianpan();
abc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
abc.setSize(400,400);
abc.setVisible(true);
String duihua=new String("datain.readUTF()");
for(;true;){
while(duihua!="datain.readUTF()"){
System.out.println("客户端收到"+datain.readUTF());
duihua="datain.readUTF()";
}
}

}
catch(Exception e){
System.out.println("发生异常"+e);
e.printStackTrace();
}
}
}