socket即时消息程序

package com.chat;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
/**
* socket写的一个聊天程序,测试方法:打开多个cmd,输入telnet localhost 10000,即可开始聊天
* @author bingyingao
*
*/
public class Server extends ServerSocket {
private static ArrayList User_List = new ArrayList();
private static ArrayList Threader = new ArrayList();
private static LinkedList Message_Array = new LinkedList();
private static int Thread_Counter = 0;
private static boolean isClear = true;
protected static final int SERVER_PORT = 10001;
protected FileOutputStream LOG_FILE = new FileOutputStream(
"d:/connect.log", true);

public Server() throws FileNotFoundException, IOException {
super(SERVER_PORT);//指定服务器所监控的端口
new Broadcast();

// append connection log
Calendar now = Calendar.getInstance();
String str = "[" + now.getTime().toString()
+ "] Accepted a connection\015\012";
byte[] tmp = str.getBytes();
LOG_FILE.write(tmp);

try {
while (true) {
Socket socket = accept();
new CreateServerThread(socket);
}
} finally {
close();
}
}

public static void main(String[] args) throws IOException {
new Server();
}

// --- Broadcast
class Broadcast extends Thread {
public Broadcast() {
start();
}

public void run() {
while (true) {
if (!isClear) {
String tmp = (String) Message_Array.getFirst();

for (int i = 0; i < Threader.size(); i++) {
CreateServerThread client = (CreateServerThread) Threader
.get(i);
client.sendMessage(tmp);
}

Message_Array.removeFirst();
isClear = Message_Array.size() > 0 ? false : true;
}
}
}
}

// --- CreateServerThread
class CreateServerThread extends Thread {
private Socket client;
private BufferedReader in;
private PrintWriter out;
private String Username;

public CreateServerThread(Socket s) throws IOException {
client = s;
in = new BufferedReader(new InputStreamReader(client
.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
out.println("--- Welcome to this chatroom ---");
out.println("Input your nickname:");
start();
}

public void sendMessage(String msg) {
out.println(msg);
}

public void run() {
try {
int flag = 0;
Thread_Counter++;
String line = in.readLine();

while (!line.equals("bye")) {
if (line.equals("l")) {
out.println(listOnlineUsers());
line = in.readLine();
continue;
}

if (flag++ == 0) {
Username = line;
User_List.add(Username);
out.println(listOnlineUsers());
Threader.add(this);
pushMessage("[< " + Username + " come on in >]");
} else {
pushMessage("<" + Username + ">" + line);
}

line = in.readLine();
}

out.println("--- See you, bye! ---");
client.close();
} catch (IOException e) {
} finally {
try {
client.close();
} catch (IOException e) {
}

Thread_Counter--;
Threader.remove(this);
User_List.remove(Username);
pushMessage("[< " + Username + " left>]");
}
}

private String listOnlineUsers() {
String s = "-+- Online list -+-\015\012";

for (int i = 0; i < User_List.size(); i++) {
s += "[" + User_List.get(i) + "]\015\012";
}

s += "-+---------------------+-";
return s;
}

private void pushMessage(String msg) {
Message_Array.addLast(msg);
isClear = false;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值