- 论坛徽章:
- 0
|
回复 13# Godbach
这是udpclient- import java.io.*;
- import java.net.*;
- public class udpclient {
- private byte[] buffer = new byte[1024];
-
- private DatagramSocket ds = null;
-
- public udpclient() throws Exception {
- ds = new DatagramSocket();
- }
- public final void setSoTimeout(final int timeout) throws Exception {
- ds.setSoTimeout(timeout);
- }
-
- public final int getSoTimeout() throws Exception {
- return ds.getSoTimeout();
- }
-
- public final DatagramSocket getSocket() {
- return ds;
- }
-
- public final DatagramPacket send(final String host, final int port,
- final byte[] bytes) throws IOException {
- DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress
- .getByName(host), port);
- ds.send(dp);
- return dp;
- }
-
- public final void close() {
- try {
- ds.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- public static void main(String[] args) throws Exception {
- udpclient client = new udpclient();
- String serverHost = "10.103.27.12";
- int serverPort = 50001;
- client.send(serverHost, serverPort, ("Hello World!").getBytes());
- }
- }
复制代码 这是udpserver- #include <stdio.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- int main(int argc, char **argv)
- {
- int sockfd;
- struct sockaddr_in servaddr;
- sockfd = socket(PF_INET, SOCK_DGRAM, 0);
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(50001);
- bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
- int n;
- char recvline[1024];
- recvfrom(sockfd, recvline, 1024, 0, NULL, NULL);
- printf("%s\n", recvline);
- close(sockfd);
- }
复制代码 这是加密后
wireshark抓的包
|
|