日期:2014-05-18 浏览次数:20932 次
package net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketTest {
public static void main(String[] args)
{
try
{
Socket socket1 = new Socket("www.baidu.com",80);
System.out.println(socket1);
InetAddress address = InetAddress.getByName("www.baidu.com");
Socket socket2 = new Socket(address,80);
System.out.println(socket2);
InetAddress host = socket1.getInetAddress();
System.out.println(host);
int port = socket1.getPort();
System.out.println(port);
int localPort = socket1.getLocalPort();
System.out.println(localPort);
InputStream is = socket1.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.close();
is.close();
OutputStream os = socket1.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
OutputStreamWriter osw = new OutputStreamWriter(bos);
osw.write("Hello, off, len");
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}