package com.zxw.CharRoom2;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static List<MyChannal> list=new ArrayList<MyChannal>();
public static void main(String[] args) throws IOException {
ServerSocket server=new ServerSocket(8888);
while(true) {
Socket client=server.accept();
MyChannal mc=new MyChannal(client);
list.add(mc);
new Thread(mc).start();
}
}
}
package com.zxw.CharRoom2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
public class MyChannal implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private boolean flag=true;
public MyChannal(Socket client) throws IOException {
try {
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
flag=false;
if(dos!=null) {
dos.close();
}
if(dis!=null) {
dis.close();
}
Server.list.remove(this);
}
}
private String receive() throws IOException {
String str="";
try {
str=dis.readUTF();
} catch (IOException e) {
flag=false;
if(dis!=null) {
dis.close();
}
Server.list.remove(this);
}
return str;
}
private void send(String str) throws IOException {
if(str.length()!=0&&str!=null) {
try {
dos.writeUTF(str);
} catch (IOException e) {
flag=false;
if(dos!=null) {
dos.close();
}
Server.list.remove(this);
}
}
}
private void sendOther() throws IOException {
String str=this.receive();
List<MyChannal> list=Server.list;
for (MyChannal other : list) {
if(other==this) {
continue;
}
other.send(str);
}
}
@Override
public void run() {
try {
sendOther();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.zxw.CharRoom2;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client=new Socket("127.0.0.1", 8888);
Send send=new Send(client);
Receive receive=new Receive(client);
new Thread(send).start();
new Thread(receive).start();
}
}
package com.zxw.CharRoom2;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Send implements Runnable{
private BufferedReader br;
private DataOutputStream dos;
private boolean flag=true;
public Send() {
br=new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket client) throws IOException {
this();
try {
dos=new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
}
}
private void send(String str) throws IOException {
try {
dos.writeUTF(str);
} catch (IOException e) {
flag=false;
if(dos!=null) {
dos.close();
}
}
}
private String getMessage() throws IOException {
String str="";
try {
str=br.readLine();
} catch (IOException e) {
flag=false;
if(br!=null) {
br.close();
}
}
return str;
}
@Override
public void run() {
while(flag) {
try {
this.send(getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.zxw.CharRoom2;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
public class Receive implements Runnable{
private DataInputStream dis;
private boolean flag=true;
public Receive(Socket client) throws IOException {
try {
dis=new DataInputStream(client.getInputStream());
} catch (IOException e) {
flag=false;
if(dis!=null) {
dis.close();
}
}
}
private String getMessage() throws IOException {
String str="";
try {
str=dis.readUTF();
} catch (IOException e) {
flag=false;
if(dis!=null) {
dis.close();
}
}
return str;
}
@Override
public void run() {
try {
System.out.println(this.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}