好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于Java语言实现Socket通信的实例

基于java语言实现socket 通信

由于近日项目需求,需要在服务器中增加socket通信的功能,接收硬件设备发送的心跳包和相关数据,因此又重新对java的网络编程进行了复习,根据项目的实际情况做了简化的编程,实现了简单的通信过程。

1. socket通信原理

源ip地址和目的ip地址以及源端口号和目的端口号的组合称为套接字。其用于标识客户端请求的服务器和服务。

以下是通过socket套接字实现客户端与服务器端通信的示意图:

在实际应用中,客户端会通过访问服务器的ip和port连接到服务器端,这个过程在服务器和客户端之间创建一个socket,然后通过i/o数据流实现数据传输,也就是socket的通信过程。

2. 服务器端

服务器端模拟接收硬件设备传输的心跳包(一个长度为10的字节数组),服务器端会获取到心跳包以及硬件设备的地址和端口号。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

public class server extends thread{

   //服务器端口号

   private int port;

   private serversocket server = null ;

   public server(){

     //创建一个服务器,同时可以接收10个设备发送的数据

     server = new serversocket(port, 10 );

     system.out.println( "socket服务器启动,开始接受数据" );

   }

   @override

   public void run(){

     while ( true ) {

       socket socket = null ;

       inputstream inputstream = null ;

       outputstream outputstream = null ;

       byte [] inputdata = new byte [ 1024 ];

       try {

         //接收socket数据

         socket = server.accept();

         //获取远程采集机的ip和端口号

         string remoteaddress = socket.getinetaddress().tostring();

         int remoteport = socket.getport();

         inputstream = socket.getinputstream();

         //获取传入的数据长度

         int length = inputstream.read(inputdata);

         //创建输出流向客户端返回信息

         outputstream = socket.getoutputstream();

         if (length == 10 ) {

           //如果长度等于10,说明传入的是心跳包

           system.out.println( "接收到心跳包,客户端信息[" +remoteaddress+ ":" +remoteport+ "]" );

           outputstream.write( "success" .getbytes());

         } else {

           system.out.println( "接收的信息有误." );

           outputstream.write( "failed" .getbytes());

         }

       } catch (ioexception e) {

         e.printstacktrace();

       } finally {

         try {

           if (inputstream != null ) {

             inputstream.close();

           }

           if (outputstream != null ) {

             outputstream.close();

           }

           if (socket != null ) {

             socket.close();

           }

         } catch (ioexception e) {

           e.printstacktrace();

         }

       }

     }

   }

   //省略getter和setter方法

}

3. 客户端

客户端负责每隔一段时间向服务器发送一个心跳包。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

public final class client {

   private string address = "" ;

   private int port = 0 ;

   private socket socket = null ;

   private client() {

   }

   private client(string address, int port) throws ioexception {

     this .address = address;

     this .port = port;

     this .socket = new socket(address, port);

   }

   public static byte [] sendcommand(string address, int port, byte [] data) {

     client client = null ;

     outputstream outputstream = null ;

     inputstream inputstream = null ;

     byte [] recevie = new byte [ 40 ];

     try {

       client = new client(address, port);

       outputstream = client.getsocket().getoutputstream();

       outputstream.write(data);

       system.out.print( "send data success" );

       inputstream = client.getsocket().getinputstream();

       inputstream.read(recevie);

     } catch (ioexception e) {

       e.printstacktrace();

     } finally {

       try {

         inputstream.close();

         outputstream.close();

         client.getsocket().close();

       } catch (ioexception ioe) {

         system.out.print( "ioe when closing resource" );

       }

     }

     return recevie;

   }

   /**

    * 测试函数

    **/

   public static void test() {

     for ( int i = 0 ; i < 10 ; i++) {

       //服务器地址和ip

       string address = "127.0.0.1" ;

       int port = 9000 ;

       //心跳包

       byte [] data = "#$*beat001" .getbytes();

       string receive = new string(client.sendcommand(address, port, data));

       system.err.println( "第" + i + "次发送心跳包" + receive);

       try {

         //每隔2分钟发送一个心跳包

         thread.sleep( 2 * 60 * 1000 );

       } catch (interruptedexception e) {

         e.printstacktrace();

       }

     }

   }

   //省略getter和setter方法

}

4. 小结

目前的这种方式比较像目前共享单车的通信模式,通过心跳包来获取client的ip和port,然后server就可以通过心跳包上传的ip和port主动的向client通信了。其他的物联网项目也可以使用这样的方式,这样就可以实现主动的与物联网设备的通信了,只需要规定好协议和传送数据了。 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/u012449363/article/details/78522554

查看更多关于基于Java语言实现Socket通信的实例的详细内容...

  阅读:10次