Implementation of Linux UDP server and client program

Time:2020-11-28

1. Source code

UDP server:

/**
 *@ brief UDP server
 * @author Mculover666
 * @date  2020/04/15
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{

  int server_sock_fd;
  struct sockaddr_in server_addr, client_addr;
  char recv_buf[100];
  int nbytes = 0;
  socklen_t len = 0;

  /*Create server socket*/
  server_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (server_sock_fd < 0)
  {
    Printf ("server socket creation failed");
    return -1;
  }
  Printf ("server socket created successfully");

  /*Binding IP and port*/
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_ addr.sin_ Port = htons (ATOI (argv [1])); // specifies the port number
  bind(server_sock_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));

  Printf ("server socket binding succeeded ');
  
  while(1)
  {
    /*Receive data from UDP client*/
    Printf ("waiting to receive client data: n");
    len = sizeof(client_addr);
    nbytes = recvfrom(server_sock_fd, recv_buf, 100, 0, (struct sockaddr *)&client_addr, &len);
    printf("ok\n");
    recv_buf[nbytes] = '
/**
*@ brief UDP server
* @author Mculover666
* @date  2020/04/15
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int server_sock_fd;
struct sockaddr_in server_addr, client_addr;
char recv_buf[100];
int nbytes = 0;
socklen_t len = 0;
/*Create server socket*/
server_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (server_sock_fd < 0)
{
Printf ("server socket creation failed");
return -1;
}
Printf ("server socket created successfully");
/*Binding IP and port*/
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_ addr.sin_ Port = htons (ATOI (argv [1])); // specifies the port number
bind(server_sock_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
Printf ("server socket binding succeeded ');
while(1)
{
/*Receive data from UDP client*/
Printf ("waiting to receive client data: n");
len = sizeof(client_addr);
nbytes = recvfrom(server_sock_fd, recv_buf, 100, 0, (struct sockaddr *)&client_addr, &len);
printf("ok\n");
recv_buf[nbytes] = '\0';
printf("recv %d bytes:%s.\n", nbytes, recv_buf);
//Receive user input and send it to client
Printf ("please input the data to be sent to the client)";
fgets(recv_buf, 100, stdin);
sendto(server_sock_fd,recv_buf,strlen(recv_buf),0,(struct sockaddr *)&client_addr,len);
}
return 0;
}
'; printf("recv %d bytes:%s.\n", nbytes, recv_buf); //Receive user input and send it to client Printf ("please input the data to be sent to the client)"; fgets(recv_buf, 100, stdin); sendto(server_sock_fd,recv_buf,strlen(recv_buf),0,(struct sockaddr *)&client_addr,len); } return 0; }

UDP client:

/**
 *@ brief UDP client program
 * @author Mculover666
 * @date  2020/04/15
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
  int sock_fd;
  struct sockaddr_in server_addr;
  char recv_buf[100];
  int nbytes = 0;
  socklen_t len = 0;

  /*Create socket*/
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock_fd < 0)
  {
    Printf ("client socket creation failed");
    return -1;
  }

  /*Binding IP and port*/
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = inet_addr(argv[1]);
  server_ addr.sin_ Port = htons (ATOI (argv [2])); // specifies the port number
  while(1)
  {
	  len = sizeof(server_addr);
    //Receive user input and send it to the server
    Printf ("please input the data to be sent to the server):";
	  fgets(recv_buf, 100, stdin);
    sendto(sock_fd,recv_buf,100,0,(struct sockaddr *)(&server_addr),len);
    
    /*Receive data from UDP client*/
    len = sizeof(server_addr);
    nbytes = recvfrom(sock_fd, recv_buf, 100, 0, (struct sockaddr *)&server_addr, &len);
    recv_buf[nbytes] = '
/**
*@ brief UDP client program
* @author Mculover666
* @date  2020/04/15
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int sock_fd;
struct sockaddr_in server_addr;
char recv_buf[100];
int nbytes = 0;
socklen_t len = 0;
/*Create socket*/
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
{
Printf ("client socket creation failed");
return -1;
}
/*Binding IP and port*/
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_ addr.sin_ Port = htons (ATOI (argv [2])); // specifies the port number
while(1)
{
len = sizeof(server_addr);
//Receive user input and send it to the server
Printf ("please input the data to be sent to the server):";
fgets(recv_buf, 100, stdin);
sendto(sock_fd,recv_buf,100,0,(struct sockaddr *)(&server_addr),len);
/*Receive data from UDP client*/
len = sizeof(server_addr);
nbytes = recvfrom(sock_fd, recv_buf, 100, 0, (struct sockaddr *)&server_addr, &len);
recv_buf[nbytes] = '\0';
printf("recv %d bytes:%s.\n", nbytes, recv_buf);
}
return 0;
}
'; printf("recv %d bytes:%s.\n", nbytes, recv_buf); } return 0; }

2. Running results in the machine

compile:


gcc server.c -o srever_wangshiwei.o
gcc client.c -o client_wangshiwei.o

function:

3. Run the result between the development board and PC

compile:


gcc server.c -o srever_wangshiwei.o
arm-linux-gnueabi-gcc client.c -o clientARM_wangshiwei.o

This article on the Linux UDP server and client implementation of the article introduced here, more related Linux UDP server and client content, please search the previous article or continue to browse the following related articles, I hope you can support developeppaer more in the future!