做网站的需求调研做网站需要什么技能
- 作者: 五速梦信息网
- 时间: 2026年04月18日 09:59
当前位置: 首页 > news >正文
做网站的需求调研,做网站需要什么技能,政务公开网站建设管理,网站设计怎么做链接网络聊天室 服务器#xff1a; 1.启动服务器#xff0c;在服务器端循环监听客户端的连接 try {ServerSocket serverSocketnew ServerSocket(6622);System.out.println(服务器已启动);while(true){//把客户端实例添加到sockets里Socket socketserverSocket.acc…网络聊天室 服务器 1.启动服务器在服务器端循环监听客户端的连接 try {ServerSocket serverSocketnew ServerSocket(6622);System.out.println(服务器已启动);while(true){//把客户端实例添加到sockets里Socket socketserverSocket.accept();sockets.add(socket);System.out.println(当前连接到客户端数量sockets.size());//为每一个链接过来的客户端开一个线程new SocketThread(socket).start();}} catch (IOException e) {e.printStackTrace();System.out.println(服务器创建失败);}2.把循环接收到的多个客户端Socket对象存储起来(集合) ArrayListSocket socketsnew ArrayList();3.在服务器端每个Socket都要监听各自客户端发来的消息 while(true){try {String msgdataInputStream.readUTF();jTextAreamsg.append(msg\n);//在服务器端显示msg}} catch (IOException e) {e.printStackTrace();System.out.println(客户端下线了);}}4.一旦某个客户端发送了消息那么在服务器端就将此消息转发给其他的客户 //向不同客户端转发消息//遍历socketsfor (Socket socket:sockets){//客户端1 客户端2 客户端3DataOutputStream dataOutputStreamnew DataOutputStream(socket.getOutputStream());dataOutputStream.writeUTF(msg);}客户端 1.用户登录创建Socket Socket socket new Socket(xxxxxxxxx, 6622);2.如果Socket创建成功打开聊天窗口 new ChatWindow(jTextaccount.getText(), socket);//打开聊天窗口3.输入内容点击发送按钮发送消息 4.在客户端监听服务器发送回来的消息并把消息显示出来 class ClientThread extends Thread{DataInputStream dataInputStream;public ClientThread(Socket socket) throws IOException {dataInputStreamnew DataInputStream(socket.getInputStream());}Overridepublic void run() {while(true){try {String msgdataInputStream.readUTF();jTextArea.append(msg\n);//可以显示聊天内容并且保持原先内容} catch (IOException e) {e.printStackTrace();break;}}}}完整代码 ChatWindow public class ChatWindow extends JFrame {JTextArea jTextArea;public ChatWindow(String name, Socket socket) throws IOException {DataOutputStream dosnew DataOutputStream(socket.getOutputStream());this.setTitle(欢迎name登录);this.setSize(500,500);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽//创建面板JPanel jPanelnew JPanel();jTextAreanew JTextArea(22,43);//显示文本域jTextArea.setEditable(false);//不可修改的JTextArea jTextArea2new JTextArea(5,33);//发送文本域jTextArea2.setLineWrap(true);//强制换行JScrollPane jnew JScrollPane(jTextArea2);//滚动条JButton jButtonsendnew JButton(发送);jPanel.add(jTextArea);jPanel.add(j);jPanel.add(jButtonsend);this.add(jPanel);this.setVisible(true);//来到聊天窗口jButtonsend.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {String messagejTextArea2.getText();if(message.length()0){//输入为空JOptionPane.showMessageDialog(null,发送内容不能为空);return;}else{//不为空向服务器发送消息String msgname new SimpleDateFormat(HH:mm:ss).format(new Date());msgmsg\nmessage;try {dos.writeUTF(msg);//发送成功清空发送内容jTextArea2.setText();} catch (IOException ioException) {ioException.printStackTrace();JOptionPane.showMessageDialog(null,内容发送失败请检查网络连接);}}}});new ClientThread(socket).start();this.addWindowListener(new WindowAdapter() {Overridepublic void windowClosing(WindowEvent e) {int resJOptionPane.showConfirmDialog(null,你确定要退出聊天吗?,警告,JOptionPane.OK_CANCEL_OPTION);if(res0){//点击确定new LoginWindow();dispose();}}});}//监听服务器发的消息即客户端123的消息class ClientThread extends Thread{DataInputStream dataInputStream;public ClientThread(Socket socket) throws IOException {dataInputStreamnew DataInputStream(socket.getInputStream());}Overridepublic void run() {while(true){try {String msgdataInputStream.readUTF();jTextArea.append(msg\n);//可以显示聊天内容并且保持原先内容} catch (IOException e) {e.printStackTrace();break;}}}}}LoginWindow public class LoginWindow extends JFrame {public LoginWindow(){this.setTitle(欢迎登录);this.setSize(500,350);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽JPanel jPanelnew JPanel();JLabel jLabelnew JLabel(欢迎登录);jLabel.setFont(new Font(横体, Font.BOLD,30));jPanel.add(jLabel);//欢迎登录//中间第一个空标签JLabel jLabel1new JLabel();//欢迎登录下面空白部分jLabel1.setPreferredSize(new Dimension(500,50));//账号JLabel jLabelAccountnew JLabel(账号);//账号标签jLabelAccount.setPreferredSize(new Dimension(30,30));JTextField jTextaccountnew JTextField(15);//账号文本域//中间第二个空标签JLabel jLabel2new JLabel();//欢迎登录下面空白部分jLabel2.setPreferredSize(new Dimension(500,20));//密码JLabel jLabelPasswordnew JLabel(密码 );//账号标签jLabelPassword.setPreferredSize(new Dimension(30,30));JPasswordField jPasswordFieldnew JPasswordField(15);//中间第二个空标签JLabel jLabel3new JLabel();//欢迎登录下面空白部分jLabel3.setPreferredSize(new Dimension(500,20));//按钮JButton ButtonLoginnew JButton(登录);JButton ButtonSignnew JButton(注册);jPanel.add(jLabel1);jPanel.add(jLabelAccount);jPanel.add(jTextaccount);jPanel.add(jLabel2);jPanel.add(jLabelPassword);jPanel.add(jPasswordField);jPanel.add(jLabel3);jPanel.add(ButtonLogin);jPanel.add(ButtonSign);this.add(jPanel);this.setVisible(true);//按钮事件ButtonLogin.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {if(jTextaccount.getText().equals()||jPasswordField.getText().equals()){//当账号或密码为空时弹出警告JOptionPane.showMessageDialog(null,账号和密码不能为空,警告,JOptionPane.ERROR_MESSAGE);return;}else if(!((jTextaccount.getText().matches(\w{1,100}))(jPasswordField.getText().matches(\w{1,100})))){//当1-100范围内不是字母和数字时弹出警告JOptionPane.showMessageDialog(null,账号密码只能由数字字母组成,警告,JOptionPane.ERROR_MESSAGE);return;}//创建Socketelse {try {Socket socket new Socket(10.13.0.67, 6622);new ChatWindow(jTextaccount.getText(), socket);//打开聊天窗口dispose();//释放窗口} catch (IOException ioException) {ioException.printStackTrace();JOptionPane.showMessageDialog(null, 服务器连接失败请稍后再试);}}}});} }Run public class Run {public static void main(String[] args) {new LoginWindow();} }Server public class Server extends JFrame {JTextArea jTextAreamsg;//放到成员变量便于在内部类中也可以使用//客户端连接的数量ArrayListSocket socketsnew ArrayList();public void ServerStart(){this.setTitle(我是服务器);this.setSize(500,500);this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭窗口时退出程序this.setResizable(false);//禁止窗口拖拽JPanel jPanelnew JPanel();jTextAreamsgnew JTextArea(28,43);jTextAreamsg.setLineWrap(true);//强制换行JScrollPane jScrollPanenew JScrollPane();jTextAreamsg.setEditable(false);//不可修改的jPanel.add(jTextAreamsg);jPanel.add(jScrollPane);this.add(jPanel);this.setVisible(true);//关闭窗口this.addWindowListener(new WindowAdapter() {Overridepublic void windowClosing(WindowEvent e) {int resJOptionPane.showConfirmDialog(null,你确定要关闭服务器吗?,警告,JOptionPane.OK_CANCEL_OPTION);if(res0){//点击确定dispose();}}});try {ServerSocket serverSocketnew ServerSocket(6622);System.out.println(服务器已启动);while(true){//把客户端实例添加到sockets里Socket socketserverSocket.accept();sockets.add(socket);System.out.println(当前连接到客户端数量sockets.size());//为每一个链接过来的客户端开一个线程new SocketThread(socket).start();}} catch (IOException e) {e.printStackTrace();System.out.println(服务器创建失败);}}//内部类用来监听自己客户端有没有发消息class SocketThread extends Thread{Socket socket;//用来引入到run方法DataInputStream dataInputStream;public SocketThread(Socket socket) throws IOException {this.socketsocket;//把成员变量socket赋值为传入的socket参数以便于客户端下线后可以从sockets里移除客户端dataInputStreamnew DataInputStream(socket.getInputStream());}Overridepublic void run() {//循环监听客户端自己发送消息while(true){try {String msgdataInputStream.readUTF();jTextAreamsg.append(msg\n);//在服务器端显示msg//向不同客户端转发消息//遍历socketsfor (Socket socket:sockets){//客户端1 客户端2 客户端3DataOutputStream dataOutputStreamnew DataOutputStream(socket.getOutputStream());dataOutputStream.writeUTF(msg);}} catch (IOException e) {e.printStackTrace();System.out.println(客户端下线了);sockets.remove(socket);break;}}}} } ServerRun public class ServerRun {public static void main(String[] args) {new Server().ServerStart();} }在这个过程中监听服务器消息和监听客户端消息都用到了内部类这样的好处是可以之间在内部类中使用大类里面的jTextArea和sockets
- 上一篇: 做网站的新闻园林效果图网站
- 下一篇: 做网站的要faq怎么给东莞网站建设需要多少钱
相关文章
-
做网站的新闻园林效果图网站
做网站的新闻园林效果图网站
- 技术栈
- 2026年04月18日
-
做网站的销售工作好吗液压电机东莞网站建设
做网站的销售工作好吗液压电机东莞网站建设
- 技术栈
- 2026年04月18日
-
做网站的销售centos 网站开发工具
做网站的销售centos 网站开发工具
- 技术栈
- 2026年04月18日
-
做网站的要faq怎么给东莞网站建设需要多少钱
做网站的要faq怎么给东莞网站建设需要多少钱
- 技术栈
- 2026年04月18日
-
做网站的优化价格php网站如何做多语言
做网站的优化价格php网站如何做多语言
- 技术栈
- 2026年04月18日
-
做网站的怎么挣钱、WordPress页面置顶菜单
做网站的怎么挣钱、WordPress页面置顶菜单
- 技术栈
- 2026年04月18日
