标签:equals string 图片 获取 super auto exce output static
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学习资料的也可以私信我!
祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!
标签:equals string 图片 获取 super auto exce output static
原文地址:https://blog.51cto.com/14623707/2468843