Java聊天系统

1.Socket类

Socket(InetAddress address, int port)
创建一个流套接字并将其连接到指定 IP 地址的指定端口号。 Socket(String host, int port)
创建一个流套接字并将其连接到指定主机上的指定端口号。 Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
创建一个套接字并将其连接到指定远程地址上的指定远程端口。 Socket(String host, int port, InetAddress localAddr, int localPort)
创建一个套接字并将其连接到指定远程主机上的指定远程端口。 close()
关闭此套接字。 connect(SocketAddress endpoint)
将此套接字连接到服务器。 connect(SocketAddress endpoint, int timeout)
将此套接字连接到服务器,并指定一个超时值。 getInetAddress()
返回套接字连接的地址。 getInputStream()
返回此套接字的输入流。 getLocalPort()
返回此套接字绑定到的本地端口。 getOutputStream()
返回此套接字的输出流。 getPort()
返回此套接字连接到的远程端口。

2.ServerSocket类

ServerSocket(int port)
创建绑定到特定端口的服务器套接字。
accept()
侦听并接受到此套接字的连接。
getInetAddress()
返回此服务器套接字的本地地址。 Socket编程步骤
服务器端创建ServerSocket对象,调用accept方法返回Socket对象
客户端创建Socket对象,通过端口连接到服务器
客户端、服务器端都使用Socket中的getInputStream方法和getOutputStream方法获得输入流和输出流,进一步进行数据读写操作 (InetAddress用来描述主机地址;
Socket用来创建两台主机之间的连接;
ServerSocket用来侦听来自客户端的请求;
Socket通常称作“套接字”,通常通过“套接字”向网络发出请求或者应答网络请求。)

3.实现的步骤:

   第一步: ChatUtil工具类:把一些常用的常量放进来

    第二步:Server开启服务
第三步:ClientSocket连接服务器的socket
第四步:CHatFrame(添加两个属性(name,sex))2.添加了getSocket方法
第五步:LoginFrame设置了默认值 处理性别获得socket对象
package com.lanqiao.demo2;
/**
* 工具类
* @author qichunlin
*
*/
public final class ChatUtil {
//地址
public static final String ADDRESS = "localhost";
//端口
public static final int PORT = 9999;
}
package com.lanqiao.demo2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; /**
* 服务端类
* @author qichunlin
*/
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(ChatUtil.PORT);
int count = 0;
while (true) {
System.out.println("等待客户端连接.......");
Socket socket = ss.accept();
count++;
System.out.println("目前有"+count+"个客户端进入了聊天室");
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.lanqiao.demo2;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; /**
* 客户端的socket
* @author qichunlin
*
*/
public class ClientSocket {
public static Socket socket;
public ClientSocket() {
try {
socket = new Socket(ChatUtil.ADDRESS, ChatUtil.PORT);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.lanqiao.demo2;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField; /**
* 聊天系统的登录界面
* @authorqichunlin
*
*/
public class LoginFrame extends JFrame implements ActionListener{
//定义组件
JLabel userLab,addrLab,portLab;//标签
JTextField userText,addrText,portText;//文本框
JRadioButton radioMan,radioWoman,radioser;//单选按钮
ButtonGroup group ;//组
JButton connectBut,closeBut;//按钮
//容器
JPanel p1,p2,p3; public LoginFrame(){
//实例化组件
p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT)); userLab = new JLabel("姓名:");
userText = new JTextField(10);
radioMan = new JRadioButton("男");
radioMan.setSelected(true);
radioWoman = new JRadioButton("女");
radioser = new JRadioButton("保密");
//把单选按钮,添加到组中
group = new ButtonGroup();
group.add(radioMan);
group.add(radioWoman);
group.add(radioser); //往p1中,添加组件了(注意:组不需要添加到容器中)
p1.add(userLab);
p1.add(userText);
p1.add(radioMan);
p1.add(radioWoman);
p1.add(radioser); p2 = new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
addrLab = new JLabel("地址:"); addrText = new JTextField(10);
addrText.setText(ChatUtil.ADDRESS); portLab = new JLabel("端口:");
portText = new JTextField(10);
portText.setText(ChatUtil.PORT+""); //把组件添加到p2中
p2.add(addrLab);
p2.add(addrText);
p2.add(portLab);
p2.add(portText); p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.CENTER)); connectBut =new JButton("连接");
//绑定事件【点击事件】
connectBut.addActionListener(this); closeBut = new JButton("断开");
//把组件添加到p3中
p3.add(connectBut);
p3.add(closeBut); //设置面板的布局模式(流式布局)
this.getContentPane().setLayout(new GridLayout(3,1));//网格布局 //把组件添加到面板了
//1、获取面板
Container c = this.getContentPane();
//把p1容器添加到面板
c.add(p1);
//把p2容器添加到面板
c.add(p2);
//把p3容器添加到面板
c.add(p3); init();
}
/**
* 初始化窗体的基本信息
*/
public void init(){
//1、标题
this.setTitle("登录界面");
//2、大小
this.setSize(350,200);
//3、关闭放大功能
this.setResizable(false);
//4、位置
this.setLocationRelativeTo(null);
//5、是否显示
this.setVisible(true);
//6、关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new LoginFrame();
}
//点击事件的处理过程
@Override
public void actionPerformed(ActionEvent e) {
//处理选择中的性别
String sex = "";
if(radioWoman.isSelected()) {
sex = "女";
}else if(radioMan.isSelected()) {
sex = "男";
}else {
sex = "保密";
}
System.out.println("============");
//1、隐藏当前的界面【登录界面】
this.setVisible(false);
//2、显示聊天的界面
ChatFrame c = new ChatFrame(userText.getText(),sex);//登录名传递过来
c.getSocket();
}
}

package com.lanqiao.demo2; import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; /**
* 服务端类
* @author qichunlin
*/
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(ChatUtil.PORT);
int count = 0;
while (true) {
System.out.println("等待客户端连接.......");
Socket socket = ss.accept();
count++;
System.out.println("目前有"+count+"个客户端进入了聊天室");
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
05-18 09:18