一个Socket程序无法发送消息的问题
大家帮我看看.连接之后..程序好像死循环了...
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SocketServerDemo extends JFrame{
//界面布局所需控件
private JLabel jlportShow;
private JTextField jtfport;
private JTextArea jtagetMessage,jtasendMessage;
private JButton jbConnect,jbSend,jbNotConnect;
//服务器端的Socket
private ServerSocket serverSocket;
private Socket client;
private int port;
private PrintStream clientOutput;
private javax.swing.Timer timer;
private DataInputStream dis;
public SocketServerDemo(){
super("ServerSocket服务器端");
initFrame();
this.setSize(640,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initFrame(){
try{
Container container = this.getContentPane();
jlportShow = new JLabel("端号口:");
jtfport = new JTextField(10);
JPanel panelNorth = new JPanel();
panelNorth.add(jlportShow);
panelNorth.add(jtfport);
container.add(panelNorth,BorderLayout.NORTH);
jtagetMessage = new JTextArea();
jtasendMessage = new JTextArea();
JScrollPane scrollpaneOne = new JScrollPane(jtagetMessage);
JScrollPane scrollpaneTwo = new JScrollPane(jtasendMessage);
container.add(scrollpaneOne,BorderLayout.CENTER);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scrollpaneTwo);
jbConnect = new JButton("连接");
jbConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
port = Integer.parseInt(jtfport.getText());
serverSocket = new ServerSocket(port);
client = serverSocket.accept();
timer = new javax.swing.Timer(50,new ActionListener(){
public void actionPerformed(ActionEvent eve){
try{
dis = new DataInputStream(client.getInputStream());
String strLine = dis.readLine();
if(strLine!=null){
jtagetMessage.insert(strLine+"\n",jtagetMessage.getDocument().getLength());
}
}catch(IOException ioexce){
ioexce.printStackTrace();
}
}
});
timer.start();
System.out.println("连接成功,目标IP是:"+client.getInetAddress());
jbConnect.setEnabled(false);
}catch(IOException ioEx){
ioEx.printStackTrace();
}catch(Exception exc){
exc.printStackTrace();
}
}
});
jbSend = new JButton("发送");
jbSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
clientOutput = new PrintStream(client.getOutputStream());
String strLine = jtasendMessage.getText();
clientOutput.println(strLine);
jtasendMessage.setText("");
}catch(IOException ioex){
ioex.printStackTrace();
}
}
});
jbNotConnect = new JButton("断开连接");
jbNotConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
client.close();
}catch(IOException ioex){
ioex.printStackTrace();
}
}
});
box.add(jbConnect);
box.add(jbSend);
box.add(jbNotConnect);
container.add(box,BorderLayout.SOUTH);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
new SocketServerDemo();
}
}
客户端:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.zip.*;
public class SocketClientDemo extends JFrame{
private int port;
private String host;
private Socket socket;
private PrintStream serverOutput;
private DataInputStream serverInput;
private JLabel jlIP,jlport;
private JTextField jtfIP,jtfport;
private JTextArea jtagetMessage,jtasendMessage;
private JButton jbConnect,jbSend,jbnotConnect;
private javax.swing.Timer timer;
public SocketClientDemo(){
super("Socket客户端程序");
initFrame();
this.setSize(640,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initFrame(){
Container container = this.getContentPane();
jlIP = new JLabel("IP地址:");
jtfIP = new JTextField(20);
jlport = new JLabel("端口:");
jtfport = new JTextField(10);
JPanel panelNorth = new JPanel();
panelNorth.add(jlIP);
panelNorth.add(jtfIP);
panelNorth.add(jlport);
panelNorth.add(jtfport);
container.add(panelNorth,BorderLayout.NORTH);
jtagetMessage = new JTextArea();
JScrollPane scrollpaneOne = new JScrollPane(jtagetMessage);
container.add(scrollpaneOne);
jtasendMessage = new JTextArea();
JScrollPane scrollpaneTwo = new JScrollPane(jtasendMessage);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scrollpaneTwo);
jbConnect = new JButton("连接");
jbConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
host = jtfIP.getText();
port = Integer.parseInt(jtfport.getText());
socket = new Socket(InetAddress.getByName(host),port);
timer = new javax.swing.Timer(50,new ActionListener(){
public void actionPerformed(ActionEvent eve){
try{
serverInput = new DataInputStream(socket.getInputStream());
String strLine = serverInput.readLine();
if(strLine !=null){
jtagetMessage.insert(strLine+"\n",jtagetMessage.getDocument().getLength());
}
}catch(IOException ex){
ex.printStackTrace();
}
}
});
timer.start();
}catch(IOException exc){
exc.printStackTrace();
}
}
});
jbSend = new JButton("发送");
jbSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
serverOutput = new PrintStream(socket.getOutputStream());
serverOutput.println(jtasendMessage.getText());
jtasendMessage.setText("");
}catch(IOException ex){
ex.printStackTrace();
}
}
});
jbnotConnect = new JButton("断开连接");
jbnotConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
socket.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
});
box.add(jbConnect);
box.add(jbSend);
box.add(jbnotConnect);
container.add(box,BorderLayout.SOUTH);
}
public static void main(String[] args){
new SocketClientDemo();
}
}
服务器端
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SocketServerDemo extends JFrame{
//界面布局所需控件
private JLabel jlportShow;
private JTextField jtfport;
private JTextArea jtagetMessage,jtasendMessage;
private JButton jbConnect,jbSend,jbNotConnect;
//服务器端的Socket
private ServerSocket serverSocket;
private Socket client;
private int port;
private PrintStream clientOutput;
private javax.swing.Timer timer;
private DataInputStream dis;
public SocketServerDemo(){
super("ServerSocket服务器端");
initFrame();
this.setSize(640,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initFrame(){
try{
Container container = this.getContentPane();
jlportShow = new JLabel("端号口:");
jtfport = new JTextField(10);
JPanel panelNorth = new JPanel();
panelNorth.add(jlportShow);
panelNorth.add(jtfport);
container.add(panelNorth,BorderLayout.NORTH);
jtagetMessage = new JTextArea();
jtasendMessage = new JTextArea();
JScrollPane scrollpaneOne = new JScrollPane(jtagetMessage);
JScrollPane scrollpaneTwo = new JScrollPane(jtasendMessage);
container.add(scrollpaneOne,BorderLayout.CENTER);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scrollpaneTwo);
jbConnect = new JButton("连接");
jbConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
port = Integer.parseInt(jtfport.getText());
serverSocket = new ServerSocket(port);
client = serverSocket.accept();
timer = new javax.swing.Timer(50,new ActionListener(){
public void actionPerformed(ActionEvent eve){
try{
dis = new DataInputStream(client.getInputStream());
String strLine = dis.readLine();
if(strLine!=null){
jtagetMessage.insert(strLine+"\n",jtagetMessage.getDocument().getLength());
}
}catch(IOException ioexce){
ioexce.printStackTrace();
}
}
});
timer.start();
System.out.println("连接成功,目标IP是:"+client.getInetAddress());
jbConnect.setEnabled(false);
}catch(IOException ioEx){
ioEx.printStackTrace();
}catch(Exception exc){
exc.printStackTrace();
}
}
});
jbSend = new JButton("发送");
jbSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
clientOutput = new PrintStream(client.getOutputStream());
String strLine = jtasendMessage.getText();
clientOutput.println(strLine);
jtasendMessage.setText("");
}catch(IOException ioex){
ioex.printStackTrace();
}
}
});
jbNotConnect = new JButton("断开连接");
jbNotConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
client.close();
}catch(IOException ioex){
ioex.printStackTrace();
}
}
});
box.add(jbConnect);
box.add(jbSend);
box.add(jbNotConnect);
container.add(box,BorderLayout.SOUTH);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
new SocketServerDemo();
}
}
客户端:
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.zip.*;
public class SocketClientDemo extends JFrame{
private int port;
private String host;
private Socket socket;
private PrintStream serverOutput;
private DataInputStream serverInput;
private JLabel jlIP,jlport;
private JTextField jtfIP,jtfport;
private JTextArea jtagetMessage,jtasendMessage;
private JButton jbConnect,jbSend,jbnotConnect;
private javax.swing.Timer timer;
public SocketClientDemo(){
super("Socket客户端程序");
initFrame();
this.setSize(640,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initFrame(){
Container container = this.getContentPane();
jlIP = new JLabel("IP地址:");
jtfIP = new JTextField(20);
jlport = new JLabel("端口:");
jtfport = new JTextField(10);
JPanel panelNorth = new JPanel();
panelNorth.add(jlIP);
panelNorth.add(jtfIP);
panelNorth.add(jlport);
panelNorth.add(jtfport);
container.add(panelNorth,BorderLayout.NORTH);
jtagetMessage = new JTextArea();
JScrollPane scrollpaneOne = new JScrollPane(jtagetMessage);
container.add(scrollpaneOne);
jtasendMessage = new JTextArea();
JScrollPane scrollpaneTwo = new JScrollPane(jtasendMessage);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scrollpaneTwo);
jbConnect = new JButton("连接");
jbConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
host = jtfIP.getText();
port = Integer.parseInt(jtfport.getText());
socket = new Socket(InetAddress.getByName(host),port);
timer = new javax.swing.Timer(50,new ActionListener(){
public void actionPerformed(ActionEvent eve){
try{
serverInput = new DataInputStream(socket.getInputStream());
String strLine = serverInput.readLine();
if(strLine !=null){
jtagetMessage.insert(strLine+"\n",jtagetMessage.getDocument().getLength());
}
}catch(IOException ex){
ex.printStackTrace();
}
}
});
timer.start();
}catch(IOException exc){
exc.printStackTrace();
}
}
});
jbSend = new JButton("发送");
jbSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
serverOutput = new PrintStream(socket.getOutputStream());
serverOutput.println(jtasendMessage.getText());
jtasendMessage.setText("");
}catch(IOException ex){
ex.printStackTrace();
}
}
});
jbnotConnect = new JButton("断开连接");
jbnotConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
socket.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
});
box.add(jbConnect);
box.add(jbSend);
box.add(jbnotConnect);
container.add(box,BorderLayout.SOUTH);
}
public static void main(String[] args){
new SocketClientDemo();
}
}