IP
try {
InetAddress ia = InetAddress.getByName("www.baidu.com");
InetAddress myHost = InetAddress.getLocalHost();
System.out.println(myHost); // 连带主机名与ip
System.out.println("---------");
System.out.println(ia.getHostAddress()); // 获取ip地址
System.out.println(ia.getCanonicalHostName()); // 获取规范的名字
System.out.println(ia.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
output:
LAPTOP-K7UPH2AE/192.168.0.102
---------
110.242.68.3
110.242.68.3
www.baidu.com
端口
public static void main(String[] args) {
InetSocketAddress isa = new InetSocketAddress("www.baidu.com", 3306);
InetAddress address = isa.getAddress();
System.out.println(address);
System.out.println(isa.getHostName());
System.out.println(isa.getPort());
}
output:
www.baidu.com/110.242.68.3
www.baidu.com
3306
通信协议
TCP聊天
客户端:
public static void main(String[] args) throws Exception {
InetAddress ip = InetAddress.getByName("localhost");
Socket socket = new Socket(ip, 9999);
OutputStream os = socket.getOutputStream();
os.write("你好,我叫王一赫,我们可以交个朋友吗?".getBytes());
os.close();
socket.close();
}
服务端:
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
Socket accept = server.accept();
InputStream is = accept.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos);
baos.close();
is.close();
accept.close();
server.close();
}
TCP文件上传
客户端
{
Socket socket = new Socket(InetAddress.getByName("localhost"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream("2.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos);
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
服务端
{
ServerSocket server = new ServerSocket(9000);
Socket accept = server.accept();
InputStream is = accept.getInputStream();
FileOutputStream fos = new FileOutputStream("out.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
OutputStream os = accept.getOutputStream();
os.write("我接受到了".getBytes());
os.close();
fos.close();
is.close();
accept.close();
server.close();
}
UDP
UDP 消息发送
接收端
{
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9900);
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, 0, data.length);
socket.receive(packet);
System.out.println(packet.getAddress());
System.out.println(new String(packet.getData(), 0, packet.getLength()));
socket.close();
}
}
发送端
{
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String data = "你好,接收端";
DatagramPacket packet = new DatagramPacket(data.getBytes(), 0, data.getBytes().length, InetAddress.getByName("localhost"), 9900);
socket.send(packet);
socket.close();
}
}