我在程序中使用# Create first snapshot
btrfs subvolume snapshot -r <original_subvol> <snapshot_1>
# Write some stuff to <original_subvol>
btrfs subvolume snapshot -r <original_subvol> <snapshot_2>
# Create the backup files
btrfs send -f <snapshot_file_1> <snapshot_1>
btrfs send -f <snapshot_file_2> -p <snapshot_1> <snapshot_2>
# Suppose you lost <original_subvol> and you restore to a brand new filesystem
btrfs receive -f <snapshot_file_1> <dest>
btrfs receive -f <snapshot_file_2> <dest>
# Create a write snapshot from the last restored snapshop
btrfs snapshot <restore_snapshot_2> <restore_subvol>
# Write some stuff to <restore_subvol> and do an incremental backup
btrfs snapshot -r <snapshot_3> <restore_subvol>
btrfs send -f <snapshot_file_3> -p <restore_snapshot_2> <restore_subvol>
# Suppose you lost <restore_subvol> (again !) and you restore to a brand new filesystem
btrfs receive -f <snapshot_file_1> <dest>
btrfs receive -f <snapshot_file_2> <dest>
btrfs receive -f <snapshot_file_3> <dest>
=> ERROR: could not find parent subvolume
,但我不希望IP地址是硬编码的。
所以我尝试inet_addr()
,但我的程序无法获得IP地址。任何人都可以帮助我吗?
服务器
inet_addr(argv[1])
客户端
int main(int argc,char *argv[])
{
char str[100]; // declare necessary variables
int listen_fd, comm_fd;
struct sockaddr_in servaddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
char welcome[100];
char i[100];
char incorrectnum[100];
char line[512],line1[500],line2[500],line3[500],line4[500],line5[500];
FILE *fp;
char ch;
char str2[100];
char *file_path = "FILE.txt";
listen_fd = socket(AF_INET, SOCK_STREAM, 0); //create a socket
if (listen_fd == -1)
{
printf("Could not create socket");
}
puts("Socket created");
strcpy(str2,argv[1]);
printf("%s\n",str2 );
servaddr.sin_family = AF_INET; // prepare the sockaddr_in structure.
servaddr.sin_port = htons(1999);
servaddr.sin_addr.s_addr = inet_addr(str2);
memset(servaddr.sin_zero, '\0', sizeof servaddr.sin_zero);
if(bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0){ //bind a name to a socket
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
listen(listen_fd, BACKLOG); //listening for incomming connection
puts("Waiting for incoming connections...");
addr_size = sizeof serverStorage;
comm_fd = accept(listen_fd, (struct sockaddr *)&serverStorage, &addr_size);
if (comm_fd < 0){
perror("accept failed");
return 1;
}
puts("Connection accepted");
printf("***___WELCOME TO MY SERVER___***\n");
bzero(welcome, 100);
strcpy(welcome,"***___WELCOME TO MY SERVER___***");
send(comm_fd, welcome,100, 0);
.....
}
答案 0 :(得分:2)
inet_addr
,以支持(以及其他方法)getaddrinfo
。此方法与IPv6兼容,通常更易于使用。
linux.die.net页面提供了一个很好的客户端/服务器通信代码示例,它正是您正在寻找的内容:http://linux.die.net/man/3/getaddrinfo(下面重现的重要部分)
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int main(int argc, char *argv[]) { // port
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(NULL, argv[1], &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind(2).
If socket(2) (or bind(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break; /* Success */
close(sfd);
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not bind\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
// ...snip - not relevant to this question
}
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) { // host port
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s, j;
if (argc < 3) {
fprintf(stderr, "Usage: %s host port msg...\n", argv[0]);
exit(EXIT_FAILURE);
}
/* Obtain address(es) matching host/port */
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
s = getaddrinfo(argv[1], argv[2], &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sfd);
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not connect\n");
exit(EXIT_FAILURE);
}
freeaddrinfo(result); /* No longer needed */
// ...snip - not relevant to this question
exit(EXIT_SUCCESS);
}