我有一个可以托管多个客户端的服务器,以及一个可以连接的客户端。我试图能够选择其中一个客户并说,通过 PrintWriter 向他们发送消息。I 下面的代码仅供参考,甚至可能不需要使用。我只是想知道是否有办法向特定客户而不是所有客户发送消息。我不知道从哪里开始..我可以做服务器到客户端和客户端到服务器,但我希望服务器到客户端的通信被发送到特定的客户端
客户:
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
public class Main{
private static Socket socket;
private static PrintWriter printWriter;
private static String inputLine;
private static Thread thread;
public static void main(String[] args) {
// System.out.println("What is the computer's IP address?");
String Ip_addr = JOptionPane
.showInputDialog("Enter the IP number of the server to connect: ");
String IP = Ip_addr;
while (true) {
try {
System.out.println("Client running...");
socket = new Socket(IP, 8080);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
System.out.println(inputLine);
JOptionPane.showMessageDialog(null, inputLine);
}
} catch (Exception e) {
}
}
}
}
服务器:
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Main
{
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static BufferedReader threadReader;
private static String inputLine;
private static Thread thread;
private static PrintWriter printWriter;
public static void main(String[] args) throws IOException
{
Thread cmd = new Thread(new Commands());
cmd.start();
while(ClientThread.Clients < 100) {
Scanner scan = new Scanner(System.in);
try
{
serverSocket = new ServerSocket(8080);
clientSocket = serverSocket.accept();
ClientThread.Clients += 1;
//Desktop.getDesktop().open(new File("C:/Users/Chris/Desktop/Test.txt")); // Opens notepad on Windows.
bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
printWriter = new PrintWriter(clientSocket.getOutputStream(),true);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
try{
thread = new Thread(new ClientThread(clientSocket));
thread.start();
}catch(Exception e){System.out.println("Error 103 = failed to create thread."); break;}
try{
serverSocket.close();
}catch(Exception e){System.out.println("Error 105 = failed to close socket.");}
}
}
}
多个客户端的线程:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;
public class ClientThread extends Thread{
static Socket socket;
BufferedReader in;
PrintWriter out;
String username;
public static int Clients = 0;
static String[] Names = new String[100];
char EOF = (char)0x00;
public ClientThread(Socket s){
socket = s;
}
public void run(){
try{
in = (new BufferedReader(new InputStreamReader(socket.getInputStream())));
out = new PrintWriter(socket.getOutputStream());
System.out.println("Client Connected from " + socket.getInetAddress() + ", " + Clients + " connected.");
Names[Clients-1] = "Client" + (Clients) + " " + socket.getInetAddress();
in.readLine();
Thread.sleep(100);
}catch(Exception e){
System.out.println(("Client Disconected from " + socket.getInetAddress()));
removeClient((socket.getInetAddress()).toString());
}
}
private void removeClient(String Inet) {
int Location;
String[] Storage = new String[Clients];
int j = 0;
for(int i = 0; i < Clients; i++) {
String line = Names[i];
StringTokenizer st = new StringTokenizer(line);
String Name = st.nextToken();
String IP = st.nextToken();
if(IP.equalsIgnoreCase(Inet)) {
//System.out.println("Client removed..");
}else {
Storage[j] = Names[i];
j++;
}
}
for(int i = 0; i < Clients; i++) {
Storage[i] = Names[i];
}
Clients--;
}
}
任何建议都非常感谢,在此先感谢。