多线程通信(服务端):
package cn.socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
/**
* @author SE-Mr.Wang
* @Date:2018-04-20
*/
public static void main(String[] args) {
try {
//创建服务端套接字对象
ServerSocket ss = new ServerSocket(8888);
//调用accept方法
Socket s = ss.accept();
//打开输入流
InputStream is = s.getInputStream();
//封装输入流
DataInputStream dis = new DataInputStream(is);
//创建输入流线程
new MyServerReader(dis).start();
/**
* @info 若实现Runnable接口则
* Thread t = new Thread(new MyServerReader(dis));
* t.start();
*/
//打开输出流
OutputStream os = s.getOutputStream();
//封装输出流
DataOutputStream dos = new DataOutputStream(os);
//创建输出流线程
new MyServerWriter(dos).start();
/**
* @info 若实现Runnable接口则
* Thread t2 = Thread(new MyServerWriter(dos));
* t2.start();
*/
} catch (IOException e) {
e.printStackTrace();
}
}
//输入流线程内部静态类
static class MyServerReader extends Thread{
private DataInputStream dis;
public MyServerReader(DataInputStream dis){
this.dis = dis;
}
public void run(){
String info = "";
while(true){
try {
info = dis.readUTF();
if(!info.equals("")){
System.out.println("客户端发来消息:"+info);
}
if(info.equals("结束")){
System.out.println("客户端结束聊天,本次聊天进程结束!!!");
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//输出流线程内部静态类
static class MyServerWriter extends Thread{
private DataOutputStream dos;
public MyServerWriter(DataOutputStream dos){
this.dos = dos;
}
public void run(){
String info = "";
//读取键盘输入流
InputStreamReader isr = new InputStreamReader(System.in);
//封装键盘输入流
BufferedReader br = new BufferedReader(isr);
while(true){
try {
info = br.readLine();
if(!info.equals("")){
dos.writeUTF(info);
}
if(info.equals("结束")){
System.out.println("已方端结束聊天,本次聊天进程结束!!!");
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
多线程通信(客户端):
package cn.socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientDemo {
/**
* @author SE-Mr.Wang
* @Date:2018-04-20
*/
public static void main(String[] args) {
//创建客户端套接字对象
try {
Socket s = new Socket("localhost",8888);
//打开输入流
InputStream is = s.getInputStream();
//封装输入流
DataInputStream dis = new DataInputStream(is);
//创建输入流线程
new MyClientReader(dis).start();
/**
* @info 若实现Runnable接口则
* Thread t = new Thread(new MyClientReader(dis));
* t.start();
*/
//打开输出流
OutputStream os = s.getOutputStream();
//封装输出流
DataOutputStream dos = new DataOutputStream(os);
//创建输出流线程
new MyClientWriter(dos).start();
/**
* @info 若实现Runnable接口则
* Thread t2 = Thread(new MyClientWriter(dos));
* t2.start();
*/
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//输入流线程内部静态类
static class MyClientReader extends Thread{
private DataInputStream dis;
public MyClientReader(DataInputStream dis){
this.dis = dis;
}
public void run(){
String info = "";
while(true){
try {
info = dis.readUTF();
if(!info.equals("")){
System.out.println("服务端发来消息:"+info);
}
if(info.equals("结束")){
System.out.println("服务端结束聊天,本次聊天进程结束!!!");
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//输出流线程内部静态类
static class MyClientWriter extends Thread{
private DataOutputStream dos;
public MyClientWriter(DataOutputStream dos){
this.dos = dos;
}
public void run(){
String info = "";
//打开键盘输入流
InputStreamReader isr = new InputStreamReader(System.in);
//封装键盘输入流
BufferedReader br = new BufferedReader(isr);
while(true){
try {
info = br.readLine();
if(!info.equals("")){
dos.writeUTF(info);
}
if(info.equals("结束")){
System.out.println("已方端结束聊天,本次聊天进程结束!!!");
System.exit(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}