大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.io.InputStream;
成都创新互联公司专注于金水网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供金水营销型网站建设,金水网站制作、金水网页设计、金水网站官网定制、微信小程序开发服务,打造金水网络公司原创品牌,更为您提供金水网站排名全网营销落地服务。
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java.io.IOException;
import java.util.Date;
class Server
{
public Server()
{
try
{
ServerSocket s=new ServerSocket(8888);
Socket ss=s.accept();
OutputStream out=ss.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
InputStream in=ss.getInputStream();
DataInputStream din=new DataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已经连接到服务器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"
+"你的链接端口:"+ss.getLocalPort()+"\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Server();
}
}
//接受客户端信息
class ReadMessage extends Thread
{
private DataInputStream din;
public ReadMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while (true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"客户端说:"+str);
if (str.equals("bye"))
{
System.out.println("客户端下线!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// 发出服务器信息
class SendMessage extends Thread
{
private DataOutputStream dout;
public SendMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
InputStreamReader inr=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inr);
String str;
try
{
while(true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("服务器退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java点虐 .Socket;
import java.io.IOException;
import java.util.Date;
class Client
{
public Client()
{
try
{
Socket s=new Socket("192.168.1.2",8888);
InputStream in=s.getInputStream();
DataInputStream din=new DataInputStream(in);
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("服务器你好!我是客户端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
class ReaderMessage implements Runnable
{
private DataInputStream din;
public ReaderMessage(DataInputStream din)
{
this.din=din;
}
public void run()
{
String str;
try
{
while(true)
{
str=din.readUTF();
System.out.println(new Date().toLocaleString()+"服务器说:"+str);
if (str.equals("bye"))
{
System.out.println("服务器已经关闭,此程序自动退出!");
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable
{
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout)
{
this.dout=dout;
}
public void run()
{
String str;
InputStreamReader inf=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(inf);
try
{
while (true)
{
str=buf.readLine();
dout.writeUTF(str);
if (str.equals("bye"))
{
System.out.println("客户端自己退出!");
System.exit(1);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Java多人聊天可以使用Java的Socket编程实现,主要的思路是:使用服务器来维护所有客户端的连接,并将客户端之间的聊天信息进行转发。
具体的实现步骤如下:
创建服务器端:使用ServerSocket类创建一个服务器端,并监听指定的端口,等待客户端的连接。
创建客户端:使用Socket类创建一个客户端,并连接到服务器端。
实现聊天功能:客户端和服务器端之间可以通过输入和输出流进行通信,客户端将聊天信息发送给服务器,服务器再将其转发给其他客户端。
处理异常:在实现聊天功能时,需要注意处理可能出现的异常,例如连接异常、输入输出异常等等。
一个简单的Java多人聊天程序的代码框架如下:
服务器端:
import java.io.IOException;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java.util.ArrayList;
public class ChatServer {
private ServerSocket serverSocket;
private ArrayListClientHandler clients;
public ChatServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
clients = new ArrayListClientHandler();
System.out.println("服务器已启动,等待客户端连接...");
}
public void start() throws IOException {
while (true) {
Socket socket = serverSocket.accept();
ClientHandler client = new ClientHandler(socket, this);
clients.add(client);
client.start();
}
}
public void broadcast(String message) {
for (ClientHandler client : clients) {
client.sendMessage(message);
}
}
public void removeClient(ClientHandler client) {
clients.remove(client);
}
public static void main(String[] args) throws IOException {
ChatServer server = new ChatServer(12345);
server.start();
}
}
客户端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java点虐 .Socket;
public class ChatClient {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private String name;
public ChatClient(String serverAddress, int port, String name) throws IOException {
socket = new Socket(serverAddress, port);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
this.name = name;
}
public void start() throws IOException {
System.out.println("欢迎来到聊天室!");
new Thread(new IncomingMessageHandler()).start();
new Thread(new OutgoingMessageHandler()).start();
}
private class IncomingMessageHandler implements Runnable {
@Override
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) {
break;
}
System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
}
}
private class OutgoingMessageHandler implements Runnable {
@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
while (true) {
String message = reader.readLine();
if (message.equals("quit")) {
break;
}
writer.println(name + ": " + message);
}
} catch (IOException e) {
e.printStackTrace;
} finally {
close();
}
}
}
先运行服务端 再运行客户端 就能对话了
服务端
package Liaotian;
import java.awt.*;
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点虐 .ServerSocket;
import java点虐 .Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class fwjiemian extends Frame implements ActionListener{
JTextArea lt;
TextField sr;
Button fs;
Panel xia;
ServerSocket ss;
Socket s;
BufferedReader br;
PrintWriter pw;
BufferedReader in;
public fwjiemian(){
lt=new JTextArea();
lt.setLineWrap(true);
sr=new TextField();
fs=new Button("发送");
xia=new Panel(new GridLayout(1,2));
xia.add(sr);
xia.add(fs);
this.add(lt);
this.add(xia,BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(400, 500);
this.setLocation(350, 100);
this.setTitle("服务器");
this.setVisible(true);
fs.addActionListener(this);
//创建客户端+接受消息
try {
lt.setFont(new java.awt.Font("宋体",0,15));
ss=new ServerSocket(8888);
lt.append("服务器已启动,等待客户端接入..."+"\n"+"\n");
s=ss.accept();
lt.append("客户端已接入..."+"\n"+"\n");
while(true){
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//接受消息
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=br.readLine();
lt.append("☆ 客户端:"+info+" "+sm.format(new Date())+"\n"+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new fwjiemian();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==fs){
try {
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//发送消息
String infotokehu=sr.getText();
pw=new PrintWriter(s.getOutputStream(),true);
pw.println(infotokehu);
lt.setFont(new java.awt.Font("宋体",0,15));
lt.append("★ 服务器:"+infotokehu+" "+sm.format(new Date())+"\n"+"\n");
sr.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
客户端
package Liaotian;
import java.awt.*;
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点虐 .ServerSocket;
import java点虐 .Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class khjiemian extends Frame implements ActionListener{
JTextArea lt;
TextField sr;
Button fs;
Panel xia;
Socket s;
PrintWriter pw;
BufferedReader br;
BufferedReader in;
public khjiemian(){
lt=new JTextArea();
lt.setLineWrap(true);
sr=new TextField();
fs=new Button("发送");
xia=new Panel(new GridLayout(1,2));
xia.add(sr);
xia.add(fs);
this.add(lt);
this.add(xia,BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(400, 500);
this.setLocation(800, 100);
this.setTitle("服务器");
this.setVisible(true);
fs.addActionListener(this);
//创建客户端+接受消息
try {
lt.setFont(new java.awt.Font("宋体",0,15));
s=new Socket("127.0.0.1",8888);
lt.append("客户端已启动,等待接入服务器..."+"\n"+"\n");
lt.append("已接入服务器..."+"\n"+"\n");
while(true){
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//接受消息
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=br.readLine();
lt.append("★ 服务器:"+info+" "+sm.format(new Date())+"\n"+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new khjiemian();
}
public void actionPerformed(ActionEvent w) {
if(w.getSource()==fs){
try {
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//发消息
String infotofuwu=sr.getText();
pw=new PrintWriter(s.getOutputStream(),true);
pw.println(infotofuwu);
lt.setFont(new java.awt.Font("宋体",0,15));
lt.append("☆ 客户端:"+infotofuwu+" "+sm.format(new Date())+"\n"+"\n");
sr.setText("");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}