我發現了一個很好的抽象,我可以使用FILE從UDP讀取數據。這對於讀取數據非常有用,但是我無法通過UDP來傳遞數據。下面是在文件流的UDP數據讀取的代碼爲:使用FILE流在c中讀取/寫入套接字
u_int8 *buf;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serverAddr;
if (sockfd < 0) {
printf("Could not open socket\n");
exit(1);
}
buf = malloc(BUF_SIZE * sizeof(u_int8));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(port);
int rc = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (rc<0) {
printf("Could not bind to port %d\n", port);
exit(1);
}
/* Make sockfd blocking */
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags & ~O_NONBLOCK);
FILE *sockfile;
sockfile = (FILE*)fdopen(sockfd, "r");
現在一些其他的代碼可以調用:
fread(data, sizeof(u_int8), frame_payload_size, sockfile);
這工作得很好。但是,反過來似乎並不:
u_int8 *buf;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in clientAddr;
if (sockfd < 0) {
printf("Could not open socket\n");
exit(1);
}
memset((char *)&clientAddr, 0, sizeof(clientAddr));
buf = malloc(BUF_SIZE * sizeof(u_int8));
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(port);
if (inet_aton("127.0.0.1", &clientAddr.sin_addr)==0) {
printf("inet_aton()\n");
abort();
}
int rc = bind(sockfd, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
FILE *sockfile;
sockfile = (FILE*)fdopen(sockfd, "w");
一些其他的代碼調用:
written = fwrite(fm->frame, sizeof(u_int8), fm->frame_length, data_out_stream);
「寫」將返回寫入元素的正數,但沒有UDP數據包似乎產生。
正在嘗試做什麼?任何建議,爲什麼它可能不工作?