Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
TcpTracts( 利用服务器,用于两个或多个私网之间建立tcp连接,可以用于发文件,传语音等 )

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

package nj.jyz;

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

public class TcpTracts
    extends Thread {
  private static TcpTracts tt;
  static int i = 1, PORT = 8082;

  public static boolean startServer(int port) {
    PORT = port;
    if (tt == null) {
      TcpTracts tt = new TcpTracts();
    }
    tt.run();
    return true;
  }

  public static boolean startServer() {
    if (tt == null) {
      TcpTracts tt = new TcpTracts();
    }
    tt.run();
    return true;
  }

  public static void main(String args[]) {
    TcpTracts tt = new TcpTracts();
    tt.run();
  }

  public void run() {
    ServerSocket server = null;
    Socket client = null;
    try {
      server = new ServerSocket(PORT);
      System.out.println("Web Server is listening on port " +
                         server.getLocalPort());
      for (; ; ) {
        client = server.accept(); //接受客户机的连接请求
        new ConnectionThread(client, i).start();
        i++;
      }
    }
    catch (Exception e) {
      System.out.println(e);
    }
  }
}

/* ConnnectionThread类完成与一个Web浏览器的通信  */
class ConnectionThread
    extends Thread {
  Socket client; //连接Web浏览器的socket字
  int counter; //计数器
  public ConnectionThread(Socket cl, int c) {
    client = cl;
    counter = c;
  }

  private static long lTcp;
  private static Hashtable hTcpTracks;

//以序号索引所有向外TCP通道
  private static Hashtable getHashTcpTrachks() {
    if (hTcpTracks == null) {
      hTcpTracks = new Hashtable();
    }
    return hTcpTracks;
  }

  //private final String   REQUESTTRACTS="requesttrackts";
  //private final String   ANSWERTRACTS="answertrackts:";
  private final String REQUESTTRACTS = "r";
  private final String ANSWERTRACTS = "a:";
  private final String FORMATERROR = "format error";
  private long lTcpIndex;

  public void run() { //线程体
    try {
      String destIP = client.getInetAddress().toString(); //客户机IP地址
      int destport = client.getPort(); //客户机端口号
      System.out.println("Connection " + counter + ":connected to " + destIP +
                         " on port " + destport + ".");
      PrintStream outstream = new PrintStream(client.getOutputStream());
      DataInputStream instream = new DataInputStream(client.getInputStream());
      String inline = instream.readLine(); //读取Web浏览器提交的请求信息
      System.out.println("Received:" + inline);

      // 1 get tcpindex
      if (inline.equals(REQUESTTRACTS)) {
        //用户建立连接后,可通过发送一个REQUESTTRACTS建立一个TCP通道,建立成功系统会回送一个序号
        lTcpIndex = ++lTcp;
        outstream.println(ANSWERTRACTS + lTcpIndex);
      }
      else if (inline.startsWith(ANSWERTRACTS)) {
        //其他用户建立TCP连接后,可通过发送ANSWERTRACTS+序号参与此TCP通道
        lTcpIndex = Integer.parseInt(inline.substring(ANSWERTRACTS.length()));
        outstream.println(ANSWERTRACTS + lTcpIndex);
      }
      else {
        outstream.println(FORMATERROR);
        return;
      }

      // 2 set tcphash by tcp index
      Hashtable hOuts = (Hashtable) getHashTcpTrachks().get(new Long(lTcpIndex));
      if (hOuts == null) {
        hOuts = new Hashtable();
        getHashTcpTrachks().put(new Long(lTcpIndex), hOuts);
      }

      InputStream is = client.getInputStream();
      client.setSoTimeout(3000);
      OutputStream os = client.getOutputStream();
      byte[] b = new byte[1];

      hOuts.put(os, os);

      //3 check input and senddata to other
      is.read(b);
      System.out.println(new String(b));

      OutputStream osother;
      try {
        while (is.read(b) > 0) {
          //用户发送的没
          Enumeration eOuts = hOuts.keys();
          while (eOuts.hasMoreElements()) {
            osother = (OutputStream) eOuts.nextElement();
            if (!osother.equals(os)) {
              osother.write(b);
            }
          }

          System.out.print(new String(b));
        }
      }
      catch (java.net.SocketTimeoutException e) {
        hOuts.remove(os);
        if (hOuts.size() == 0) {
          getHashTcpTrachks().remove(new Long(lTcpIndex));
        }
        os = null;
        is = null;
        e.printStackTrace();
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
      finally {
        hOuts.remove(os);
        if (hOuts.size() == 0) {
          getHashTcpTrachks().remove(new Long(lTcpIndex));
        }
        os = null;
        is = null;
      }

      long m1 = 1; //延时
      while (m1 < 11100000) {
        m1++;
      }
      client.close();
    }
    catch (IOException e) {
      System.out.println("Exception:" + e);
    }
  }
}
//jianghukeji@hotmail.com




相关文章

相关软件