下面是一个简单的使用公共无线接口协议实现的客户端和服务器端的示例,使用C语言编写。
服务器端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 1024
// 创建并绑定socket
int create_and_bind_socket(int port) {
int server_socket, opt = 1;
struct sockaddr_in server_address;
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
return server_socket;
}
int main(int argc, char const *argv[]) {
int server_socket, client_socket, read_size;
struct sockaddr_in client_address;
char buffer[MAX_BUFFER_SIZE] = {0};
CPRI_Frame* incoming_frame;
if (argc < 2) {
printf("Usage: ./server port\n");
return 0;
}
int port = atoi(argv[1]);
server_socket = create_and_bind_socket(port);
if (listen(server_socket, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d\n", port);
while (1) {
int address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr *)&client_address, (socklen_t*)&address_size);
if (client_socket < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New connection from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
while ((read_size = read(client_socket, buffer, MAX_BUFFER_SIZE)) > 0) {
incoming_frame = (CPRI_Frame*)buffer;
printf("Received frame of length %d, type %d, ID %d\n", incoming_frame->length, incoming_frame->type, incoming_frame->id);
printf("Data: ");
for (int i = 0; i < incoming_frame->length - CPRI_HEADER_SIZE; i++) {
printf("%02X ", incoming_frame->data[i]);
}
printf("\n");
// 处理接收到的数据
// ...
memset(buffer, 0, MAX_BUFFER_SIZE);
}
if (read_size == 0) {
printf("Client disconnected\n");
} else {
perror("recv failed");
}
close(client_socket);
}
return 0;
}
```
客户端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_ADDRESS "127.0.0.1"
#define MAX_BUFFER_SIZE 1024
// 连接服务器
int connect_to_server(char* server_address, int port) {
int client_socket;
struct sockaddr_in server_address_struct;
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
server_address_struct.sin_family = AF_INET;
server_address_struct.sin_port = htons(port);
// 将点分十进制的IP地址转换为二进制格式
if (inet_pton(AF_INET, server_address, &server_address_struct.sin_addr) <= 0) {
perror("address invalid");
exit(EXIT_FAILURE);
}
if (connect(client_socket, (struct sockaddr *)&server_address_struct, sizeof(server_address_struct)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
return client_socket;
}
int main(int argc, char const *argv[]) {
int client_socket, read_size;
char buffer[MAX_BUFFER_SIZE] = {0};
CPRI_Frame* outgoing_frame;
if (argc < 3) {
printf("Usage: ./client server_address port\n");
return 0;
}
char* server_address = (char*)argv[1];
int port = atoi(argv[2]);
client_socket = connect_to_server(server_address, port);
printf("Connected to server %s:%d\n", server_address, port);
// 构建CPRI数据帧
unsigned char data[] = {0x01, 0x02, 0x03};
unsigned short data_length = sizeof(data) / sizeof(unsigned char);
outgoing_frame = create_cpri_frame(CPRI_HEADER_SIZE + data_length, CPRI_TYPE_DATA, CPRI_ID_ONE);
memcpy(outgoing_frame->data, data, data_length);
// 发送数据
send(client_socket, (void*)outgoing_frame, outgoing_frame->length, 0);
printf("Sent frame of length %d, type %d, ID %d\n", outgoing_frame->length, outgoing_frame->type, outgoing_frame->id);
printf("Data: ");
for (int i = 0; i < data_length; i++) {
printf("%02X ", outgoing_frame->data[i]);
}
printf("\n");
// 销毁帧
destroy_cpri_frame(outgoing_frame);
// 接收服务器返回的数据
while ((read_size = read(client_socket, buffer, MAX_BUFFER_SIZE)) > 0) {
printf("Server response: %s\n", buffer);
memset(buffer, 0, MAX_BUFFER_SIZE);
}
if (read_size == 0) {
printf("Server disconnected\n");
} else {
perror("recv failed");
}
close(client_socket);
return 0;
}
```
上述示例中,我们分别实现了一个简单的服务器端和客户端,使用公共无线接口协议(CPRI)进行数据传输。服务器端通过`create_and_bind_socket`函数创建并绑定socket,然后使用`accept`函数接收客户端连接,并使用`read`函数接收客户端发送的数据。客户端通过`connect_to_server`函数连接服务器,并使用`send`函数发送数据。在发送数据和接收数据的过程中,我们分别使用了`CPRI_Frame`结构体来构建和解析CPRI数据帧。