Java是一種面向對象的編程語言,被廣泛應用于各種領域中的軟件開發。Java通信可以實現不同設備之間的信息交流。
import java.net.*; import java.io.*; public class UDPClient{ public static void main(String args[]){ DatagramSocket aSocket = null; try { aSocket = new DatagramSocket(); byte [] m = args[0].getBytes(); InetAddress aHost = InetAddress.getByName(args[1]); int serverPort = 6789; DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, serverPort); aSocket.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket.receive(reply); System.out.println("Reply: " + new String(reply.getData())); }catch (SocketException e){ System.out.println("Socket: " + e.getMessage()); }catch (IOException e){ System.out.println("IO: " + e.getMessage()); }finally { if(aSocket != null) aSocket.close(); } } }
C語言是一種強大的編程語言,在嵌入式系統和網絡編程中得到了廣泛的應用。C語言通信可以實現各種設備之間的消息傳遞。
#include#include #include #include #include #define PORT 9096 int main(void) { int sockfd; struct sockaddr_in servaddr, cliaddr; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servaddr.sin_port = htons(PORT); char buffer[1024]; printf("Enter a message to send: "); fgets(buffer, sizeof(buffer), stdin); sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr*)&servaddr, sizeof(servaddr)); printf("Message sent!\n"); close(sockfd); return 0; }