1、TCP(Transmission Control Protocol传输控制协议),是一种面向连接的、可靠的、基于字节流的传输层通信协议,提供两台计算机之间可靠的数据传送,保证数据从一端送至连接的另一端的排列顺序和送出时的顺序相同,可靠性高。
2、端口(port)和套接字(Socket)
网络设计中的端口并非真实的物理存在,只是一个假想的连接装置,规定为0~65535之间的整数。0~1023用于一些知名的网络服务和应用,用户的普通网络应用程序应该使用1024以上的port。
Socket套接字用于将应用程序与端口连接起来。它也是一个假想的连接装置。
3、TCP通信
TCP网络程序设计是指利用Socket类编写通信程序。利用TCP通信的两个应用程序有主次之分,一个称为服务器程序,一个称为客户端程序。
3.1 InetAddress类
方法 | 返回值 | 功能 |
getByName(String host) | InetAddress | Returns:an IP address for the given host name |
getHostAddress() | String | Returns:the raw IP address in a string format. |
getHostName() | String | Returns:the host name for this IP address |
get(int index) | Object | ~~ |
getLocalHost() | InetAddress | Returns the address of the local host. |
toString() | String | Returns:a string representation of this IP address. |
package Nineteen;import java.net.InetAddress;import java.net.UnknownHostException;public class InetAddressgetByName { @SuppressWarnings("null") public static void main(String[] args){ try{ /*Returns: an IP address for the given host name.*/ InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");//www.baidu.com/180.97.33.107 /*Returns: the raw IP address in a string format.*/ String str = inetAddress1 .getHostAddress();//180.97.33.107 /*Returns:the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.*/ String str1 = inetAddress1.getHostName();//www.baidu.com InetAddress inetAddress2 = InetAddress.getLocalHost();//zhic-PC/169.254.25.6 System.out.println(str); System.out.println(str1); System.out.println(inetAddress1.toString()); System.out.println(inetAddress2.toString()); }catch(UnknownHostException e){ e.printStackTrace(); } }}
3、2 ServerSocket
用来表示服务器套接字。一般用于设置端口号和监听,accept()之后,其任务就完成了,剩下的是服务器与客户端通过Socket进行通信。
重要的两个方法:
//构造方法 new ServerSocket(int port);//创建绑定特定端口的服务器套接字//如果多台客户机同时提出连接请求,服务器套接字会将请求连接的客户机存入队列中,队列默认大小50。serverSocket.accept(); //等待客户机连接,连接成功,返回一个服务器创建的新的套接字
3.2 Socket类
客户机创建了Socket对象后,会向指定的IP地址和端口号进行连接。服务器套接字accept()后创建新的套接字与客户端建立连接,成功连接后,可以利用Socket对象的getInputStream()获取套接字的输入流读取(readUTF)接收的消息,getOutputStream()获取套接字的输出流发送消息(writeUTF)。
//Server package Nineteen;import java.net.ServerSocket;import java.net.Socket;import java.io.DataOutputStream;import java.io.DataInputStream;public class Server { public static void main(String[] args){ ServerSocket serverSocket = null; Socket clientSocket = null; String str = null; DataOutputStream out = null; DataInputStream in = null; try{ //java.net.ServerSocket.ServerSocket(int port) throws IOException //Creates a server socket, bound to the specified port serverSocket = new ServerSocket(4331);//创建一个绑定4331端口的服务器套接字 clientSocket = serverSocket.accept();//返回一个服务器创建的新Socket对象,该对象绑定了客户端程序的端口号 in = new DataInputStream(clientSocket.getInputStream());//调用Socket的getInputStream()获得输入流 out = new DataOutputStream(clientSocket.getOutputStream());//获得输出流来发送消息 while(true){ str = in.readUTF();//读传送来的消息 out.writeUTF("Hello. I am Server");//发送消息 out.writeUTF(str);//发送从客户端读进的消息 System.out.println("Server received: "+str); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); }}}
//clientpackage Nineteen;import java.net.Socket;import java.io.DataInputStream;import java.io.DataOutputStream;public class Client { public static void main(String[] args){ String str = null; Socket clientSocket; DataInputStream in = null; DataOutputStream out = null; try{ /*java.net.Socket.Socket(String host, int port) throws UnknownHostException, IOException Creates a stream socket and connects it to the specified port number on the named host.创建一个套接字与127.0.0.1的4331端口连接 */ clientSocket = new Socket("127.0.0.1",4331); in = new DataInputStream(clientSocket.getInputStream());//读消息 out = new DataOutputStream(clientSocket.getOutputStream());//发送消息 //void java.io.DataOutputStream.writeUTF(String str) throws IOException out.writeUTF("Hi");//向发送流中写消息 while(true){ //Open Declaration String java.io.DataInputStream.readUTF() throws IOException str = in.readUTF();//读服务器发送的消息 out.writeUTF(((int)(Math.random()*10)+1)+"");//向发送流中写消息 System.out.println("Client received: "+str); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); } }}