C语言通过socket发送接收二维字符串数组,二维字符串数组长度不确定,并将二维字符串数组输出,给出具体代码。
时间: 2024-01-11 18:02:09 浏览: 89
lax-wendroff-shock-compute,c语言下载网页源码,c语言程序
以下是一个示例代码,可以通过socket发送接收二维字符串数组,并将其输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
char **str_array = NULL; // 二维字符串数组
int rows = 0; // 二维数组的行数
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置socket选项
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定socket
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听socket
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// 接收客户端连接
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// 接收二维字符串数组的行数
if ((valread = read(new_socket, buffer, 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
sscanf(buffer, "%d", &rows); // 将字符串转换为整数
// 动态分配二维字符串数组
str_array = (char **)malloc(rows * sizeof(char *));
for (int i = 0; i < rows; i++) {
str_array[i] = (char *)malloc(1024 * sizeof(char));
}
// 接收二维字符串数组的内容
for (int i = 0; i < rows; i++) {
if ((valread = read(new_socket, str_array[i], 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
}
// 输出二维字符串数组
printf("Received string array:\n");
for (int i = 0; i < rows; i++) {
printf("%s\n", str_array[i]);
}
// 发送响应
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
// 释放内存
for (int i = 0; i < rows; i++) {
free(str_array[i]);
}
free(str_array);
return 0;
}
```
客户端代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char **str_array = NULL; // 二维字符串数组
int rows = 3; // 二维数组的行数
// 动态分配二维字符串数组
str_array = (char **)malloc(rows * sizeof(char *));
for (int i = 0; i < rows; i++) {
str_array[i] = (char *)malloc(1024 * sizeof(char));
sprintf(str_array[i], "String%d", i+1); // 生成测试数据
}
// 创建socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// 将IP地址转换为网络字节序
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
// 连接服务器
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
// 发送二维字符串数组的行数
char rows_str[10];
sprintf(rows_str, "%d", rows);
send(sock, rows_str, strlen(rows_str), 0);
// 发送二维字符串数组的内容
for (int i = 0; i < rows; i++) {
send(sock, str_array[i], strlen(str_array[i]), 0);
}
// 接收响应
char buffer[1024] = {0};
if ((valread = read(sock, buffer, 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
printf("%s\n", buffer);
// 释放内存
for (int i = 0; i < rows; i++) {
free(str_array[i]);
}
free(str_array);
return 0;
}
```
阅读全文