TCPTRACTS (Using the server, establishing a TCP connection between two or more private networks, can be used for sending files,

xiaoxiao2021-03-06  40

Package nj.jyz;

Import java.io. *; import java.ut. *; import java.util. *;

Public Class Tcptracts Extends Thread {Private Static Tcptracts Tt; Static Int i = 1, Port = 8082;

Public Static Boolean Start Server (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 (); for ;;) {Client = server.Accept (); // Accept the client's connection request New connectionthread (Client, i) .Start (); i ;}} catch (exception e) {system.out.println (e) }}}

/ * ConnnectionThread class does communicate with a Web browser * / class ConnectionThread extends Thread {Socket client; // socket connected Web browser word int counter; // counter public ConnectionThread (Socket cl, int c) {client = cl Counter = C;}

Private static long ltcp; private static hashtable htcptracks;

// With a serial number index all outward TCP channel 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 () {// thread body try {string destip = client.getinetaddress (). Tostring (); // client IP address int destport = client.getPort (); // Client port number System.out.println ( "Connection" counter ": connected to" destIP "on port" destport "."); PrintStream outstream = new PrintStream (client.getOutputStream ()); DataInputStream instream = New DataInputStream ()); string inline = instream.readline (); // reads the request information submitted by the web browser system.out.println ("receive:" inline);

// 1 get tcpindex if (INLINE.EQUESTRACTS)) {// User After establishing a connection, you can establish a TCP channel by sending a RequestTracts, and establishing a successful system will return a serial number LTCPINDEX = LTCP; Outstream.Println (Answertracts LTCPINDEX);} Else If (Inline.StartSwith (Answith)) {// Other Users have established TCP connections, can participate in this TCP channel LTCPINDEX = Integer.ParSeint (INLINE.SUBSTRING (INSWERTRACTS.LENGTH ()) );} Else {outstream.println (formaterror); return;}

// 2 set tcphash by TCP index hashtable houts = (hashtable) getHashtcptrachks (). Get (new long (lope)); if (houts == null) {houts = new hashtable (); getHashtcptrachks (). PUT (NEW long) (ltcpindex);} InputStream is = client.getinputStream (); client.setsotimeout (3000); OutputStream OS = Client.getOutputStream (); Byte [] B = New Byte [1];

Houts.Put (OS, OS);

// 3 Check INPUT AND sentData to other is.read (b); system.out.println (new string (b));

OutputStream Osother; Try {while (is.read (b)> 0) {// User Send No Enumeration Eouts = HOUTS.KEYS (); While (Eouts.haASMoreElements ()) {OSTHER = (OutputStream) Eouts.nexTelement ); If (! Osite.equals (os)) {OS 2her.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 (new long (lope));} OS = null; is = null;}

LONG M1 = 1; // Delay while (m1 <11100000) {m1 ;} client.close ();} catch (ion (ooException e) {system.out.println ("Exception:" e);}}} / /Jianghuikeji@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-76813.html

New Post(0)