Socket UDP、TCP 简介
1.1 Java 获取 IP、URL
public class IPDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress ip=InetAddress.getByName("gudu");
System.out.println(ip);
System.out.println(ip.getHostAddress());
System.out.println(ip.getHostName());
System.out.println(ip.getLocalHost());
System.out.println("-----------------------获取网络IP------------------------");
InetAddress baiduIP=InetAddress.getByName("www.baidu.com");
System.out.println(baiduIP);
}
}
public class URLDemo {
public static void main(String[] args){
InputStream in=null;
OutputStream out=null;
try {
URL url=new URL("");
URLConnection conn=url.openConnection();
in=conn.getInputStream();
File file =new File("D:URL");
if(!file.exists()){
file.mkdirs();
}
out=new FileOutputStream(file.getPath()+"/"+"pag.png");
byte[] buf=new byte[1024];
int len =-1;
System.out.println("下载开始........");
while(-1!=(len=in.read(buf))){
out.write(buf, 0, len);
}
System.out.println("下载成功........");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2 UDP 服务端\客户端
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket(10003);
System.out.println("接收端启动.........");
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
ds.receive(dp);
byte[] data=dp.getData();
InetAddress ip=dp.getAddress();
int port=dp.getPort();
System.out.println(ip+"说了"+new String(data,0,dp.getLength()));
ds.close();
}
}
public static double convert(byte[] data) throws IOException{
DataInputStream dis =new DataInputStream(new ByteArrayInputStream(data));
double num =dis.readDouble();
dis.close();
return num;
}
public class SendDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(10002);
System.out.println("发送端启动........");
byte[] buf="测试,我爱你".getBytes();
DatagramPacket dp=new DatagramPacket(buf, buf.length,
InetAddress.getByName("127.0.0.1"),10003);
ds.send(dp);
ds.close();
}
}
public static byte[] convert(double num) throws IOException{
byte[] data =null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos =new DataOutputStream(bos);
dos.writeDouble(num);
dos.flush();
data = bos.toByteArray();
dos.close();
return data;
}
}
1.3 TCP 服务端/客户端
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket server=new ServerSocket(10001);
Socket client=null;
InputStream in=null;
System.out.println("服务器启动........");
boolean b=true;
while(b){
client=server.accept( );
in=client.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
InetAddress ip=client.getInetAddress();
System.out.println(ip+"说"+new String(buf,0,len));
}
server.close();
client.close();
}
}
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket client=new Socket("127.0.0.1",10001);
OutputStream out=client.getOutputStream();
out.write("你好Server".getBytes());
client.close();
}
}