一个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();
}
}
搜索更多相关的解决方案:
Socket
----------------解决方案--------------------------------------------------------
最主要是这两段..
[CODE]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();
}
}
});[/CODE]
[CODE]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();
}
}
});
[/CODE]
----------------解决方案--------------------------------------------------------
不要用这种循环,
先实现发一条收一条,回一条显示一条.不要Timer来做
----------------解决方案--------------------------------------------------------
哦..如果不用timer...怎么实现..随时接收数据呢?...我想不到..
----------------解决方案--------------------------------------------------------
起一个线程专门接收数据
发数据用事件处理
----------------解决方案--------------------------------------------------------
又要用线程啊?...我不懂...没有其它方法了吗?..
----------------解决方案--------------------------------------------------------
没有别的方法了
只能用线程
因为数据读取是一个阻塞的方法
你不用线程的话,你的主线程就会被阻死,所以必须起一个新的线程去做这种事
----------------解决方案--------------------------------------------------------
其实线程很容易的,你可以试着用用看
----------------解决方案--------------------------------------------------------
嗯 ..那好吧..我先找找这方面的资料..看看.
----------------解决方案--------------------------------------------------------
把这贴沉了...
----------------解决方案--------------------------------------------------------