udp 测试程序 c & c++ &java 版本 syslog

本程序是接受syslog日志的。

c语言

#include "stdio.h"
#include "stdlib.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#include "netdb.h"
#include "string.h"

#define PORT 514
#define MAXSIZE 8096

int main() {
        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

        struct sockaddr_in my_socket,remote_socket;
        bzero(&my_socket, sizeof(struct sockaddr_in));
        my_socket.sin_family = AF_INET;
        my_socket.sin_addr.s_addr = htonl(INADDR_ANY);
        my_socket.sin_port = htons(PORT);

        bind(sockfd, (struct sockaddr *)&my_socket, sizeof(struct sockaddr_in));

        char buf[MAXSIZE];
        int count=0;
        while(count++<10)
        {
                //socklen_t remoteAddrLength = sizeof(remote_socket);
                //int result = recvfrom(my_socket, &buf, sizeof(buf), 0, (struct sockaddr *)&remote_socket, &remoteAddrLength);
                //printf("=====result=%d remoteAddrLength=%d
",result,remoteAddrLength);
                int n = read(sockfd, buf, MAXSIZE);
                buf[n] = ‘’;
                printf("%s
", buf);
        }
        exit(0);
}

c++

#include <iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main (int argc, char *argv[])
{
        cout << "Hello world!" << endl;
        int localSocket,remoteSocket;
        struct sockaddr_in localAddr,remoteAddr;
        localSocket = socket(AF_INET, SOCK_DGRAM, 0);
        bzero(&localAddr, sizeof(localAddr));
        localAddr.sin_family = AF_INET;
        localAddr.sin_port = htons(514);
        localAddr.sin_addr.s_addr = INADDR_ANY;
        cout << "socket returned : " << localSocket << endl;
        int result = bind(localSocket, (struct sockaddr *)&localAddr, sizeof(localAddr));
        cout << "bind returned : " << result << endl;
        while(1)
        {
                char buffer[52000];
                socklen_t remoteAddrLength = sizeof(remoteAddr);
                result = recvfrom(localSocket, &buffer, sizeof(buffer), 0, (struct sockaddr *)&remoteAddr, &remoteAddrLength);
                //printf("*******ip=%s port=%d",remoteAddr.sin_addr,remoteAddr.sin_port);
                printf("%s
",buffer);
                //cout << "received UDP data." << endl;
                char sendBuff[512];
                memset(sendBuff, 0, sizeof(sendBuff));
                sendto(localSocket, &sendBuff, sizeof(sendBuff),0, (struct sockaddr *)&remoteAddr ,remoteAddrLength);
        }
        return 0;
}

java

package udpclient;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
        String sendContent = "hello world";
        byte[] buffer = sendContent.getBytes();
       
        DatagramSocket ds = null;
        DatagramPacket dp = null;
        try {
            ds = new DatagramSocket();
            dp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.1.157"), 9548);
            ds.send(dp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(ds != null)
                ds.close();
        }
    }

}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>