可以发送消息 但是接收不到消息,哪位知道的指点下,谢谢!!!服务器端:Server.java
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;public class Server extends JFrame implements ActionListener{
JTextArea outputArea;
JTextArea inputArea;
JButton send;
JButton cancel;
JPanel panel;
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server(){
super("Server");
outputArea=new JTextArea(10,5);
inputArea=new JTextArea(10,5);
outputArea.setBackground(Color.LIGHT_GRAY);
outputArea.setEditable(false);
inputArea.setBackground(Color.GRAY);
send=new JButton("send");
cancel=new JButton("cancel");
panel=new JPanel();
panel.add(send);
panel.add(cancel);
Container cp=getContentPane();
cp.add(outputArea,BorderLayout.NORTH);
cp.add(inputArea,BorderLayout.CENTER);
cp.add(panel,BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
setResizable(false);
send.addActionListener(this);
cancel.addActionListener(this);
try{
ss=new ServerSocket(4000);
socket=ss.accept();
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream());
outputArea.append("Client says:"+in.readLine());
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="cancel"){
inputArea.setText(" ");
}
if(e.getActionCommand()=="send"){
try{
outputArea.append("Server says"+inputArea.getText()+"\n");
inputArea.setText(" ");
out.write(inputArea.getText());
}catch(Exception e1){}
}
}
public static void main(String []args){
new Server();
}
}客户端:Client.javaimport java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.net.Socket;
import java.net.InetAddress;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;public class Client extends JFrame implements ActionListener{
JTextArea outputArea;
JTextArea inputArea;
JButton send;
JButton cancel;
JPanel panel;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Client(){
super("Client");
outputArea=new JTextArea(10,5);
inputArea=new JTextArea(10,5);
outputArea.setBackground(Color.LIGHT_GRAY);
outputArea.setEditable(false);
inputArea.setBackground(Color.GRAY);
send=new JButton("send");
cancel=new JButton("cancel");
panel=new JPanel();
panel.add(send);
panel.add(cancel);
Container cp=getContentPane();
cp.add(outputArea,BorderLayout.NORTH);
cp.add(inputArea,BorderLayout.CENTER);
cp.add(panel,BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
setResizable(false);
send.addActionListener(this);
cancel.addActionListener(this);
try{
socket=new Socket(InetAddress.getLocalHost(),4000);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream());
outputArea.append("Server says:"+in.readLine());
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="cancel"){
inputArea.setText(" ");
}
if(e.getActionCommand()=="send"){
outputArea.append("Client says:"+inputArea.getText()+"\n");
inputArea.setText(" ");
out.write(inputArea.getText());
}
}
public static void main(String []args){
new Client();
}
}
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;public class Server extends JFrame implements ActionListener{
JTextArea outputArea;
JTextArea inputArea;
JButton send;
JButton cancel;
JPanel panel;
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server(){
super("Server");
outputArea=new JTextArea(10,5);
inputArea=new JTextArea(10,5);
outputArea.setBackground(Color.LIGHT_GRAY);
outputArea.setEditable(false);
inputArea.setBackground(Color.GRAY);
send=new JButton("send");
cancel=new JButton("cancel");
panel=new JPanel();
panel.add(send);
panel.add(cancel);
Container cp=getContentPane();
cp.add(outputArea,BorderLayout.NORTH);
cp.add(inputArea,BorderLayout.CENTER);
cp.add(panel,BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
setResizable(false);
send.addActionListener(this);
cancel.addActionListener(this);
try{
ss=new ServerSocket(4000);
socket=ss.accept();
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream());
outputArea.append("Client says:"+in.readLine());
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="cancel"){
inputArea.setText(" ");
}
if(e.getActionCommand()=="send"){
try{
outputArea.append("Server says"+inputArea.getText()+"\n");
inputArea.setText(" ");
out.write(inputArea.getText());
}catch(Exception e1){}
}
}
public static void main(String []args){
new Server();
}
}客户端:Client.javaimport java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.net.Socket;
import java.net.InetAddress;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;public class Client extends JFrame implements ActionListener{
JTextArea outputArea;
JTextArea inputArea;
JButton send;
JButton cancel;
JPanel panel;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Client(){
super("Client");
outputArea=new JTextArea(10,5);
inputArea=new JTextArea(10,5);
outputArea.setBackground(Color.LIGHT_GRAY);
outputArea.setEditable(false);
inputArea.setBackground(Color.GRAY);
send=new JButton("send");
cancel=new JButton("cancel");
panel=new JPanel();
panel.add(send);
panel.add(cancel);
Container cp=getContentPane();
cp.add(outputArea,BorderLayout.NORTH);
cp.add(inputArea,BorderLayout.CENTER);
cp.add(panel,BorderLayout.SOUTH);
setSize(500,400);
setVisible(true);
setResizable(false);
send.addActionListener(this);
cancel.addActionListener(this);
try{
socket=new Socket(InetAddress.getLocalHost(),4000);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream());
outputArea.append("Server says:"+in.readLine());
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="cancel"){
inputArea.setText(" ");
}
if(e.getActionCommand()=="send"){
outputArea.append("Client says:"+inputArea.getText()+"\n");
inputArea.setText(" ");
out.write(inputArea.getText());
}
}
public static void main(String []args){
new Client();
}
}
应该另外启动一条线程不断轮询in.readLine()
in.readLine()
} 在不另起轮询线程的时候这样也可以
但是不推荐 这样太没意思 实现以后就是 客户端说一句 服务器端说一句 顶楼上的做法