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); } } } //[email protected] 
|