在使用Java写软件的时候,通常会用到网络编程(Socket)在网络上传输数据,下面我们通过代码给大家展示如何使用Java来使用TCP/IP协议在网络上传输数据。
首先我们要了解网络通信的要点
* 1、IP 和 端口号* 2、网络通信协议 TCP/IP 参考模型(应用层、传输层、网络层、数据链路层)* 3、在 java 中 InetAddress 来表示 IP 地址* 4、域名:www.baidu.com www.github.com* 5、实例化 InetAddress 对象的方法有 实现 getByName() 和 getLocalHost()* 6、两个常用方法 getHostName() getHostAddress()* 7、端口号和IP地址组合成一个套接字 Socket
想要跟网络上的服务器进行通信,首先要知道他的IP和端口;我们知道在Java中InetAddress这个类 就是用来表示IP地址
InetAddress类没有构造方法,所以不能直接new出一个对象;
可以通过InetAddress类的静态方法获得InetAddress的对象;
通过运行结果我们可以看到输出InetAddress对象的时候,是把IP地址输出了,www.baidu.com 我输入域名也能通过DNS域名解析服务器将他的IP地址给解析出来,到这里IP地址我们已经有了,想要和服务器进行通信,我们还需要一个端口号,所以我们引用了Socket,在Java中为了方便我们使用它将 IP 地址和端口号组合成了一个套接字 Socket
下面我们来看看客户端代码应该怎样写。
直接将IP地址和端口写到 Socket 的构造方法中,然后创建输出流,把想要和服务端通信的数据放进去
服务端代码
服务端的代码与客户端的代码大致一样,只是服务端需要将客户端发送的数据接收过来,所以多了一个ByteArrayOutputStream,我们将读取到的数据放到它的对象中,然后在打印出来就完成了整个服务端接收数据的过程,下面看一下运行结果
我可以看到服务端能够接收到客户端发送过来的数据
源码:InetAddressTest 类
package Socket;import java.net.InetAddress;import java.net.UnknownHostException;/*** Created by 高金明 2019/9/20 20:04 * Description 网络通信的要点 * 1、IP 和 端口号 * 2、网络通信协议 TCP/IP 参考模型(应用层、传输层、网络层、数据链路层) * 3、在 java 中 InetAddress 来表示 IP 地址 * 4、域名:www.baidu.com www.github.com * 5、实例化 InetAddress 对象的方法有 实现 getByName() 和 getLocalHost() * 6、两个常用方法 getHostName() getHostAddress() * 7、端口号和IP地址组合成一个套接字 Socket * Version 1.0 */public class InetAddressTest { public static void main(String[] args) { try { InetAddress inet1 = InetAddress.getByName("192.168.123.141"); System.out.println(inet1); //通过域名获取 IP 地址 InetAddress inet2 = InetAddress.getByName("www.baidu.com"); System.out.println(inet2); //获取本地地址 InetAddress inet3 = InetAddress.getLocalHost(); System.out.println(inet3); //获取本地主机名 System.out.println(inet3.getHostName()); //获取本地IP地址 System.out.println(inet3.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } }}
TestTCP 类
package Socket;import org.junit.Test;import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;/*** Created by 高金明 2019/9/20 21:34 * Description 实现 TCP 的网络编程 * 例子1:客户端给服务端发送消息,服务端将信息显示在控制台上 * Version 1.0 */public class TestTCP { //客户端 @Test public void client() { Socket socket = null; OutputStream os = null; try { //创建socket对象,指明服务器端的IP和端口号 InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); socket = new Socket(inetAddress, 8899); //获取一个输出流输出数据 os = socket.getOutputStream(); //写出数据 os.write("我是客户端".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源 if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } //服务端 @Test public void server() { ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //启动服务,并且监听自己8899端口号 serverSocket = new ServerSocket(8899); //等待客户端连接8899端口 socket = serverSocket.accept(); //可以接受来自客户端的数据 is = socket.getInputStream(); //读取输入流中的数据 baos = new ByteArrayOutputStream(); byte[] bytes = new byte[5]; int len; while ((len = is.read(bytes)) != -1) { baos.write(bytes, 0, len); } System.out.println(baos.toString()); System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress()+"的数据"); } catch (IOException e) { e.printStackTrace(); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
原项目下载连接:
https://github.com/InterPargram/course
代码堆
好用的代码一大堆!
邮箱:2712225255@qq.com