服务端代码
package com.xiva.demo.socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class SocketServer { public final static List<Socket> socketList = new ArrayList<Socket>(); public static int number = 0; public static void main(String[] args) { try { //绑定服务端在8888端口 ServerSocket server = new ServerSocket(8888); Socket socket; int count = 0; while (true) { //监听客户端程序 socket = server.accept(); count = count + 1; System.out.println(socket.getInetAddress().getHostAddress()); //一个客户端连接后启动一个线程进行监听客户端消息 ReadThread readThread = new ReadThread(socket); readThread.start(); socketList.add(socket); } } catch (IOException e) { e.printStackTrace(); } } } class ThreadSocketServer extends Thread { Socket server; public ThreadSocketServer() { } public ThreadSocketServer(Socket s) { this.server = s; } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); PrintWriter pw = new PrintWriter(server.getOutputStream()); //循环发送信息给客户端 while (true) { String s = br.readLine(); pw.println(s); pw.flush(); System.out.println("Server:" + s); } } catch (IOException e) { e.printStackTrace(); } } } class ReadThread extends Thread { private Socket socket; public ReadThread(Socket socket) { this.socket = socket; } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream())); String read = null; // ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); StringBuffer sBuffer = new StringBuffer(); //循环接受客户端信息 while (true) { read = br.readLine(); // ois.readObject(); if ("bye".equals(read)) { SocketServer.socketList.remove(this.socket); break; } if(read.endsWith("#userName#")) { String welcome = read.substring(0, read.length()-10); List<Socket> socketList = SocketServer.socketList; for (int i = 0; i < socketList.size(); i++) { Socket socket = socketList.get(i); if (this.socket == socket) { continue; } PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.println("Welcome " + welcome + " to join us talking!"); pw.flush(); } continue; } sBuffer.append(read); if(!read.endsWith("#end#")) { sBuffer.append("\r\n"); continue; } String userIp = socket.getInetAddress().getHostAddress(); String content = sBuffer.substring(0, sBuffer.length() - 5); System.out.println(userIp + "client说:" + content); List<Socket> socketList = SocketServer.socketList; for (int i = 0; i < socketList.size(); i++) { Socket socket = socketList.get(i); if (this.socket == socket) { continue; } PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.println(userIp + " say:\r\n" + content); pw.flush(); } sBuffer.delete(0, sBuffer.length()); } br.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
客户端代码:
package com.xiva.demo.socket; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.SocketException; import javax.swing.JFrame; /** * * @author xiva * */ public class TalkFrame extends Frame implements ActionListener { /** * serialVersionUID */ private static final long serialVersionUID = 203806385437188889L; private TextArea msgSend; private TextArea msgShow; private TextArea onLineList; private Panel cPanel; private Panel cPanelMsg; private Label note1; private Button button_exe; private PrintWriter pWriter; private Socket socket; private ReadString read; public TalkFrame() { super("聊天室"); JFrame msgFrame = new JFrame("聊天室"); // msgFrame.setLayout(new GridLayout(2, 2)); cPanel = new Panel(new GridBagLayout()); cPanelMsg = new Panel(new GridBagLayout()); button_exe = new Button("发送消息"); button_exe.setSize(80, 30); msgSend = new TextArea(10, 10); onLineList = new TextArea(27, 18); onLineList.setEditable(false); onLineList.setBackground(Color.WHITE); msgShow = new TextArea(27, 68); msgShow.setEditable(false); msgShow.setBackground(Color.WHITE); note1 = new Label("输入消息:"); GridBagConstraints gbcon = new GridBagConstraints(); // 设定第一个单元格的属性值 gbcon.gridx = 0; gbcon.gridy = 0; gbcon.gridwidth = 1; gbcon.gridheight = 1; gbcon.weightx = 0; gbcon.weighty = 0; gbcon.anchor = GridBagConstraints.WEST; gbcon.fill = GridBagConstraints.HORIZONTAL; gbcon.insets = new Insets(5, 5, 5, 5); gbcon.ipadx = 0; gbcon.ipady = 0; cPanelMsg.add(msgShow, gbcon); // 设定第二个单元格的属性值 gbcon.gridx = 1; gbcon.gridy = 0; gbcon.gridwidth = GridBagConstraints.REMAINDER; gbcon.gridheight = 1; gbcon.weightx = 1; gbcon.weighty = 0; gbcon.anchor = GridBagConstraints.EAST; gbcon.fill = GridBagConstraints.NONE; gbcon.insets = new Insets(0, 0, 0, 0); gbcon.ipadx = 0; gbcon.ipady = 0; cPanelMsg.add(onLineList, gbcon); GridBagConstraints gbc = new GridBagConstraints(); // 设定第一个单元格的属性值 gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0; gbc.weighty = 0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets(0, 0, 0, 0); gbc.ipadx = 0; gbc.ipady = 0; cPanel.add(note1, gbc); // 设定第二个单元格的属性值 gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridheight = 1; gbc.weightx = 1; gbc.weighty = 0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(5, 5, 5, 5); gbc.ipadx = 0; gbc.ipady = 0; cPanel.add(msgSend, gbc); // 设定第二个单元格的属性值 gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = GridBagConstraints.EAST; gbc.gridheight = 1; gbc.weightx = 1; gbc.weighty = 0; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets(1, 1, 1, 1); gbc.ipadx = 0; gbc.ipady = 0; cPanel.add(button_exe, gbc); msgFrame.addWindowListener(new WinClose()); button_exe.addActionListener(this); msgFrame.getContentPane().add(cPanel, "South"); msgFrame.getContentPane().add(cPanelMsg, "North"); msgFrame.setSize(620, 680); msgFrame.setLocation(200, 200); msgFrame.setResizable(false); msgFrame.setVisible(true); } public TextArea getMsgShow() { return msgShow; } public void actionPerformed(ActionEvent e) { if (e.getSource() == button_exe) { if (pWriter == null) { String old = msgShow.getText(); msgShow.setText(old + "\r\n" + "Server down!"); msgSend.setText(""); } else { String sendMsg = msgSend.getText(); pWriter.println(sendMsg + "#end#"); pWriter.flush(); String old = msgShow.getText(); msgShow.setText(old + "\r\nYou:\r\n" + sendMsg); msgSend.setText(""); } } } public static void main(String arg[]) { TalkFrame talk = new TalkFrame(); talk.conent(); } public void conent() { try { this.socket = new Socket("192.168.40.33", 8888); this.read = new ReadString(this); read.start(); this.pWriter = new PrintWriter(this.socket.getOutputStream()); this.pWriter.println(System.getProperty("user.name") + "#userName#"); this.pWriter.flush(); } catch (Exception e) { this.msgShow.setText("Connect Fail!"); } } class WinClose implements WindowListener { @SuppressWarnings("deprecation") public void windowClosing(WindowEvent e) { try { if (socket != null) { read.stop(); pWriter.println("bye"); pWriter.flush(); socket.close(); } } catch (IOException ioe) { } finally { System.exit(0); } } public void windowOpened(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } } public Socket getSocket() { return socket; } } class ReadString extends Thread { private Socket socket; private TextArea msgShow; public boolean exitProgram; public ReadString(Socket socket) { this.socket = socket; } public ReadString(TalkFrame talkFrame) { this.socket = talkFrame.getSocket(); this.msgShow = talkFrame.getMsgShow(); } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream())); // 循环接受服务端信息 while (true) { String read = ""; try { read = br.readLine(); } catch (SocketException se) { System.out.println("Socket closed!"); if (socket.isClosed()) { break; } } System.out.println("Server:" + read); String old = msgShow.getText(); msgShow.setText(old + "\r\n" + read); if (read.equals("bye")) { break; } } br.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
针对上一个版本多了Frame
相关推荐
本项目"Linux下的socket聊天室程序"就是基于Socket编程实现的一个简单多用户交互平台。这个程序可以支持多个客户端同时连接到同一服务器,进行实时的文本聊天。 1. **Socket基础**:Socket是网络通信的基本单元,它...
本项目“php socket聊天室”显然旨在利用PHP的Socket功能创建一个实时的在线聊天平台,使得用户可以通过Web浏览器进行实时交互。下面将详细讨论PHP Socket、聊天室的实现原理以及如何通过cmd运行`start_for_win.bat`...
【标题】"聊天室基础项目资料_socket聊天室_聊天室_java项目_socket_" 涉及的是基于Java的Socket编程技术,用于实现一个TCP/IP通信的即时聊天室。在这个项目中,开发者将学习如何利用Java的Socket类来创建客户端和...
Java Socket聊天室是一种基于Java编程语言实现的网络通信应用,它利用了Socket接口来构建一个局域网内的多人聊天环境,类似于QQ群的功能。在这个项目中,开发者运用了多线程技术来处理并发的用户交互,同时利用...
socket聊天室可以私人对聊,也可以群发,服务器界面可以观察聊天记录,观察上线离线情况,代码适合初步了解Socket的同学,入门必看
Java Socket聊天室是一种基于Java编程语言实现的网络通信应用,主要利用了Socket编程接口来创建客户端和服务器端的连接,从而实现数据的双向传输。在这个系统中,用户可以通过客户端发送消息,服务器端接收到消息后...
【标题】基于多线程和SOCKET聊天室 在IT领域,网络通信是不可或缺的一部分,尤其是在构建实时交互系统时,如聊天室。本项目“基于多线程和SOCKET聊天室”就是一个很好的实例,它利用C#语言在Windows Forms(WINFORM...
Java Socket聊天室是一种基于Java网络编程技术实现的通信系统,主要使用了Socket接口来构建客户端与服务器端的连接。在这个项目中,用户通过控制台(console)进行交互,体验类似于简单的在线聊天室。以下是对这个Java...
Socket聊天室源码是一个基于`.NET`框架,利用`ASP.NET`和`C#`语言实现的网络通信项目。在该源码中,主要涉及了客户端(Client)和服务器端(Server)之间的交互,通过Socket类来实现实时的数据传输,从而创建一个...
在这个“简单的socket聊天室”项目中,我们将深入探讨如何使用Python的socket库来创建一个基础的多人聊天环境,这对于初学者来说是一个很好的实践案例。 首先,我们需要了解什么是Socket。Socket在计算机网络中是一...
用socket实现聊天室1.1版.zip
C# 实现的Socket聊天室完整代码
Java Socket聊天室是一种基于TCP/IP协议的网络通信应用,它允许客户端和服务端通过Socket进行双向通信,实现数据的交换。本项目提供了一个完整的聊天室实现,包括服务端和客户端的代码,具有图形用户界面(GUI),...
本项目"**C# Socket聊天室**"就是基于C#语言实现的,它为我们提供了了解和学习Socket通信的一个实例。下面将详细介绍这个项目的核心知识点。 首先,让我们了解什么是Socket。Socket在计算机网络中是一种进程间通信...
本项目名为"C# SOCKET 聊天室",是一个基于C#和SOCKET技术构建的客户端-服务器端聊天软件。下面将详细介绍这个项目中的关键知识点。 1. **C#语言基础**: C#是一种面向对象的编程语言,由微软开发并用于.NET框架。...
本项目“MFC SOCKET 聊天室客户端”显然是一个使用MFC库和SOCKET API开发的网络聊天室应用,它允许用户通过网络与其他客户端进行实时交流。 MFC是微软为开发者设计的一个面向对象的类库,它封装了许多Windows API...
本主题将深入探讨“JAVA+Socket聊天室源程序”,这是一个基于Java和Socket技术实现的简单聊天室项目,非常适合初学者进行学习和实践。 首先,让我们了解一下Socket。Socket是网络通信的基本组件,它提供了进程间...
在IT领域,Socket编程是一种常见的网络通信方式,它允许客户端和服务端进行双向数据传输,从而实现各种网络应用,如创建聊天室。在这个场景下,我们将深入探讨如何利用Socket技术来构建一个简单的聊天室。 首先,...
在Android平台上,构建一个Socket聊天室是一个典型的网络通信应用场景,主要涉及到Java的Socket编程和Android的客户端开发。本文将深入探讨如何使用Socket技术在Android上实现一个简单的聊天室功能。 首先,我们...
本项目“net.rar_socket 聊天室_socket聊天室_多人聊天室”显然是一个基于`socket`技术实现的多人在线聊天室应用,它涉及到的技术点主要包括以下几个方面: 1. **TCP/IP协议栈**:`socket`编程通常基于TCP/IP协议栈...