Java小项目之:教你做个聊天系统!

Java小项目之:聊天系统

今天给大家带来的java练手小项目是一个简单的聊天室,界面简单,操作不难。

分为注册系统,登录系统和聊天系统三部分,很适合java小白练手。

完整的源码和素材请关注并私信我获取!

界面展示:

Java小项目之:教你做个聊天系统!

Java小项目之:教你做个聊天系统!

代码展示:

package chatRoom2;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.Socket;

public class Client {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Socket socket = null;

try {

socket = new Socket("127.0.0.1", 8000);

new ClientWriterThread(socket).start();

new ClientReaderThread(socket).start();

} catch (Exception e) {

e.printStackTrace();

}

}

}

class ClientWriterThread extends Thread {

private Socket socket;

private BufferedReader in;

private PrintWriter out;

public ClientWriterThread(Socket socket) {

super();

this.socket = socket;

try {

in = new BufferedReader(new InputStreamReader(System.in));

out = new PrintWriter(new OutputStreamWriter(

socket.getOutputStream()), true);

} catch (Exception e) {

e.printStackTrace();

}

}

@Override

public void run() {

while (true) {

try {

String str = in.readLine();

if (str == null || "exit".equals(str)) {

break;

}

out.println(str);

} catch (Exception e) {

e.printStackTrace();

break;

}

}

}

@Override

public String toString() {

return "ClientWriterThread [socket=" + socket + "]";

}

}

class ClientReaderThread extends Thread {

private Socket socket;

private BufferedReader in;

public ClientReaderThread(Socket socket) {

super();

this.socket = socket;

try {

in = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

} catch (Exception e) {

e.printStackTrace();

}

}

@Override

public void run() {

while (true) {

try {

String str = in.readLine();

if ("exit".equals(str)) {

break;

}

System.out.println("服务器说:" + str);

} catch (Exception e) {

e.printStackTrace();

break;

}

}

}

@Override

public String toString() {

return "ClientReaderThread [socket=" + socket + "]";

}

}

喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!

祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

上一篇:VSS SVN GIT SVN 加锁签出


下一篇:IT兄弟连 HTML5教程 HTML5技术的应用现状及HTML5平台的兴起