日期:2014-05-20 浏览次数:21078 次
import java.io.*;
import java.net.*;
public class SocketTest
{
public static void main(String[] args) throws MalformedURLException, IOException
{
SocketTest server = new SocketTest();
server.await();
}
public void await() throws MalformedURLException, IOException
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(80);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
while (true)
{
Socket socket = null;
InputStream input = new URL("http://www.sina.com").openStream();
OutputStream output = null;
try
{
socket = serverSocket.accept();
output = socket.getOutputStream();
int temp;
while((temp=input.read())>0)
{
output.write(temp);
System.out.print(Integer.toHexString(temp)+" ");
}
output.flush();
output.close();
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
continue;
}
}
}
}