ソケット実装ポイント・ツー・ポイント送信メッセージ
15282 ワード
package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author hualei
* @date Sep 3, 2017 4:26:34 PM
*
*/
public class Client {
private Socket socket;
private String alias;
public Client(){
try {
socket = new Socket("127.0.0.1", 8088);
// socket = new Socket("ip", 8088);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
// System.out.println("enter client ...");
Scanner scanner = null;
try {
//
ServerHandler serverHandler = new ServerHandler(socket);
Thread thread = new Thread(serverHandler);
thread.setDaemon(true);
thread.start();
OutputStream outputStream = socket.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream, "utf-8"), true);
// pw.println("Hello, server!");
//
scanner = new Scanner(System.in);
System.out.println(" :");
alias = scanner.nextLine();
System.out.println(" !" + alias + ", !");
pw.println(alias + " ");
while(true){
pw.println(alias + " :" + scanner.nextLine());
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
scanner.close();
}
}
public static void main(String[] args) {
Client client = new Client();
client.start();
}
private class ServerHandler implements Runnable{
private Socket socket;
public ServerHandler(Socket socket){
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream;
try {
inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
while(true){
System.out.println(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author hualei
* @date Sep 3, 2017 4:26:51 PM
*
*/
public class Server {
private ServerSocket serverSocket;
// private volatile int count;
private List allOut = new ArrayList();
private ExecutorService threadPool = Executors.newFixedThreadPool(10);
private Map aliasOuts = new HashMap();
public Server(){
try {
serverSocket = new ServerSocket(8088);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
// System.out.println("enter server ...");
try {
// socket
while(true){
System.out.println(" ...");
Socket socket = serverSocket.accept();
System.out.println(" ");
//count ++;
//
/*System.out.println(" "+count+" ");
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
//
while(true){
System.out.println(" " + count + " :" + br.readLine());
}*/
ClientHandler clientHandler = new ClientHandler(socket);
threadPool.execute(clientHandler);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.start();
}
private class ClientHandler implements Runnable{
// private int ccount;
private Socket socket;
public ClientHandler(Socket socket /*, int count*/){
this.socket = socket;
//this.ccount = count;
}
@Override
public void run() {
// System.out.println(" "+ccount+" ");
PrintWriter pw = null;
try {
//
OutputStream outputStream = socket.getOutputStream();
pw = new PrintWriter(new OutputStreamWriter(outputStream, "utf-8"), true);
add(pw);
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String content = "";
while((content = br.readLine()) != null){
if(!content.contains(":")){
aliasOuts.put(content.trim(), pw);
}
if(content.startsWith("@") && content.contains(":")){
String pwName = content.substring(content.indexOf("@"), content.indexOf(":"));
aliasOuts.get(pwName).println(content.substring(content.indexOf(":")+1));
}else{
foreach(content);
}
}
// ,
/*InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String content = "";
//
while((content = br.readLine()) != null){
System.out.println(" " + ccount + " :" + content);
}*/
} catch (IOException e) {
e.printStackTrace();
}finally{
remove(pw);
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private synchronized void add(PrintWriter pw){
allOut.add(pw);
}
private synchronized void remove(PrintWriter pw){
allOut.remove(pw);
}
private synchronized void foreach(String content){
Set> entrySet = aliasOuts.entrySet();
Iterator> iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry next = iterator.next();
String key = next.getKey();
if(!key.contains(" ")){
next.getValue().println(content);
}
}
}
}