Closeable自定义关闭类
package TalkTCPSocket;
import java.io.Closeable;
public class Utils {
public static void close(Closeable... targets) {
for (Closeable target : targets) {
try {
if (null != target) {
target.close();
}
} catch (Exception e) {
}
}
}
}
Client
客户发送端
package TalkTCPSocket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 发送端
* @author pmc
*
*/
public class TChatClient4Send implements Runnable{
private BufferedReader re;
private DataOutputStream out;
private Socket client;
private boolean isRunning;
public TChatClient4Send(Socket client){
this.client=client;
this.isRunning=true;
System.out.println("Send:"+isRunning);
try {
re=new BufferedReader(new InputStreamReader(System.in));
out=new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
System.out.println("C_1");
cls();
}
}
//发送消息
public void msgs(String msg){
try {
out.writeUTF(msg);
out.flush();
} catch (IOException e) {
System.out.println("C_2");
cls();
}
}
//获取控制台输入
public String str(){
String msg="";
try {
msg=re.readLine();
return msg;
} catch (IOException e) {
System.out.println("C_3");
cls();
}
return msg;
}
public void cls(){
this.isRunning=false;
Utils.close(re,out,client);
}
@Override
public void run() {
while(isRunning){
String msg=str();
if(!msg.equals("")){
msgs(msg);
}
}
}
}
Client
客户接收端
package TalkTCPSocket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 接收端
* @author pmc
*
*/
public class TChatClient4Receive implements Runnable{
private DataInputStream in;
private Socket client;
private boolean isRunning;
public TChatClient4Receive(Socket client){
this.client=client;
this.isRunning=true;
System.out.println("Receive:"+isRunning);
try {
in=new DataInputStream(client.getInputStream());
} catch (IOException e) {
System.out.println("R_1");
cls();
}
}
public String receive(){
String str = "";
try {
str = in.readUTF();
// System.out.println("服务器返回信息:"+str);
} catch (IOException e) {
System.out.println("R_2");
cls();
}
return str;
}
public void cls(){
this.isRunning=false;
Utils.close(in,client);
}
@Override
public void run() {
while(isRunning){
String msg=receive();
if(!msg.equals("")){
System.out.println(msg);
}
}
}
}
Server
服务端
package TalkTCPSocket;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 在线聊天室:客户端
* 目标:使用多线程实现多个客户可以正常收发多条消息
* 问题:其他客户必须等待之前的客户退出,才能继续排队
*
* @author pmc
*
*/
public class TChatServer4 {
public static void main(String[] args) throws IOException {
System.out.println("-----Server-----");
// 指定端口使用ServerSocket
ServerSocket server = new ServerSocket(8885);
// 阻塞式等待连接accept
while (true) {
Socket client = server.accept();
System.out.println("已建立连接");
// 服务端接收数据
// 读取客户端信息
new Thread(new Chat(client)).start();
}
// server.close();
}
static class Chat implements Runnable{
private DataInputStream re;
private DataOutputStream out;
private Socket client;
private boolean isRunning;
public Chat(Socket client){
this.client=client;
try {
re=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
isRunning=true;
} catch (IOException e) {
close();
}
}
// 接收消息
private String receive() {
String msg = "";
try {
msg = re.readUTF();
return msg;
} catch (IOException e) {
e.fillInStackTrace();
System.out.println("---接收---");
close();
}
return "";
}
// 发送消息
private void send(String msg) {
try {
out.writeUTF(msg);
out.flush();
} catch (IOException e) {
System.out.println("---发送---");
close();
}
}
// 释放资源
private void close() {
this.isRunning=false;
Utils.close(re,out,client);
}
@Override
public void run() {
while(isRunning){
String msg=receive();
System.out.println(msg);
if(!msg.equals("")){
send(msg);
}
}
}
}
}
本文介绍了如何使用Java进行网络编程,通过面向对象封装实现了一个自定义关闭的Closeable类,详细讲解了客户端的发送和接收端的实现,并且在服务端采用了线程封装以提高效率。

957

被折叠的 条评论
为什么被折叠?



