Ace_Gp
kuop4wn
2015-12-25 02:00
采纳率: 50%
浏览 1.5k
已采纳

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+"\r\n");
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"+"bye\n");
        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));
}

}

可以运行 但是不能进行通信 求大神帮助一波

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • wojiushiwo945you
    毕小宝 2015-12-25 05:32
    已采纳

    测试了下你的代码,整体代码没有问题,之所以不能通信是因为你手误多写了两个;
    分别是:1)if(e.getActionCommand().equals("发送")); 2)while(line!=null&&!line.endsWith("bye"));
    去掉这两个地方的末尾的分号,同时给Frame框设置个size.即在this.setVisible(true);这行代码前面加一行:this.setSize(400, 400);
    先启动Server,再启动Client,程序就能正常运行了。

    点赞 评论
  • Royal_lr
    Royal_lr 2015-12-25 02:09

    我不知道你是怎样在通信,你这样肯定不能跟服务端通信的,服务端都没有接受数据

    点赞 评论

相关推荐