JAVA 双向通讯 我在自己电脑上 运行 服务器发出消息 客户端收不到
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.omg.CORBA.portable.OutputStream;
@SuppressWarnings("serial")
public class Lt extends JFrame{
JFrame b = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton("发送");
JTextArea jt = new JTextArea();
JTextArea jt1 = new JTextArea();
Lt() throws Exception{
b.setBounds(100,100,500,600);
b.setResizable(false);
b.setLayout(null);
p.setBounds(0, 300,500,100);
p.setLayout(null);
p.setBackground(Color.GREEN);
// p.setLayout(new BorderLayout());
b.add(p);
jb.setForeground(Color.red);
jb.setBounds(400,12,85, 80);
p.add(jb);
jt.setBounds(16,5,460,300);
b.add(jt);
jt1.setBounds(0,405,490,180);
b.add(jt1);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
ServerSocket set = new ServerSocket(8080);
Socket s = set.accept();
InputStream in = s.getInputStream();
java.io.OutputStream out = s.getOutputStream();
new Shuru(in,this).start();
new Shuchu(out,this).start();
}
public static void main(String[] args) throws Exception{
new Lt();
}
}
服务器端主程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Shuru extends Thread {
InputStream in;
Lt LL;
String str = "";
public Shuru(InputStream in,Lt LL){
this.in = in;
this.LL=LL;
LL.b.add(LL.jt);
}
public void run(){
InputStreamReader in1 = new InputStreamReader(in);
BufferedReader buf = new BufferedReader(in1);
while(true){
try {
String str = buf.readLine();
LL.jt.append("客户:"+str+'\n');
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
服务器输入流,
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class Shuchu extends Thread {
OutputStream in;
Lt LL;
String str = "";
public Shuchu(OutputStream in,Lt LL){
this.in = in;
this.LL=LL;
LL.b.add(LL.jt1);
LL.b.add(LL.jb);
}
public void run(){
LL.jb.setFocusable(true);
LL.jb.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
String str = LL.jt1.getText();
PrintWriter p = new PrintWriter(in,true);
p.print(str);
LL.jt1.setText("");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}});
}
}
服务器输出流,
import java.awt.Color;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.omg.CORBA.portable.OutputStream;
public class Lt2 extends JFrame{
JFrame b = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton("发送");
JTextArea jt = new JTextArea();