client:
package com.lzs.net;
import java.io.*;
import java.net.Socket;
/**
* Created by zaish on 2016-3-20.
*/
public class MyTCPClient {
public static void main(String[] args) throws IOException {
//1 创建服务器指定服务器和端口
Socket socket=new Socket("localhost",8888);
new Thread(new Send(socket)).start();
new Thread(new Recieve(socket)).start();
}
}
class Send implements Runnable {
private BufferedReader br;
private DataOutputStream dos;
private boolean isRunning=true;
public Send(){
br=new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket socket) {
this();
try {
dos=new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
isRunning=false;
CloseUtil.close(dos,br);
}
}
public void send(){
String msg=getMsgFromConsole();
if(null!=msg&&!msg.equals("")){
try {
dos.writeUTF(msg);
} catch (IOException e) {
isRunning=false;
CloseUtil.close(dos,br);
}
}
}
private String getMsgFromConsole(){
try {
return br.readLine();
} catch (IOException e) {
return "";
}
}
@Override
public void run() {
while (isRunning){
send();
}
}
}
class Recieve implements Runnable {
private BufferedWriter bw;
private DataInputStream dis;
private boolean isRunning=true;
public Recieve(){
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public Recieve(Socket socket){
this();
try {
dis=new DataInputStream(socket.getInputStream());
} catch (IOException e) {
isRunning=false;
CloseUtil.close(dis,bw);
}
}
public void recieve(){
String msg=null;
try {
msg=dis.readUTF();
} catch (IOException e) {
isRunning=false;
CloseUtil.close(dis,bw);
}
try {
bw.write(msg);
bw.newLine();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (isRunning){
recieve();
}
}
}
server:
package com.lzs.net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zaish on 2016-3-20.
*/
public class MyTCPServer {
private List<MyChannel> list=new ArrayList<MyChannel>();
public static void main(String[] args) throws IOException {
new MyTCPServer().start();
}
private void start()throws IOException {
//1 创建服务器指定端口
ServerSocket ss=new ServerSocket(8888);
while(true){
//2 接受客户端连接,阻塞式
Socket socket=ss.accept();
System.out.println(socket.getChannel()+"建立连接");
MyChannel myChannel=new MyChannel(socket);
new Thread(myChannel).start();
list.add(myChannel);
}
}
private class MyChannel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private boolean isRunning=true;
public MyChannel(Socket socket){
try {
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
isRunning=false;
list.remove(this);
CloseUtil.close(dis,dos);
}
}
private String receive(){
String msg="";
try {
msg=dis.readUTF();
} catch (IOException e) {
isRunning=false;
list.remove(this);
CloseUtil.close(dis,dos);
}
return msg;
}
private void send(String msg){
if("".equals(msg)){
return;
}
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
isRunning=false;
list.remove(this);
CloseUtil.close(dis,dos);
}
}
private void sendOthers(){
String msg=receive();
for(MyChannel m:list){
if(m==this){
continue;
}else {
m.send(msg);
}
}
}
@Override
public void run() {
while (isRunning){
sendOthers();
}
}
}
}