服务器端:
package chatingRoomThread;
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 TestServerThread {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(8888);
Socket socket = s.accept();
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
Thread t1 = new Thread(new MyServerReader(dis));
Thread t2 = new Thread(new MyServerWriter(dos));
t1.start();
t2.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyServerReader implements Runnable{
private DataInputStream dis;
public MyServerReader(DataInputStream dis) {
this.dis = dis;
}
@Override
public void run() {
// TODO Auto-generated method stub
String info;
try {
while(true){
info = dis.readUTF();
System.out.println("客户端说:"+info);
if(info.equals("bye")){
System.out.println("客户端下线,程序退出");
System.exit(0);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyServerWriter implements Runnable{
private DataOutputStream dos;
public MyServerWriter(DataOutputStream dos) {
this.dos = dos;
}
@Override
public void run() {
// TODO Auto-generated method stub
String info;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
while(true){
info = br.readLine();
dos.writeUTF(info);
if(info.equals("bye")){
System.out.println("服务器下线,程序退出");
System.exit(0);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端:
package chatingRoomThread;
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 TestClientThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket s;
try {
s = new Socket("localhost", 8888);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
Thread t1 = new Thread(new MyClientWriter(dos));
Thread t2 = new Thread(new MyClientReader(dis));
t1.start();
t2.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyClientReader implements Runnable{
private DataInputStream dis;
public MyClientReader(DataInputStream dis) {
this.dis = dis;
}
@Override
public void run() {
// TODO Auto-generated method stub
String info;
try {
while(true){
info = dis.readUTF();
System.out.println("服务器说:"+info);
if(info.equals("byr")){
System.out.println("服务器端下线");
System.exit(0);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyClientWriter implements Runnable{
private DataOutputStream dos;
public MyClientWriter(DataOutputStream dos) {
super();
this.dos = dos;
}
@Override
public void run() {
// TODO Auto-generated method stub
String info;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while(true){
try {
info = br.readLine();
dos.writeUTF(info);
if(info.equals("byr")){
System.out.println("客户端下线");
System.exit(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
基本流程为:客户端写-》服务器读-》服务器写-》客户端读-》客户端写 其中每个端口写的次数是不限的,所以读的次数也是不限的,这就实现了多线程