java Sokcet编程(五)

java网络编程:

ServerSocket和DatagramSokcet

ServerSocket是以TCP的链接方式进行通信的。

DatagramSocket是以UDP的链接方式进行通信的。

如何通过主机名称获得IP地址

InetAddress inet = InetAddress.getByName("www.baidu.com");
System.out.println(inet.getHostAddress());

如何知道谁在访问我的主机

serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
System.out.println ("Connection from : " 
	+"IP address:"+socket.getInetAddress().getHostAddress() 
	+"Port :" + socket.getPort());

如何查询本机IP地址

InetAddress local = InetAddress.getLocalHost();
System.out.println ("Local IP : " + local.getHostAddress());

相关网址:

http://www.oracle.com/technetwork/java/socket-140484.html

http://docs.oracle.com/javase/tutorial/networking/sockets/

http://docs.oracle.com/javase/tutorial/networking/urls/index.html

http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html

http://www.devarticles.com/c/a/Java/Socket-Programming-in-Java/1/

http://docs.oracle.com/javase/tutorial/index.html

http://www.java-tips.org/java-me-tips/midp/how-to-use-http-connection-instead-of-a-stream-socket.html

http://www.cs.swan.ac.uk/~csneal/InternetComputing/ThreadSync.html



 

原文地址:https://www.cnblogs.com/mengjianzhou/p/5986880.html