问题描述
- 下面TCPsocket通信问题的代码存在哪些问题
-
package com.gp.Socket;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class ChatTTCPServer {
public ChatTTCPServer(int port,String name) throws IOException{
ServerSocket server=new ServerSocket(port);
Socket client=server.accept();
new ChatTTCPSocketJFrame(name,client);
server.close();
}
public static void main(String[] args) throws IOException
{
new ChatTTCPServer(9999,"Jack");
}}
package com.gp.Socket;import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;public class ChatTTCPSocketJFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private JTextArea text_receiver;
private JTextField text_sender;
private PrintWriter cout;
private JButton buttons[];
public ChatTTCPSocketJFrame(String name ,Socket socket) throws HeadlessException, IOException{
super("聊天室"+name+""+InetAddress.getLocalHost()+":"+socket.getPort());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver=new JTextArea();
this.text_receiver.setEditable(false);
this.getContentPane().add(new JScrollPane(this.text_receiver));
JToolBar toolBar = new JToolBar();
this.getContentPane().add(toolBar,"South");
toolBar.add(this.text_sender=new JTextField(30));
String[] buttonstr={"发送","离线"};
buttons=new JButton[buttonstr.length];
for(int i=0;i<buttonstr.length;i++){
toolBar.add(buttons[i]=new JButton(buttonstr[i]));
buttons[i].addActionListener(this);
}
this.setVisible(true);
this.name=name;
this.cout=new PrintWriter(socket.getOutputStream(),true);
this.cout.println(name);
BufferedReader br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line="连接"+br.readLine()+"成功";
while(line!=null&&!line.endsWith("bye"));
{
text_receiver.append(line+"rn");
line=br.readLine();
}
br.close();
this.cout.close();
socket.close();
buttons[0].setEnabled(false);
buttons[1].setEnabled(false);
}@Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("发送")); { this.cout.println(name+"说"+text_sender.getText()); text_receiver.append("我说: "+text_sender.getText()+"n"); text_sender.setText(""); } if(e.getActionCommand().equals("离线")){ text_receiver.append("离线"); this.cout.println(name+"离线n"+"byen"); buttons[0].setEnabled(false); buttons[1].setEnabled(false); } } public static void main(String[] args) throws IOException { new ChatTTCPSocketJFrame("Mike","localhost",9999); } public ChatTTCPSocketJFrame (String name,String host,int port) throws IOException { this(name,new Socket(host,port)); }
}
不知道为什么不能进行通信 新手求帮助
解决方案
用sniffer调试下,看看数据包有没有发出去。