echourlconnection.java
来自「孙卫琴 java网络编程详解源代码 用到了以下软件:  」· Java 代码 · 共 48 行
JAVA
48 行
package echo;
import java.net.*;
import java.io.*;
public class EchoURLConnection extends URLConnection{
private Socket connection=null;
public final static int DEFAULT_PORT=8000;
public EchoURLConnection(URL url){
super(url);
}
public synchronized InputStream getInputStream() throws IOException{
if(!connected)connect();
return connection.getInputStream();
}
public synchronized OutputStream getOutputStream() throws IOException{
if(!connected)connect();
return connection.getOutputStream();
}
public String getContentType(){
return "text/plain";
}
public synchronized void connect()throws IOException{
if(!connected){
int port=url.getPort();
if(port<0 || port>65535)port=DEFAULT_PORT;
this.connection=new Socket(url.getHost(),port);
this.connected=true;
}
}
public synchronized void disconnect() throws IOException{
if(connected){
this.connection.close();
this.connected=false;
}
}
}
/****************************************************
* 作者:孙卫琴 *
* 来源:<<Java网络编程精解>> *
* 技术支持网址:www.javathinker.org *
***************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?