#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
enum type_pack {
a_null = 0,
a_get_file = 1,
a_put_file = 2,
a_file_data = 3,
a_get_date = 4,
a_end_file = 5
};
struct t_get_file {
enum type_pack code;
};
#define t_get_file_size sizeof(enum type_pack)
struct t_put_file {
enum type_pack code;
unsigned long file_size;
//unsigned short date_size;
unsigned long hash;
};
#define t_put_file_size sizeof(enum type_pack)+2*sizeof(unsigned long)/*+sizeof(unsigned short)*/
struct t_file_date {
enum type_pack code;
unsigned short index_data;
unsigned long date[512];
unsigned long hash;
};
#define t_file_date_size sizeof(enum type_pack)+sizeof(unsigned short)+(512+1)*sizeof(unsigned long)
/*struct t_file_date_e {
unsigned long hash;
};
#define t_file_date_e_size sizeof(unsigned long)*/
struct t_get_date {
enum type_pack code;
unsigned short index_data;
unsigned long hash;
};
#define t_get_date_size sizeof(enum type_pack)+sizeof(unsigned short)+sizeof(unsigned long)
/*char msg1[] = "Hello there!\n";
char msg2[] = "Bye bye!\n";*/
int main()
{
int sock;
int bytes_read;
long buf[1024];
int fd = creat("select.fl", 0644); //Дескриптор файла
printf("creat\n");
struct sockaddr_in addr;
struct t_get_file i_get_file;
struct t_put_file i_put_file;
struct t_file_date i_file_date;
//struct t_file_date_e i_file_date_e;
struct stat i_file_stat;
printf("1\n");
enum type_pack program_status = a_null;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
printf("socket\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
/* sendto(sock, msg1, sizeof(msg1), 0,
(struct sockaddr *)&addr, sizeof(addr));
connect(sock, (struct sockaddr *)&addr, sizeof(addr));
/* send(sock, msg2, sizeof(msg2), 0);*/
printf("connect\n");
i_get_file.code = a_get_file;
send(sock, &i_get_file, sizeof(i_get_file)/*t_get_file_size*/, 0);
program_status = a_get_file;
printf("send\n");
do {
switch (program_status) {
case a_get_file:
{
printf("a_get_file\n");
program_status = a_put_file;
fd = open("select.fl", O_TRUNC, 0);
};
case a_put_file:
{
printf("a_put_file\n");
bytes_read = recv(sock, &i_put_file, t_put_file_size, 0);
printf("a_put_file 1\n");
if (bytes_read == t_put_file_size && i_put_file.code == a_put_file)
printf("a_put_file 2\n");
{program_status = a_file_data;};
};
case a_file_data:
{
printf("a_file_data\n");
bytes_read = recv(sock,
&i_file_date,
t_file_date_size/*
+ t_file_date_e_size
+ i_put_file.date_size*/,
0);
if (bytes_read == (t_file_date_size/*
+ t_file_date_e_size
+ i_put_file.date_size*/) && i_file_date.code == a_file_data)
{
lseek(fd, i_file_date.index_data * 512, SEEK_SET);
fstat(fd, &i_file_stat);
write(fd, i_file_date.date, (i_put_file.file_size - i_file_stat.st_size) >= 512
? 512
: (i_put_file.file_size % 512));
};
};
case a_get_date:
{
printf("a_get_date\n");};
default:
{
printf("6\n");};
};
} while (program_status == a_end_file);
close(sock);
close(fd);
return 0;
}
не срабатывает recvfrom
Существует две программы
client
Код:
Код:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
enum type_pack {
a_null = 0,
a_get_file = 1,
a_put_file = 2,
a_file_data = 3,
a_get_date = 4,
a_end_file = 5
};
struct t_get_file {
enum type_pack code;
};
#define t_get_file_size sizeof(enum type_pack)
struct t_put_file {
enum type_pack code;
unsigned long file_size;
//unsigned short date_size;
unsigned long hash;
};
#define t_put_file_size sizeof(enum type_pack)+2*sizeof(unsigned long)/*+sizeof(unsigned short)*/
struct t_file_date {
enum type_pack code;
unsigned short index_data;
unsigned long date[512];
unsigned long hash;
};
#define t_file_date_size sizeof(enum type_pack)+sizeof(unsigned short)+(512+1)*sizeof(unsigned long)
/*struct t_file_date_e {
unsigned long hash;
};
#define t_file_date_e_size sizeof(unsigned long)*/
struct t_get_date {
enum type_pack code;
unsigned short index_data;
unsigned long hash;
};
#define t_get_date_size sizeof(enum type_pack)+sizeof(unsigned short)+sizeof(unsigned long)
int main()
{
int sock;
struct sockaddr_in addr;
struct sockaddr addr_to;
int addr_to_len;
char buf[1024];
int bytes_read, index_in_file=0;
printf("0\n");
int fd/* = creat("from.fl", 0644)*/; //Дескриптор файла
struct t_get_file i_get_file;
struct t_put_file i_put_file;
struct t_file_date i_file_date;
//struct t_file_date_e i_file_date_e;
struct stat i_file_stat;
printf("1\n");
enum type_pack program_status = a_null;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
printf("socket\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
printf("bind\n");
while(1)
{
bytes_read = recvfrom(sock, &program_status, sizeof(program_status/*enum type_pack*/), /*MSG_PEEK*/0, /*&addr_to*/NULL, /*&addr_to_len*/NULL);
printf("recvfrom\n");
switch (program_status) {
case a_get_file:
{
printf("a_get_file\n");
program_status = a_put_file;
fd = open("from.fl", O_RDONLY, 0);
fstat(fd, &i_file_stat);
i_put_file.code = a_put_file;
i_put_file.file_size = i_file_stat.st_size;
i_put_file.hash = 0;
sendto(sock, &i_put_file, t_put_file_size, 0, &addr_to, addr_to_len);
};
case a_put_file:
{
printf("a_put_file\n");
program_status = a_file_data;
};
case a_file_data:
{
printf("a_file_data\n");
i_file_date.code = a_file_data;
i_file_date.index_data = index_in_file /512;
index_in_file += read(fd, &i_file_date.date, 512);
i_file_date.hash = 0;
sendto(sock, &i_file_date, t_file_date_size, 0, &addr_to, addr_to_len);
};
case a_get_date:
{
printf("a_get_date\n");
};
default:
{};
};
}
return 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
enum type_pack {
a_null = 0,
a_get_file = 1,
a_put_file = 2,
a_file_data = 3,
a_get_date = 4,
a_end_file = 5
};
struct t_get_file {
enum type_pack code;
};
#define t_get_file_size sizeof(enum type_pack)
struct t_put_file {
enum type_pack code;
unsigned long file_size;
//unsigned short date_size;
unsigned long hash;
};
#define t_put_file_size sizeof(enum type_pack)+2*sizeof(unsigned long)/*+sizeof(unsigned short)*/
struct t_file_date {
enum type_pack code;
unsigned short index_data;
unsigned long date[512];
unsigned long hash;
};
#define t_file_date_size sizeof(enum type_pack)+sizeof(unsigned short)+(512+1)*sizeof(unsigned long)
/*struct t_file_date_e {
unsigned long hash;
};
#define t_file_date_e_size sizeof(unsigned long)*/
struct t_get_date {
enum type_pack code;
unsigned short index_data;
unsigned long hash;
};
#define t_get_date_size sizeof(enum type_pack)+sizeof(unsigned short)+sizeof(unsigned long)
int main()
{
int sock;
struct sockaddr_in addr;
struct sockaddr addr_to;
int addr_to_len;
char buf[1024];
int bytes_read, index_in_file=0;
printf("0\n");
int fd/* = creat("from.fl", 0644)*/; //Дескриптор файла
struct t_get_file i_get_file;
struct t_put_file i_put_file;
struct t_file_date i_file_date;
//struct t_file_date_e i_file_date_e;
struct stat i_file_stat;
printf("1\n");
enum type_pack program_status = a_null;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
printf("socket\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
printf("bind\n");
while(1)
{
bytes_read = recvfrom(sock, &program_status, sizeof(program_status/*enum type_pack*/), /*MSG_PEEK*/0, /*&addr_to*/NULL, /*&addr_to_len*/NULL);
printf("recvfrom\n");
switch (program_status) {
case a_get_file:
{
printf("a_get_file\n");
program_status = a_put_file;
fd = open("from.fl", O_RDONLY, 0);
fstat(fd, &i_file_stat);
i_put_file.code = a_put_file;
i_put_file.file_size = i_file_stat.st_size;
i_put_file.hash = 0;
sendto(sock, &i_put_file, t_put_file_size, 0, &addr_to, addr_to_len);
};
case a_put_file:
{
printf("a_put_file\n");
program_status = a_file_data;
};
case a_file_data:
{
printf("a_file_data\n");
i_file_date.code = a_file_data;
i_file_date.index_data = index_in_file /512;
index_in_file += read(fd, &i_file_date.date, 512);
i_file_date.hash = 0;
sendto(sock, &i_file_date, t_file_date_size, 0, &addr_to, addr_to_len);
};
case a_get_date:
{
printf("a_get_date\n");
};
default:
{};
};
}
return 0;
}
client
Цитата:
creat
1
socket
connect
send
a_get_file
a_put_file
1
socket
connect
send
a_get_file
a_put_file
server
Цитата:
0
1
socket
bind
1
socket
bind
при этом
sender
Код:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
char msg1[] = "Hello there!\n";
char msg2[] = "Bye bye!\n";
int main()
{
int sock;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sendto(sock, msg1, sizeof(msg1), 0,
(struct sockaddr *)&addr, sizeof(addr));
connect(sock, (struct sockaddr *)&addr, sizeof(addr));
send(sock, msg2, sizeof(msg2), 0);
close(sock);
return 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
char msg1[] = "Hello there!\n";
char msg2[] = "Bye bye!\n";
int main()
{
int sock;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sendto(sock, msg1, sizeof(msg1), 0,
(struct sockaddr *)&addr, sizeof(addr));
connect(sock, (struct sockaddr *)&addr, sizeof(addr));
send(sock, msg2, sizeof(msg2), 0);
close(sock);
return 0;
}
Код:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main()
{
int sock;
struct sockaddr_in addr;
char buf[1024];
int bytes_read;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
while(1)
{
bytes_read = recvfrom(sock, buf, 1024, 0, NULL, NULL);
buf[bytes_read] = '\0';
printf(buf);
}
return 0;
}
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main()
{
int sock;
struct sockaddr_in addr;
char buf[1024];
int bytes_read;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(2);
}
while(1)
{
bytes_read = recvfrom(sock, buf, 1024, 0, NULL, NULL);
buf[bytes_read] = '\0';
printf(buf);
}
return 0;
}
Подскажите, в чём может быть сложность?
компилируется в CygWin
Заранее спасибо !
ошибся разделом.