자바 채 팅 방 구현 코드
채 팅 방 인터페이스:
원본 코드:
public class ClientFrame extends Frame {
private TextField textFieldContent = new TextField();
private TextArea textAreaContent = new TextArea();
private Socket socket = null;
private OutputStream out = null;
private DataOutputStream dos = null;
private InputStream in = null;
private DataInputStream dis = null;
private boolean flag = false;
/**
* : : _Victor :2016 5 8 9:19:51 :
*
* @param args
*/
public static void main(String[] args) {
new ClientFrame().init();
}
/**
* : : _Victor :2016 5 8 9:20:43 :
*/
private void init() {
this.setSize(300, 300);
setLocation(250, 150);
setVisible(true);
setTitle("WeChatRoom");
//
this.add(textAreaContent);
this.add(textFieldContent, BorderLayout.SOUTH);
textAreaContent.setFocusable(false);
pack();
//
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println(" ");
disconnect();
System.exit(0);
}
});
// textFieldContent
textFieldContent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickEnter();
}
});
//
connect();
new Thread(new ReciveMessage()).start();
}
private class ReciveMessage implements Runnable {
@Override
public void run() {
flag = true;
try {
while (flag) {
String message = dis.readUTF();
textAreaContent.append(message + "
");
}
} catch (EOFException e) {
flag = false;
System.out.println(" ");
// e.printStackTrace();
} catch (SocketException e) {
flag = false;
System.out.println(" ");
// e.printStackTrace();
} catch (IOException e) {
flag = false;
System.out.println(" ");
e.printStackTrace();
}
}
}
/**
* : : : _Victor :2016 5 8 9:49:30
*/
private void onClickEnter() {
String message = textFieldContent.getText().trim();
if (message != null && !message.equals("")) {
String time = new SimpleDateFormat("h:m:s").format(new Date());
textAreaContent.append(time + "
" + message + "
");
textFieldContent.setText("");
sendMessageToServer(message);
}
}
/**
* : : : _Victor :2016 5 8 10:13:48
*
* @param message
*/
private void sendMessageToServer(String message) {
try {
dos.writeUTF(message);
dos.flush();
} catch (IOException e) {
System.out.println(" ");
e.printStackTrace();
}
}
/**
* : socket : : _Victor :2016 5 8 10:00:38
*/
private void connect() {
try {
socket = new Socket("localhost", 8888);
out = socket.getOutputStream();
dos = new DataOutputStream(out);
in = socket.getInputStream();
dis = new DataInputStream(in);
} catch (UnknownHostException e) {
System.out.println(" ");
e.printStackTrace();
} catch (IOException e) {
System.out.println(" ");
e.printStackTrace();
}
}
/**
* : : : _Victor :2016 5 8 10:01:32
*/
private void disconnect() {
flag = false;
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
System.out.println("dos ");
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
System.out.println("dos ");
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("socket ");
e.printStackTrace();
}
;
}
}
}
package com.chat;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class ChatServer {
private List<Client> clients = new ArrayList<>();
/**
* : ChatSever : : _Victor :2016 5 8 10:26:41
*
* @param args
*/
public static void main(String[] args) {
new ChatServer().init();
}
/**
* : chatserver : : _Victor :2016 5 8 10:27:09
*/
private void init() {
System.out.println(" ");
// BindException
ServerSocket ss = null;
Socket socket = null;
try {
ss = new ServerSocket(8888);
} catch (BindException e) {
System.out.println(" ");
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Client client = null;
while (true) {
socket = ss.accept();
System.out.println(" ");
client = new Client(socket);
clients.add(client);
new Thread(client).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class Client implements Runnable {
private Socket socket = null;
InputStream in = null;
DataInputStream din = null;
OutputStream out = null;
DataOutputStream dos = null;
boolean flag = true;
public Client(Socket socket) {
this.socket = socket;
try {
in = socket.getInputStream();
din = new DataInputStream(in);
} catch (IOException e) {
System.out.println(" ");
e.printStackTrace();
}
}
public void run() {
String message;
try {
while (flag) {
message = din.readUTF();
// System.out.println(" :" + message);
forwordToAllClients(message);
}
} catch (SocketException e) {
flag = false;
System.out.println(" ");
clients.remove(this);
// e.printStackTrace();
} catch (EOFException e) {
flag = false;
System.out.println(" ");
clients.remove(this);
// e.printStackTrace();
} catch (IOException e) {
flag = false;
System.out.println(" ");
clients.remove(this);
e.printStackTrace();
}
if (din != null) {
try {
din.close();
} catch (IOException e) {
System.out.println("din ");
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
System.out.println("din ");
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("din ");
e.printStackTrace();
}
}
}
/**
* : : : _Victor :2016 5 8 11:11:59
*
* @param message
* @throws IOException
*/
private void forwordToAllClients(String message) throws IOException {
for (Client c : clients) {
if (c != this) {
out = c.socket.getOutputStream();
dos = new DataOutputStream(out);
forwordToClient(message);
}
}
}
/**
* : : : _Victor :2016 5 8 11:16:12
*
* @throws IOException
*/
private void forwordToClient(String message) throws IOException {
dos.writeUTF(message);
dos.flush();
System.out.println(" !");
}
}
}
원본 다운로드:자바 채 팅 방 코드이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.