展开全部
我这刚好有一个,给你参考一下:(分客户端和服务器两个小程序)
//客户端程序及聊天室的图形用户界面
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class ChatTCPSocketJFrame extends JFrame implements ActionListener
{
private String name; //网名
private Socket socket; //TCP Socket对象
private JTextArea text_receiver; //显示对话内容的文本区
private JTextField text_sender; //输入发送内容的文本行
private JButton button_send,button_leave; //发送和离线按钮
private PrintWriter cout; //字符输出流对象
public ChatTCPSocketJFrame(String name, Socket socket) throws IOException//提供图形用户界面
{
super("聊天室 "+name+" "+InetAddress.getLocalHost()+" : "+socket.getLocalPort());
this.setBounds(320,240,400,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(false); //文本区不可编辑
this.getContentPane().add(new JScrollPane(this.text_receiver));
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
this.text_sender = new JTextField(16);
panel.add(this.text_sender);
button_send = new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
button_leave = new JButton("离线");
panel.add(button_leave);
button_leave.addActionListener(this);
this.setVisible(true);
this.name = name;
this.socket = socket;
this.cout = new PrintWriter(socket.getOutputStream(),true);//获得Socket对象的输出流,立即flush
this.cout.println(name); //发送自己网名给对方
BufferedReader cin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得Socket对象的输入流,创建字符输入流
String aline=cin.readLine(); //接收到对方网名
text_receiver.append("连接"+aline+"成功"+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
while (aline!=null && !aline.equals("bye")) //从输入流接收对方发来的字符串
{ //"bye"是约定的离线信息
text_receiver.append(aline+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
}
cin.close();
cout.close();
socket.close(); //关闭Socket连接
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
public ChatTCPSocketJFrame(String name, String host, int port) throws IOException //客户端
{
this(name, new Socket(host, port)); //客户端向指定主机的端口发出TCP连接请求
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button_send) //发送按钮
{
this.cout.println(name+" 说:"+text_sender.getText()); //通过流发送给对方
text_receiver.append("我说:"+text_sender.getText()+"\n");
text_sender.setText("");
}
if (e.getSource()==button_leave) //离线
{
text_receiver.append("我离线\n");
this.cout.println(name+"离线\n"+"bye"); //发送给对方离线约定
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
}
public static void main(String args[]) throws IOException
{
new ChatTCPSocketJFrame("小蜜蜂", "127.0.0.1", 2001);
}
}
//服务端程序
import java.net.*;
import java.io.*;
public class ChatTCPServer
{
public ChatTCPServer(int port, String name) throws IOException //约定端口、网名
{ //本机IP地址和指定端口构成服务端的Socket
ServerSocket server = new ServerSocket(port); //ServerSocket提供TCP连接服务
Socket client = server.accept(); //等待接收客户端的连接申请
new ChatTCPSocketJFrame(name,client); //服务端的聊天室,图形用户界面同客户端
server.close();
}
public static void main(String args[]) throws IOException
{
new ChatTCPServer(2001, "花仙子"); //启动服务端,约定端口,指定网名
}
}
//客户端程序及聊天室的图形用户界面
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class ChatTCPSocketJFrame extends JFrame implements ActionListener
{
private String name; //网名
private Socket socket; //TCP Socket对象
private JTextArea text_receiver; //显示对话内容的文本区
private JTextField text_sender; //输入发送内容的文本行
private JButton button_send,button_leave; //发送和离线按钮
private PrintWriter cout; //字符输出流对象
public ChatTCPSocketJFrame(String name, Socket socket) throws IOException//提供图形用户界面
{
super("聊天室 "+name+" "+InetAddress.getLocalHost()+" : "+socket.getLocalPort());
this.setBounds(320,240,400,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(false); //文本区不可编辑
this.getContentPane().add(new JScrollPane(this.text_receiver));
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
this.text_sender = new JTextField(16);
panel.add(this.text_sender);
button_send = new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
button_leave = new JButton("离线");
panel.add(button_leave);
button_leave.addActionListener(this);
this.setVisible(true);
this.name = name;
this.socket = socket;
this.cout = new PrintWriter(socket.getOutputStream(),true);//获得Socket对象的输出流,立即flush
this.cout.println(name); //发送自己网名给对方
BufferedReader cin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得Socket对象的输入流,创建字符输入流
String aline=cin.readLine(); //接收到对方网名
text_receiver.append("连接"+aline+"成功"+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
while (aline!=null && !aline.equals("bye")) //从输入流接收对方发来的字符串
{ //"bye"是约定的离线信息
text_receiver.append(aline+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
}
cin.close();
cout.close();
socket.close(); //关闭Socket连接
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
public ChatTCPSocketJFrame(String name, String host, int port) throws IOException //客户端
{
this(name, new Socket(host, port)); //客户端向指定主机的端口发出TCP连接请求
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button_send) //发送按钮
{
this.cout.println(name+" 说:"+text_sender.getText()); //通过流发送给对方
text_receiver.append("我说:"+text_sender.getText()+"\n");
text_sender.setText("");
}
if (e.getSource()==button_leave) //离线
{
text_receiver.append("我离线\n");
this.cout.println(name+"离线\n"+"bye"); //发送给对方离线约定
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
}
public static void main(String args[]) throws IOException
{
new ChatTCPSocketJFrame("小蜜蜂", "127.0.0.1", 2001);
}
}
//服务端程序
import java.net.*;
import java.io.*;
public class ChatTCPServer
{
public ChatTCPServer(int port, String name) throws IOException //约定端口、网名
{ //本机IP地址和指定端口构成服务端的Socket
ServerSocket server = new ServerSocket(port); //ServerSocket提供TCP连接服务
Socket client = server.accept(); //等待接收客户端的连接申请
new ChatTCPSocketJFrame(name,client); //服务端的聊天室,图形用户界面同客户端
server.close();
}
public static void main(String args[]) throws IOException
{
new ChatTCPServer(2001, "花仙子"); //启动服务端,约定端口,指定网名
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家国内优质海外HTTP代理商,拥有一个庞大的IP资源池,覆盖200多个地区,IP数量大且匿名度高。其优点还包括超高并发、稳定高效、技术服务等特点,同时提供HTTP、HTTPS以及SOCKS5协议支持。此外,Sto...
点击进入详情页
本回答由Storm代理提供
展开全部
到底要表达什么意思嘛
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询