Javaベースのマルチユーザオンラインチャットルーム
12997 ワード
プロジェクトの説明
ログイン、終了、チャット、グループチャットをサポートします.
テクノロジーの使用
JavaベースマルチスレッドSocketプログラミング
サービス側コード
クライアントコード
ログイン、終了、チャット、グループチャットをサポートします.
テクノロジーの使用
JavaベースマルチスレッドSocketプログラミング
サービス側コード
package com.github7.Server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultServer {
private static final Map Online_Client = new ConcurrentHashMap<>();
public static void main(String[] args) {
try {
//
ExecutorService executorService = Executors.newFixedThreadPool(10);
//
ServerSocket serverSocket = new ServerSocket(65535);
for (int i = 0; i < 10; i++) {
//
Socket client = serverSocket.accept();
System.out.println(" , " + client.getInetAddress());
executorService.submit(new ExecuteClient(client));
}
} catch (IOException e) {
e.printStackTrace();
}
}
//
public static class ExecuteClient implements Runnable {
private Socket client;
public ExecuteClient(Socket client) {
this.client = client;
}
@Override
public void run() {
try {
InputStream clientInput = client.getInputStream();
Scanner scanner = new Scanner(clientInput);
while (true) {
// , ,
String message = scanner.nextLine();
/*
1. login:name
2. chat:name:content
3. chats:content
4. exit:name
*/
String[] array = message.split(":");
if ("login".equals(array[0])) {
String loginName = array[1];
loginFunction(loginName);
continue;
} else if ("chat".equals(array[0])) {
String toName = array[1];
String content = array[2];
chatFunction(toName, content);
continue;
} else if ("chats".equals(array[0])) {
String alluserMessage = array[1];
chatsFunction(alluserMessage);
continue;
} else if ("exit".equals(array[0])) {
String exitName = array[1];
exitFunction(exitName);
continue;
} else {
sendMessageToClient(this.client, " , ", false);
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessageToClient(Socket toclient, String mess, boolean sendfix) {
try {
OutputStream outputStream = null;
outputStream = toclient.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
String sendFrom = "";
for (Map.Entry entry : Online_Client.entrySet()) {
if (entry.getValue().equals(this.client)) {
sendFrom = entry.getKey();
}
}
//
if (sendfix == true) {
writer.write(sendFrom + "( )-->" + mess + "
");
} else {
writer.write(sendFrom+"-->"+mess + "
");
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loginFunction(String loginName) throws IOException {
boolean hasLogin = false;
for (Map.Entry entry : Online_Client.entrySet()) {
if (entry.getKey().equals(loginName)) {
hasLogin = true;
this.sendMessageToClient(this.client, loginName + "Error!!! 。", false);
printOnlineClient();
break;
}
}
if (hasLogin == false) {
Online_Client.put(loginName, this.client);
printOnlineClient();
this.sendMessageToClient(this.client, loginName + " ", false);
}
}
//
private void chatFunction(String toName, String content) throws IOException {
// Map
Socket toClient = Online_Client.get(toName);
if (toClient == null) {
this.sendMessageToClient(this.client, " , ", false);
} else {
this.sendMessageToClient(toClient, content, false);
}
}
//
private void chatsFunction(String alluserMessage) throws IOException {
for (Map.Entry entry : Online_Client.entrySet()) {
Socket toClient = entry.getValue();
if (!toClient.equals(this.client)) {
this.sendMessageToClient(toClient, alluserMessage, true);
}
}
}
//
private void exitFunction(String exitName) {
for (Map.Entry entry : Online_Client.entrySet()) {
//
if (entry.getValue().equals(this.client)) {
Online_Client.remove(entry.getKey());
}
}
printOnlineClient();
}
//
private void printOnlineClient() {
System.out.println(" :");
for (String clienName : Online_Client.keySet()) {
System.out.println(" -->" + clienName);
}
}
}
}
クライアントコード
package com.github7.Server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultServer {
private static final Map Online_Client = new ConcurrentHashMap<>();
public static void main(String[] args) {
try {
//
ExecutorService executorService = Executors.newFixedThreadPool(10);
//
ServerSocket serverSocket = new ServerSocket(65535);
for (int i = 0; i < 10; i++) {
//
Socket client = serverSocket.accept();
System.out.println(" , " + client.getInetAddress());
executorService.submit(new ExecuteClient(client));
}
} catch (IOException e) {
e.printStackTrace();
}
}
//
public static class ExecuteClient implements Runnable {
private Socket client;
public ExecuteClient(Socket client) {
this.client = client;
}
@Override
public void run() {
try {
InputStream clientInput = client.getInputStream();
Scanner scanner = new Scanner(clientInput);
while (true) {
// , ,
String message = scanner.nextLine();
/*
1. login:name
2. chat:name:content
3. chats:content
4. exit:name
*/
String[] array = message.split(":");
if ("login".equals(array[0])) {
String loginName = array[1];
loginFunction(loginName);
continue;
} else if ("chat".equals(array[0])) {
String toName = array[1];
String content = array[2];
chatFunction(toName, content);
continue;
} else if ("chats".equals(array[0])) {
String alluserMessage = array[1];
chatsFunction(alluserMessage);
continue;
} else if ("exit".equals(array[0])) {
String exitName = array[1];
exitFunction(exitName);
continue;
} else {
sendMessageToClient(this.client, " , ", false);
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessageToClient(Socket toclient, String mess, boolean sendfix) {
try {
OutputStream outputStream = null;
outputStream = toclient.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
String sendFrom = "";
for (Map.Entry entry : Online_Client.entrySet()) {
if (entry.getValue().equals(this.client)) {
sendFrom = entry.getKey();
}
}
//
if (sendfix == true) {
writer.write(sendFrom + "( )-->" + mess + "
");
} else {
writer.write(sendFrom+"-->"+mess + "
");
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loginFunction(String loginName) throws IOException {
boolean hasLogin = false;
for (Map.Entry entry : Online_Client.entrySet()) {
if (entry.getKey().equals(loginName)) {
hasLogin = true;
this.sendMessageToClient(this.client, loginName + "Error!!! 。", false);
printOnlineClient();
break;
}
}
if (hasLogin == false) {
Online_Client.put(loginName, this.client);
printOnlineClient();
this.sendMessageToClient(this.client, loginName + " ", false);
}
}
//
private void chatFunction(String toName, String content) throws IOException {
// Map
Socket toClient = Online_Client.get(toName);
if (toClient == null) {
this.sendMessageToClient(this.client, " , ", false);
} else {
this.sendMessageToClient(toClient, content, false);
}
}
//
private void chatsFunction(String alluserMessage) throws IOException {
for (Map.Entry entry : Online_Client.entrySet()) {
Socket toClient = entry.getValue();
if (!toClient.equals(this.client)) {
this.sendMessageToClient(toClient, alluserMessage, true);
}
}
}
//
private void exitFunction(String exitName) {
for (Map.Entry entry : Online_Client.entrySet()) {
//
if (entry.getValue().equals(this.client)) {
Online_Client.remove(entry.getKey());
}
}
printOnlineClient();
}
//
private void printOnlineClient() {
System.out.println(" :");
for (String clienName : Online_Client.keySet()) {
System.out.println(" -->" + clienName);
}
}
}
}