Menu Close

Achieving Ethernet Communication Via PS

This article talks about how to use the open read and close commands to implement Linux PS Uart, as well as adding axe uart IP to implement PL uart.

For related subjects, please refer to the SOC Table of Contents.

 

We will be using sockets for internet connection, which means we will need to write two application programs – one for the server side and one for the client side.

Server side code download: server_socket_package_test

The code for the server side is as follows:

// Server side implementation of UDP client-server model 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define PORT 8080 
#define MAXLINE 1024 

// Driver code 
int main(int argc,char** argv) {
    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from server";
    struct sockaddr_in servaddr, cliaddr;

    // Creating socket file descriptor 
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&servaddr, 0, sizeof(servaddr));
    memset(&cliaddr, 0, sizeof(cliaddr));

    // Filling server information 
    servaddr.sin_family = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(PORT);

    //int sport;
    if(argc>1)
    {
        int sport;
        sscanf(argv[1],"%d",&sport);
        printf("port is %d\n",sport);
        servaddr.sin_port=htons(sport);
    }

    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr,
        sizeof(servaddr)) < 0 )
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    int len, n,cport;
    char addr_buffer[20];
    len=sizeof(struct sockaddr);
    printf("before recvfrom len =%d\n",len);
    n = recvfrom(sockfd, (char *)buffer, MAXLINE,
                            MSG_WAITALL, ( struct sockaddr *) &cliaddr,
                            &len);
    buffer[n] = '\0';
    printf("Client : %s\n", buffer);
    inet_ntop(AF_INET,&(cliaddr.sin_addr.s_addr),addr_buffer,len);
    cport=ntohs(cliaddr.sin_port);
    printf("caddr=%x\n",cliaddr.sin_addr.s_addr);
    printf("addr=%s, len=%d\n",addr_buffer,len);
    printf("c family=%d\n",cliaddr.sin_family);
    printf("c port=%d and=%d\n",cliaddr.sin_port,cport);
    sendto(sockfd, (const char *)hello, strlen(hello),
                MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
                len);
    printf("Hello message sent.\n");
    //check the server info
    inet_ntop(AF_INET,&(servaddr.sin_addr),addr_buffer,len);
    cport=ntohs(servaddr.sin_port);
    printf("check server addr=%s, port=%d\n",addr_buffer,cport);

    return 0;
}

Client side code download: client_socket_package_test

The code for the client side is as follows:

// Client side implementation of UDP client-server model 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define PORT 8080 
#define MAXLINE 1024 

// Driver code 
int main(int argc,char **argv) {
    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from client";
    struct sockaddr_in servaddr;

    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&servaddr, 0, sizeof(servaddr));

    // Filling server information 
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = INADDR_ANY;

    if(argc>=2)
    {
        inet_pton(AF_INET,argv[1],&(servaddr.sin_addr));
    }
    if(argc>=3){
        int sport;
        sscanf(argv[2],"%d",&sport);
        servaddr.sin_port=htons(sport);
    }
    int n, len;

    sendto(sockfd, (const char *)hello, strlen(hello),
                           MSG_CONFIRM, (const struct sockaddr *) &servaddr,
                           sizeof(servaddr));
    printf("Hello message sent.\n");
    char serverbuffer[20];
    int sport;
    n = recvfrom(sockfd, (char *)buffer, MAXLINE,
                            MSG_WAITALL, (struct sockaddr *) &servaddr,
                            &len);
    buffer[n] = '\0';
    printf("Server : %s\n", buffer);
    inet_ntop(AF_INET,&(servaddr.sin_addr.s_addr),serverbuffer,len);
    sport=ntohs(servaddr.sin_port);
    printf("server=%s, port=%d len=%d\n",serverbuffer,sport,len);

    close(sockfd);
    return 0;
}

After finishing these two programs, you can use PetaLinux to write the .o file. Don’t forget to use spell to set the PetaLinux pathway first. The commands are as follows. Remember to change the file name of the .c file to the one you set, as well as naming the .o file what you want.

arm-linux-gnueabihf-gcc server_socket_test.c -o server.o
arm-linux-gnueabihf-gcc client_socket_test.c -o client.o

Next, we can send the .o file over to the development board. We can use methods such as the SD card, WinSCP, as well as RCP/SCP.

Keep in mind to run the server side application first, and then run the client side application once the server side application is running. We can use two windows in PuTTY.

If we use a package analyzer such as Wireshark, we can verify that the package is indeed being sent and received.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!