⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppserverconnection.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:


package com.sri.oaa2.pp;

import java.net.*;
import java.io.*;
import java.util.*;

public class ppServerConnection extends Thread{

  int mPortNumber;
  ppListener mListener;
  ServerSocket mServerSocket;
  Socket mClientSocket;
  int mSocketId;
  Hashtable mClients = new Hashtable();
  ppServices mParent;
  boolean done = false;

  public ppServerConnection(ppServices inParent, int inPortNumber, ppListener inListener) {
    mPortNumber = inPortNumber;
    mListener = inListener;
    mParent = inParent;
    
    try {
      mServerSocket = new ServerSocket(mPortNumber);
    } catch (IOException newE) {
      mSocketId = -1;
      mListener.ppHandleMessage(new ppEvent(ppEvent.PP_MSG_CANNOT_LISTEN,0,null,0));
    }
    mSocketId = mServerSocket.hashCode();
    mParent.mServerConnections.put(new Integer(mSocketId), this);
  }

  public void run() {
    while (!done) {
      try {
        mClientSocket = mServerSocket.accept();
      } catch (IOException eAccept) {
        mListener.ppHandleMessage(new ppEvent(ppEvent.PP_MSG_CANNOT_ACCEPT,0,null,0));
        done = true;
      }
      // A new client has connected
      mListener.ppHandleMessage(new ppEvent(ppEvent.PP_MSG_CLIENT_CONNECT,mClientSocket.hashCode(),null,0));
      ppRemoteClientConnection newClient = new ppRemoteClientConnection(this, mClientSocket, mListener);
      mClients.put(new Integer(mClientSocket.hashCode()), newClient);
      newClient.start();
    }
  }

  // Currently sends data to ALL connected clients (broadcast)
  public synchronized void sendData(byte inData[], int inSize) {
    for (Enumeration enum = mClients.elements(); enum.hasMoreElements();) {
      ppRemoteClientConnection currentClient = (ppRemoteClientConnection)enum.nextElement();
      currentClient.sendData(inData, inSize);
    }
  }

  public synchronized void disconnect() {
    try {
      mServerSocket.close();
    }catch(IOException EClose) {};
    mClients.clear();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -