Java与ActionScript的Socket(1)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cn.vicky.socket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
/**
*
* @author Vicky
* 广播类
*/
public class BManager extends Vector<Socket> {
/**
* 对所有已经连接上的socket发送消息
* @param msg
* @throws IOException
*/
public synchronized void sendToAll(String msg) throws IOException {
Socket socket = null;
PrintWriter writer = null;
for (int i = 0; i < size(); i++) {
socket = get(i);
if (socket != null) {
writer = new PrintWriter(socket.getOutputStream(),true);
if (writer != null) {
writer.println(msg);
}
}
}
}
/**
* 向某个人发送消息
* @param msg
* @param socket
* @throws IOException
*/
public synchronized void sendToOne(String msg, Socket socket) throws IOException {
PrintWriter writer = null;
if (socket != null && contains(socket)) {
writer = new PrintWriter(socket.getOutputStream(),true);
if (writer != null) {
writer.println(msg);
}
}
}
/**
* 向所有人发送当前在线人数
* @throws IOException
*/
public synchronized void sendInfo() throws IOException {
String msg = "当前人数:" + size() + "人";
sendToAll(msg);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cn.vicky.socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Vicky
* 聊天系统服务器
*/
public class ChatServer {
private static final Logger logger
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
Java中的字符串也是一连串的字符。但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理。将字符串作为内置的对象处理允许Java提供十分丰富的功能特性以方便处理字符串。下面是一些使用频率比较高的函数及其相关说明。
substring()
它有两种形式,第一种是:String substring( ......
华杰教育是新乡最专业的软件实训基地,思科网络工程师考试中心、 JAVA、.NET、php软件培训中心、计算机等级考试培训中心 是豫北地区最大的IT软件教育机构!
软件实训:
课程分为.net和java两个方向,有实训早餐和短期软件培训,
《实训早餐》项目:针对计算机及 ......
有许多人学了很长时间的Java,但一直不明白hashCode方法的作用,
我来解释一下吧。首先,想要明白hashCode的作用,你必须要先知道Java中的集合。
总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set。
你知道它们的区别吗?前者集合内的元素是有序的,元素可以重复;后者元素无序,但元素不 ......