在此中我们需要创建4个类
一个类专门用来发送数据socket,一个类专门用来接听数据和发送用户名client,一个类类似于服务器,可以存贮id地址和屏蔽某人的一些小功能,另外一个是同步线程类。处理相关的信息和给client发送信息。
以下是相关代码实现:
socket:sender
package classroom1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Sender {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Socket socket =new Socket("127.0.0.1", 8080);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
while(!(str=br.readLine()).equalsIgnoreCase("exit")){//条件应该是输入的字符str=br.readLine()
bw.write(str);
bw.newLine();
bw.flush();
}
socket.shutdownOutput();
bw.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------
client类
package classroom1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入用户名");
String name=new Scanner(System.in).next();
login(name);
start();
}
/**
* 显示所有服务器发送的信息
*/
public static void start(){
ServerSocket ss=null;
try {
ss=new ServerSocket(9090);
while(true){
Socket socket=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void login(String name){
/*将用户名发送给服务器*/
try {
Socket socket=new Socket("127.0.0.1", 8080);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(name);
bw.flush();
bw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
----------------------------------------------------
server类:
package classroom1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
/**
* 键:IP地址
* 值:用户名称
* @param args
*/
static Map<String , String>users=Collections.synchronizedMap(new HashMap<String,String>());
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss=new ServerSocket(8080);
ExecutorService es=Executors.newCachedThreadPool();
long start=0;
boolean flag=false;
while(true){
Socket socket=ss.accept();
/*拉黑*/
if("10.20.160.12".equals(socket.getInetAddress().getHostAddress())){
continue;
}
String ip=socket.getLocalAddress().getHostAddress();
/*禁言30s*/
if("10.20.160.232".equals(ip)){
System.out.println("黑户口来了");
if(start==0){
start=System.currentTimeMillis();
}else{
long end=System.currentTimeMillis();
if(end-start>30000){
flag=true;
}
}
if(flag==false){
continue;
}
}
ServerThread st=new ServerThread(socket);
es.execute(st);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-------------------------------------------------
serverThread类:
package classroom1;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
public class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//创建日志文件
FileOutputStream fos=new FileOutputStream("D:\\android\\abc\\qq\\qq.log",true);
//先获取客户端的IP地址
String ip=socket.getInetAddress().getHostAddress();
String str;
while((str=br.readLine())!=null){
boolean flag=Server.users.containsKey(ip);
if(!flag){
Server.users.put(ip, str);
}
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
String now=sdf.format(new Date());
String info="";//要响应的信息内容
if(flag){
info=Server.users.get(ip)+""+now+"\r\n"+str;
}else{
info="系统消息"+now+"\r\n"+str+"加入聊天室";
}
fos.write(info.getBytes());//写入日志
//遍历users里所有的ip地址,发送info信息
Set<String>ipList=Server.users.keySet();//获取所有的ip地址
System.out.println("<"+ip+">:"+info);
//逐个发送信息
Iterator<String >it=ipList.iterator();
while(it.hasNext()){
Socket socket=new Socket(it.next(), 9090);
OutputStream os=socket.getOutputStream();
os.write(info.getBytes());
os.close();
socket.close();
}
}
fos.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}