Java Scocketベースの簡単なマルチスレッドチャットプログラム
クライアントとサービス側があり、グループチャット機能を行うことができる簡単なチャットプログラムです.Scocketとマルチスレッドの知識に関する2つのファイルしかありません.
あまり話さないで直接コードをつける
サーバ側コード:
クライアントコード:
あまり話さないで直接コードをつける
サーバ側コード:
/**
* Created by lean on 2018/7/30.
*/
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Server extends JFrame implements ActionListener{
private Map clients = new HashMap();
private JTextArea msg = new JTextArea(" \r
");
private JButton msgSend = new JButton(" ");
public Server() {
// TODO Auto-generated constructor stub
this.setVisible(true);
this.setSize(500, 650);
this.setLayout(new FlowLayout());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
super.windowClosing(arg0);
System.exit(0);
}
});
msgSend.addActionListener(this);
msgSend.setActionCommand("sendMsg");
msg.setAutoscrolls(true);
msg.setColumns(40);
msg.setRows(30);
JScrollPane spanel = new JScrollPane(msg);
this.add(spanel);
this.add(msgSend);
}
public static void main(String[] args){
new Server().listenClient();
}
public void listenClient(){
int port = 8899;
String temp = "";
// ServerSocket 8899
try {
ServerSocket server = new ServerSocket(8899);
// server Socket ,server accept
while (true) {
System.out.println(" ");
Socket socket = server.accept();
temp = " "+socket.getPort()+": ";
clients.put(socket.getPort(), socket);
this.apppendMsg(temp);
new mythread(socket, this).start();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void apppendMsg(String msg){
this.msg.append(msg+"\r
");
}
public void sendMsgToAll(Socket fromSocket, String msg) {
Set keset = this.clients.keySet();
java.util.Iterator iter = keset.iterator();
while(iter.hasNext()){
int key = iter.next();
Socket socket = clients.get(key);
if(socket != fromSocket){
try {
if(socket.isClosed() == false){
if(socket.isOutputShutdown() == false){
Writer writer = new OutputStreamWriter(
socket.getOutputStream());
writer.write(msg);
writer.flush();
}
}
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String temp = "";
if("sendMsg".equals(e.getActionCommand())){
System.out.println(" ");
Set keset = this.clients.keySet();
java.util.Iterator iter = keset.iterator();
while(iter.hasNext()){
int key = iter.next();
Socket socket = clients.get(key);
try {
if(socket.isClosed() == false){
if(socket.isOutputShutdown() == false){
temp = " "+socket.getPort()+" ";
System.out.println(temp);
Writer writer = new OutputStreamWriter(
socket.getOutputStream());
this.apppendMsg(temp);
writer.write(" ");
writer.flush();
}
}
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
class mythread extends Thread{
private Socket socket = null;
private Server server = null;
private InputStreamReader reader = null;
char chars[] = new char[64];
int len;
private String temp = null;
public mythread(Socket socket, Server server) {
// TODO Auto-generated constructor stub
this.socket = socket;
this.server = server;
init();
}
private void init(){
try {
reader = new InputStreamReader(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(" ");
while(true){
try {
System.out.println(" "+this.getId()+": ——>");
while ((len = ((Reader) reader).read(chars)) != -1) {
temp = new String(chars, 0, len);
System.out.println(" "+socket.getPort()+" :" +temp);
server.apppendMsg(" "+socket.getPort()+" :" +temp);
server.sendMsgToAll(this.socket, " "+socket.getPort()+" :" +temp);
}
if(socket.getKeepAlive() == false){
((Reader) reader).close();
// temp = " "+this.getId()+"——> ";
// System.out.println(temp);
temp = " "+socket.getPort()+": ";
server.apppendMsg(temp);
socket.close();
this.stop();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
try {
((Reader) reader).close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
クライアントコード:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class CKClient extends JFrame implements ActionListener{
// ,
String host = "127.0.0.2"; // IP
int port = 8899; //
mythread1 thread = null;
Socket client = null;
Writer writer = null;
private JTextArea msg = new JTextArea(" \r
");
private JTextArea input = new JTextArea();
private JButton msgSend = new JButton(" ");
public CKClient() {
// TODO Auto-generated constructor stub
initSocket();
this.setVisible(true);
this.setSize(550, 750);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
super.windowClosing(arg0);
try {
if(client != null){
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(thread != null){
thread.stop();
}
System.exit(0);
}
});
input.setColumns(40);
input.setRows(10);
input.setAutoscrolls(true);
msgSend.addActionListener(this);
msgSend.setActionCommand("sendMsg");
msg.setAutoscrolls(true);
msg.setColumns(40);
msg.setRows(25);
JScrollPane spanel = new JScrollPane(msg);
JScrollPane editpanel = new JScrollPane(input);
this.add(spanel);
this.add(editpanel);
this.add(msgSend);
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
new CKClient();
}
public void initSocket(){
try {
client = new Socket(this.host, this.port);
writer = new OutputStreamWriter(client.getOutputStream());
//
thread = new mythread1(client, this);
thread.start();
this.appendMsg(" ");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
this.appendMsg(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
this.appendMsg(" ");
}
}
public void appendMsg(String msg){
this.msg.append(msg+"\r
");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String temp = "";
try {
if("sendMsg".equals(e.getActionCommand())){
if((temp = this.input.getText()) != null){
writer.write(temp);
writer.flush();
this.appendMsg(" ("+this.client.getLocalPort()+") ——>"+temp);
this.input.setText("");
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class mythread1 extends Thread {
private Socket socket = null;
private Reader reader = null;
private int len = 0;
char chars[] = new char[64];
private CKClient client = null;
private String temp = "";
public mythread1(Socket socket, CKClient client) {
// TODO Auto-generated constructor stub
this.socket = socket;
this.client = client;
try {
reader = new InputStreamReader(socket.getInputStream());
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println(" "+this.getId()+" ");
while (true) {
try {
if (socket.isClosed() == false) {
if (socket.isInputShutdown() == false) {
while ((len = ((Reader) reader).read(chars)) != -1) {
temp = " ——>"+":"+ new String(chars, 0, len);
client.appendMsg(temp);
System.out.println();
}
}
} else {
if (socket.getKeepAlive() == false) {
reader.close();
socket.close();
this.stop();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}