sockets - Implementing Stop and wait with C & UDP, and what does cause 'resource temporarily unavailable'? -
i implementing stop&wait c , udp socket programming. simulate protocol, wrote 2 codes.
this server.c
file:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <unistd.h> #include <arpa/inet.h> typedef struct packet{ char data[1024]; }packet; typedef struct frame{ int frame_kind; //ack:0, seq:1 fin:2 int sq_no; int ack; packet packet; }frame; int main(int argc, char** argv) { int sockfd; int clilen; int state; int n; int sum; int recv_result; int frame_id = 0; packet packet; frame frame; frame recv_frame; char buffer[1024] = ""; struct timeval tv; struct sockaddr_in serveraddr, clientaddr; file* infile = fopen(argv[2], "r"); if(argc != 3) { perror("error! usage: $./server <port> filename\n"); exit(1); } if(infile == null) { printf("error! failed open file.\n"); exit(0); } clilen = sizeof(clientaddr); sockfd = socket(af_inet, sock_dgram, 0); //using diagram instead of stream-- udp if(sockfd < 0) { perror("socket error: "); exit(0); } bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = af_inet; serveraddr.sin_addr.s_addr = htonl(inaddr_any); serveraddr.sin_port = htons(atoi(argv[1])); //set port number 9999 //set socket option -- timeout 100000 microseconds tv.tv_sec = 1; tv.tv_usec = 0; setsockopt(sockfd, sol_socket, so_rcvtimeo, (char*)&tv, sizeof(struct timeval)); state = bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)); if(state == -1) { perror("bind error: "); exit(0); } do{ n = fscanf(infile, "%s", buffer); strcpy(packet.data, buffer); memcpy(&(frame.packet), &packet, sizeof(packet)); frame.frame_kind = 1; //seq frame.sq_no = frame_id; frame.ack = 0; printf("flag2"); while(1) { sendto(sockfd, &frame, sizeof(frame), 0, (struct sockaddr*)&clientaddr, (socklen_t)clilen); printf("frame %d sent\n", frame_id); recv_result = recvfrom(sockfd, &recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&clientaddr, &clilen ); if(recv_result > 0 && recv_frame.sq_no == 0 && recv_frame.ack == frame_id){ printf("ack %d received\n", recv_frame.sq_no); break; }else{ printf("frame %d time expired\n", frame_id); } } frame_id++; }while(n > 0); printf("finished\n"); return 0; }
this client.c
file:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> typedef struct packet{ char data[1024]; }packet; typedef struct frame{ int frame_kind; //ack:0, seq:1 fin:2 int sq_no; int ack; packet packet; }frame; int main(int argc, char *argv[]) { int sock; int str_len; socklen_t adr_sz; file* outfile = fopen("output.txt", "w"); struct sockaddr_in serv_adr, from_adr; frame ackframe, recv_frame; int frame_id; int recv_result; struct timeval tv; if(argc!=3){ printf("usage : %s <ip> <port>\n", argv[0]); exit(1); } sock=socket(pf_inet, sock_dgram, 0); if(sock==-1) { printf("error! failed open socket\n"); exit(1); } memset(&serv_adr, 0, sizeof(serv_adr)); adr_sz = sizeof(serv_adr); serv_adr.sin_family=af_inet; serv_adr.sin_addr.s_addr=inet_addr(argv[1]); serv_adr.sin_port=htons(atoi(argv[2])); tv.tv_sec = 1; tv.tv_usec = 0; setsockopt(sock, sol_socket, so_rcvtimeo, (char*)&tv, sizeof(struct timeval)); frame_id = 0; while(1) { recv_result = recvfrom(sock, &recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&serv_adr, &adr_sz); perror("error: "); printf("data %s\n", recv_frame.packet.data); printf("recv result %d\n", recv_result); printf("recv frame=%d, frame_kind = %d\n", recv_frame.sq_no, recv_frame.frame_kind); if(recv_result > 0 && recv_frame.frame_kind == 1 && recv_frame.sq_no==frame_id){ printf("frame %d received\n", recv_frame.sq_no); fprintf(outfile, "%s", recv_frame.packet.data); ackframe.frame_kind = 0; ackframe.ack = recv_frame.sq_no + 1; printf("ack %d sent\n", ackframe.ack); sendto(sock, &recv_frame, sizeof(recv_frame), 0,(struct sockaddr*)&serv_adr, adr_sz); break; }else{ printf("frame %d time expired\n", frame_id); } } close(sock); fclose(outfile); return 0; }
the server needs 3 arguments: ./server <port> <test.txt>
the client needs 3 arguments: ./client <ip> <port>
it executed, recvfrom
of client returns -1
.
i read questions same topic, not find out error code. there way fix code recvfrom
& sendto
? or need use select
function? (i communicate 1 server & 1 client.)
the "client" , "server" program contains dubious code. first lets take "server", calls sendto
clientaddr
haven't initialized. knows try send packets. this, in fact, leads undefined behavior, contents of structure indeterminate.
then lets take client, in call recvfrom
on socket isn't bound locally. address structure pointer pass recvfrom
filled in recvfrom
function, data in not used in other way.
there other problems, these 2 stand out.
Comments
Post a Comment