gpt4 book ai didi

java - JAVA 上的服务器-客户端-聊天应用程序示例

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:23 27 4
gpt4 key购买 nike

我已经“谷歌搜索”了很长一段时间,寻找服务器-客户端-聊天应用程序的示例,但我无法真正理解它们。他们中的许多人都使用一个类并从中创建 GUI,我不想直接从它复制。很多示例都没有真正解释如何从客户端向服务器发送消息,然后将消息发送给所有其他客户端。

我正在使用 NetBeans,我想知道是否有一些好的教程或示例可以帮助我解决这个问题?

最佳答案

多线程程序来了:)服务器端有两个类,客户端有一个类。希望您喜欢!

服务器主类:

    import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Main {

public static void main(String[] args) throws IOException {
int MAXCLIENTS = 20;
int port = 4444;
ServerSocket server = null;
Socket clientSocket = null;
// An array of clientsConnected instances
ClientThread[] clientsConnected = new ClientThread[MAXCLIENTS];

try {
server = new ServerSocket(port);
System.out.println("listening on port: " + port);
} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();

}

while (true) {
try {
clientSocket = server.accept();

} catch (IOException e) {
e.printStackTrace();
if (!server.isClosed()){server.close();}
if (!clientSocket.isClosed()){clientSocket.close();}
}

System.out.println("Client connected!");

for (int c = 0; c < clientsConnected.length; c++){
if (clientsConnected[c] == null){
// if it is empty ( null) then start a new Thread, and pass the socket and the object of itself as parameter
(clientsConnected[c] = new ClientThread(clientSocket, clientsConnected)).start();
break; // have to break, else it will start 20 threads when the first client connects :P
}
}
}

}



}

服务器客户端类别:

    import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class ClientThread extends Thread{

private ClientThread[] clientsConnected;
private Socket socket = null;
private DataInputStream in = null;
private DataOutputStream out = null;
private String clientName = null;

//Constructor
public ClientThread(Socket socket, ClientThread[] clientsConnected){
this.socket = socket;
this.clientsConnected = clientsConnected;
}

public void run(){
try {
// Streams :)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

String message = null;

clientName = in.readUTF();

while (true){
message = in.readUTF();

for (int c = 0; c < clientsConnected.length; c++){
if (clientsConnected[c]!= null && clientsConnected[c].clientName != this.clientName){ //dont send message to your self ;)
clientsConnected[c].sendMessage(message, clientName); // loops through all the list and calls the objects sendMessage method.
}
}

}

} catch (IOException e) {
System.out.println("Client disconnected!");
this.clientsConnected = null;
}
}
// Every instance of this class ( the client ) will have this method.
private void sendMessage(String mess, String name){
try {
out.writeUTF(name + " says: " + mess);
} catch (IOException e) {
e.printStackTrace();
}
}

}

最后是客户:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Main
{
public static void main(String[] args) throws IOException {

Main m = new Main();
m.connect();
}

public void connect() throws IOException{
//declare a scanner so we can write a message
Scanner keyboard = new Scanner(System.in);


// localhost ip
String ip = "127.0.0.1";
int port = 4444;
Socket socket = null;

System.out.print("Enter your name: ");
String name = keyboard.nextLine();

try {

//connect
socket = new Socket(ip, port);

//initialize streams
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

//start a thread which will start listening for messages
new ReceiveMessage(in).start();

// send the name to the server!
out.writeUTF(name);

while (true){
//Write messages :)
String message = keyboard.nextLine();
out.writeUTF(message);
}

}
catch (IOException e){
e.printStackTrace();
if (!socket.isClosed()){socket.close();}
}
}

class ReceiveMessage extends Thread{

DataInputStream in;

ReceiveMessage(DataInputStream in){
this.in = in;
}

public void run(){
String message;
while (true){
try {
message = in.readUTF();
System.out.println(message);

} catch (IOException e) {
e.printStackTrace();
}

}
}

}

}

我在 eclipse 中运行服务器,并从 CMD 启动了两个客户端,如下所示:

enter image description here

关于java - JAVA 上的服务器-客户端-聊天应用程序示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com