1.发送客户端代码
package com.dasenlin.threadchart;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 多线程的发送客户端
* @author Administrator
*
*/
public class Send implements Runnable{
private BufferedReader console;//控制台的输入流
private DataOutputStream dos;//管道的输出流
private boolean isRunning =true;//控制线程的标志符
public Send(){
console = new BufferedReader(new InputStreamReader(System.in));
}
/**
* 发送控制台的输入
* @param client
*/
public Send(Socket client){
this();//调用的是这里的无参默认的构造方法,必须是第一句
try {
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
isRunning =false;
IoCloserUtil.closeAll(dos,console);
}
}
/**
* 从控制台获取数据信息
* @return
*/
public String getMsgFromConsole(){
try {
return console.readLine();
} catch (IOException e) {
}
return "";
}
/**
* 从控制台获取数据
* 从客户端发送出去
*/
public void send(){
String msg = getMsgFromConsole();
try {
if(null!=msg && !msg.equals("")){
dos.writeUTF(msg);
dos.flush();
}
} catch (IOException e) {
isRunning =false;
IoCloserUtil.closeAll(dos,console);
}
}
/**
* 循环而已
*/
@Override
public void run() {
//线程体
while(isRunning){
send();
}
}
}
2.工具类
package com.dasenlin.threadchart;
import java.io.Closeable;
import java.io.IOException;
/**
* 关闭流
* @author Administrator
*
*/
public class IoCloserUtil {
/**
* 关闭所有流的方法
* @param io
*/
public static void closeAll(Closeable...io){
for(Closeable temp:io){
try {
if(null!=temp){
temp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.客户端启动代码
package com.dasenlin.threadchart;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* 客户端
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
try {
Socket client = new Socket("localhost",9999);
new Thread(new Send(client)).start();//只需要给一个线程,实例化一个Send();然后调用线程的start()方法;
new Thread(new Recieve(client)).start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.接收端代码
package com.dasenlin.threadchart;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
/**
* 接收端的封装
* @author Administrator
*
*/
public class Recieve implements Runnable{
private DataInputStream dis;
private Boolean isRunning=true;
public Recieve(){
}
public Recieve(Socket client){
try {
dis = new DataInputStream(client.getInputStream());
} catch (IOException e) {
isRunning=false;
IoCloserUtil.closeAll(dis);
}
}
/**
* 接收信息
* @return
*/
public String recieve(){
String msg="";
try {
msg = dis.readUTF();
} catch (IOException e) {
isRunning=false;
IoCloserUtil.closeAll(dis);
}
return msg;
}
/**
* 读取这些数据
*/
@Override
public void run() {
while(isRunning){
System.out.println(recieve());
}
}
}
5.服务器端
package com.dasenlin.threadchart;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
* @author Administrator
*
*/
public class Server {
public static void main(String[] args) {
try {
while(true){
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
//写出数据,输入流
DataInputStream dis = new DataInputStream(client.getInputStream());
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
while(true){
String msg=dis.readUTF();
System.out.println(msg);
dos.writeUTF("服务器"+msg);
dos.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}