欢迎来到天天文库
浏览记录
ID:37861073
大小:15.30 KB
页数:3页
时间:2019-06-01
《Java——简单代理服务器实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、这个程序主要实现的是一个简单的代理功能。用户请求代理,代理请求指定页面再原封不动的传回客户端,代码如下:importjava.io.*;importjava.net.*;importjava.util.*;importjava.awt.*;importjavax.swing.*;publicclassProxyServerextendsJFrame{privateJTextAreajta=newJTextArea();//createanJTextAreatooutputsomeinformationpublicstaticvoidmain(String[]arg
2、s){newProxyServer();}publicProxyServer(){setLayout(newBorderLayout());add(newJScrollPane(jta),BorderLayout.CENTER);setTitle("ProxyServerbyLiZhaoji");setSize(500,300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);try{ServerSocketserverSocket=newServerSocket(8081);//Li
3、steningport8081jta.append("ProxyServerstartedat"+newDate()+'');jta.append("ProxyServerislisteningonport"+serverSocket.getLocalPort()+''+'');intclientNo=1;while(true){Socketsocket=serverSocket.accept();jta.append("1*****************************************************************
4、***********1"+'');jta.append("Startingthreadforclient"+clientNo+"at"+newDate()+'');InetAddressinetAddress=socket.getInetAddress();//getthehostPC'sinformationjta.append("Client"+clientNo+"'shostnameis"+inetAddress.getHostName()+'');jta.append("Client"+clientNo+"'sIPAddressis"+ine
5、tAddress.getHostAddress()+'');HandleClienttask=newHandleClient(socket);newThread(task).start();//startanewthreadtohandletherequireofthehostclientNo++;}}catch(IOExceptionex){System.err.println(ex);}}classHandleClientimplementsRunnable{privateSocketsocket;publicHandleClient(Socketsock
6、et){this.socket=socket;}publicvoidrun(){try{BufferedReaderinputFromClient=newBufferedReader(newInputStreamReader(socket.getInputStream()));PrintStreamoutputToClient=newPrintStream(socket.getOutputStream());Stringstr=inputFromClient.readLine();//readtheGETrequirestringfromHostjta.appen
7、d("TherequirefromClient:"+str+'');Stringinurl=str.substring(4,str.indexOf("HTTP/")-1);//gettheURLstringfromGETjta.append("TherequireURL:"+inurl+'');URLurl=newURL(inurl);InputStreaminfile=url.openStream();//theinputstreamthatgetthedatafromWebServerintn=infile.available();//thenumbe
8、rofby
此文档下载收益归作者所有