JAVA local and raspberry pie in the use of UDP transfer text, pictures

Today solved a puzzle for several days, because the game needs, you need to use java language, and the use of UDP transport protocol, make raspberry pie server (that is, the machine) to establish a connection to transmit video and pictures.

Because UDP is a connectionless protocol built on, so to come across a very embarrassing problem is that the client has been sent, but is not receiving data at the local server, the most disgusting is simply not being given, they simply do not know is wrong in which of. After we talked about access to information, and finally solve the problem,

1. First, the first Raspberry Pi and the local server firewalls are turned off.

2. The second is the code number of parameters to be noted that the specific code is as follows:

Copy the code
package UDPTest;

import java.net.DatagramPacket;  
import java.net.DatagramSocket;  
import java.net.InetAddress;  
 
public class Client {  
   public static void main(String[] args) {  
       try {  
           // create a socket of the sender, IP defaults to the local port number randomly  
           DatagramSocket sendSocket = new DatagramSocket();  
 
           // determine the message you want to send:  
           String mes = "! Recipient Hello!";  
 
           // Since datagrams character array data is stored in the form of transfer, so transmissible data  
           byte[] buf = mes.getBytes();  
 
           // determine the sender's IP address and port number, the address of the local machine address  
           12306 Port = int;   
// here to fill the server's IP address InetAddress ip = InetAddress.getByName("192.168.43.57"); // create a send type of data reported: DatagramPacket sendPacket = new DatagramPacket(buf, buf.length,ip, port); // send data through a socket: sendSocket.send(sendPacket); // determine the buffer memory to accept feedback data, i.e., data stored in a byte array byte[] getBuf = new byte[1024]; // create an accepted type of datagram DatagramPacket getPacket = new DatagramPacket(getBuf, getBuf.length); // accept data through the socket sendSocket.receive(getPacket); // parse the message feedback and prints String backMes = new String(getBuf, 0, getPacket.getLength()); System.out.println ( "returned message Recipient:" + backMes); // close the socket sendSocket.close(); } catch (Exception e) { e.printStackTrace (); } } }
Copy the code

Receiving end code is as follows:

Copy the code
package UDPTest;

import java.net.DatagramPacket;  
import java.net.DatagramSocket;  
import java.net.InetAddress;  
import java.net.SocketAddress;  
 
public class Server {  
   public static void main(String[] args) {  
       try {  
           // OK to accept the IP and port number of the parties, IP address for the local machine address  
           InetAddress ip = InetAddress.getLocalHost();  
           int port = 12306;  
 
           // create a socket of the receiver, and to develop the port number and IP address  
           DatagramSocket getSocket = new DatagramSocket(port, ip);  
 
           // determine the array size datagrams received data  
           byte[] buf = new byte[1024];  
 
           // Create a datagram to accept the type of data stored in buf  
           DatagramPacket getPacket = new DatagramPacket(buf, buf.length);  
 
           // receive data through the socket  
           getSocket.receive(getPacket);  
 
           // parse the message sender's transmission, and prints  
           String getMes = new String(buf, 0, getPacket.getLength());  
           System.out.println ( "each other's messages:" + getMes);  
 
           // get IP and port number of the sender of a datagram, and print  
           InetAddress sendIP = getPacket.getAddress();  
           int sendPort = getPacket.getPort();  
           System.out.println ( "each other's IP address is:" + sendIP.getHostAddress ());  
           System.out.println ( "the other side of the port number is:" + sendPort);  
 
           // get the socket address of the sender datagrams  
           SocketAddress sendAddress = getPacket.getSocketAddress();  
 
           // determine the content of the message to be fed back to the sender, and converts an array of bytes  
           String feedback = "recipient said: I received!";  
           byte[] backBuf = feedback.getBytes();  
 
           // Create the type of sending datagrams  
           DatagramPacket sendPacket = new DatagramPacket(backBuf,  
                   backBuf.length, sendAddress);  
 
           // send data through the socket  
           getSocket.send(sendPacket);  
 
           // close the socket  
           getSocket.close();  
       } catch (Exception e) {  
           e.printStackTrace ();  
       }  
   }  
}  

Guess you like

Origin www.cnblogs.com/shyshy/p/11028660.html