Skip to content

Commit 8b384c8

Browse files
committed
The second project of CSE530 Network Programming
1 parent afd7dc7 commit 8b384c8

20 files changed

+2233
-0
lines changed

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CC := gcc
2+
3+
LIBS := -lresolv -lnsl -lpthread -lm\
4+
../stevens_code/libunp.a\
5+
6+
FLAGS := -g -O2
7+
8+
CFLAGS := ${FLAGS} -I../stevens_code/lib
9+
10+
all: client server test get_ifi_info_plus.o rtt.o serv_send.o serv_recv.o cli_send.o cli_recv.o
11+
12+
test: test.o
13+
${CC} ${FLAGS} -o test test.o ${LIBS}
14+
server.o: server.c
15+
${CC} ${CFLAGS} -c server.c
16+
17+
server: server.o get_ifi_info_plus.o rtt.o serv_send.o serv_recv.o
18+
${CC} ${FLAGS} -o server server.o get_ifi_info_plus.o rtt.o serv_send.o serv_recv.o ${LIBS}
19+
server.o: server.c
20+
${CC} ${CFLAGS} -c server.c
21+
22+
23+
client: client.o get_ifi_info_plus.o rtt.o cli_send.o cli_recv.o
24+
${CC} ${FLAGS} -o client client.o get_ifi_info_plus.o rtt.o cli_send.o cli_recv.o ${LIBS}
25+
client.o: client.c
26+
${CC} ${CFLAGS} -c client.c
27+
28+
29+
get_ifi_info_plus.o: get_ifi_info_plus.c
30+
${CC} ${CFLAGS} -c get_ifi_info_plus.c
31+
32+
rtt.o: rtt.c
33+
${CC} ${CFLAGS} -c rtt.c
34+
35+
data_send.o: serv_send.c
36+
${CC} ${CFLAGS} -c serv_send.c
37+
38+
data_recv.o: serv_recv.c
39+
${CC} ${CFLAGS} -c serv_recv.c
40+
41+
cli_send.o: cli_send.c
42+
${CC} ${CFLAGS} -c cli_send.c
43+
44+
cli_recv.o: cli_recv.c
45+
${CC} ${CFLAGS} -c cli_recv.c
46+
47+
48+
clean:
49+
rm test test.o client client.o server server.o get_ifi_info_plus.o rtt.o serv_send.o serv_recv.o cli_send.o cli_recv.o
50+

README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2013-2015 by Jun Zeng. <[email protected]> <[email protected]>
2+
All rights reserved.
3+
4+
Based on Steven Codes' library. UNIX Network Programming, Volume 1, Third Edition.
5+
6+
Unrestricted rights are granted to instructors and students of CSE530 class in Fall 2014
7+
at Stony Brook University.

ReadMe

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
YIGONG WANG SBU-ID:109973706
2+
JUN ZENG SBU-ID:109811026
3+
4+
5+
1. The modifications to ensure that only unicast addresses are bound:
6+
Use a loop to build up the socket and bound to the IP address the function get_ifi_info_plus(professor given) return except the first loopback address.
7+
8+
Our implementation of the array of structures described above:
9+
Store the socket file description, the IP address and network mask returned from the given function into the servIF and cliIF structure. Then the subnet address is got by the binary and operation of IP address and the relative network mask.
10+
11+
2. The details of our modifications to the code of Section 22.5:
12+
1> The parameters in struct rtt_info have set to int except rtt_base;
13+
2> All the multiply and divide operation have changed into binary operation;
14+
3> The return value of rtt_start is divided by 1000;
15+
4> RTT_RXTMIN is set to 1000 msec, RTT_RXTMAX is set to 3000 msec and RTT_MAXNREXMT is set to 12;
16+
5> All the second parameters have been changed into millisecond;
17+
6> rtt_timeout passes its value through the function rtt_minmax, after doubling the RTO.
18+
19+
20+
3. The TCP mechanisms we implemented:
21+
When the transmittion starts, the conjection window size(cwin) is set to 1, and ssthresh is set to be equal to the cwin. Then every time a successful transmittion, cwin doubles. When the sender loses its datagram and receives the duplicate ack from the receiver, the cwin and ssthresh both divide half. If there is no ack received, the cwin is set to 1 and the ssthresh divides half. If then it is successful to transmit the datagrams, cwin is set to be equal to ssthresh. When the cwin bigger than ssthresh, it changes from double increasing into linear increasing. After the sender time out 12 times, it judges that the receiver is shut down.
22+
23+
24+
4. Sender notifying receiver of the last datagram:
25+
Add one more parameter-fin into the hdr structure, and set it as 0 when the file stream has not reached the end. When the file stream is read to the end, fin is set to 1 and pass to the client with the hdr structure to notify receiver that this is the last datagram.
26+
27+
Clean closing:
28+
After send to the server with the last ack, the client uses a recvmsg and the rtt mechanism to keep listen for the retransmitted datagram for a certain time. Here we set the time as 15 seconds. Then after 15 seconds, the recvmsg times out and the client closes.

aa.c

3.5 KB
Binary file not shown.

cli_recv.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* include dgsendrecv1 */
2+
#include "unprtt.h"
3+
#include <setjmp.h>
4+
5+
#define CLI_TIMEOUT 8000
6+
#define RTT_DEBUG
7+
8+
static int rttinit = 0;
9+
static struct msghdr msgrecv; /* assumed init to 0 */
10+
static struct hdr {
11+
uint32_t seq; /* sequence # */
12+
uint32_t ts; /* timestamp when sent */
13+
uint32_t fin; /* mark when file eof */
14+
} recvhdr;
15+
16+
static void sig_alrm(int signo);
17+
static sigjmp_buf jmpbuf;
18+
19+
struct hdr cli_recv(int fd, void *inbuff, size_t inbytes)
20+
{
21+
ssize_t n;
22+
struct iovec iovrecv[2];
23+
struct rtt_info rttinfo;
24+
25+
26+
msgrecv.msg_name = NULL;
27+
msgrecv.msg_namelen = 0;
28+
msgrecv.msg_iov = iovrecv;
29+
msgrecv.msg_iovlen = 2;
30+
iovrecv[0].iov_base = &recvhdr;
31+
iovrecv[0].iov_len = sizeof(struct hdr);
32+
iovrecv[1].iov_base = inbuff;
33+
iovrecv[1].iov_len = inbytes;
34+
35+
rttinfo.rtt_rto = CLI_TIMEOUT; //8 sec time out
36+
37+
if (rttinit == 0) {
38+
rtt_init(&rttinfo); /* first time we're called */
39+
rttinit = 1;
40+
rtt_d_flag = 1;
41+
}
42+
43+
Signal(SIGALRM, sig_alrm);
44+
rtt_newpack(&rttinfo); /* initialize for this packet */
45+
46+
alarm(rtt_start(&rttinfo)/1000); /* calc timeout value & start timer */
47+
48+
//#ifdef RTT_DEBUG
49+
rtt_debug(&rttinfo);
50+
//#endif
51+
52+
if (sigsetjmp(jmpbuf, 1) != 0) {
53+
err_msg("cli_recv: no response from server, giving up");
54+
rttinit = 0; /* reinit in case we're called again */
55+
errno = ETIMEDOUT;
56+
err_sys("[ERROR]: Timeout");
57+
return recvhdr;
58+
}
59+
60+
n = Recvmsg(fd, &msgrecv, 0);
61+
if (n < sizeof(struct hdr)){
62+
err_sys("[ERROR]: Received packet incomplete");
63+
}
64+
65+
printf("Length: %d\n", (int)n);
66+
//printf("BUFFER: %s\n", (char *)iovrecv[1].iov_base);
67+
printf("SEQ NUM: %u\n", recvhdr.seq);
68+
printf("IS FIN: %u\n", recvhdr.fin);
69+
alarm(0); /* stop SIGALRM timer */
70+
return(recvhdr); /* return size of received datagram */
71+
}
72+
73+
static void sig_alrm(int signo)
74+
{
75+
siglongjmp(jmpbuf, 1);
76+
}
77+

cli_send.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "unprtt.h"
2+
#include <setjmp.h>
3+
4+
#define RTT_DEBUG
5+
6+
static struct msghdr msgsend; /* assumed init to 0 */
7+
static struct hdr {
8+
uint32_t seq; /* sequence # */
9+
uint32_t ts; /* timestamp when sent */
10+
uint32_t fin; /* mark when file eof */
11+
} sendhdr;
12+
13+
ssize_t cli_send(int fd, uint32_t seq_num, uint32_t timestamp, void *outbuff, size_t outbytes)
14+
{
15+
ssize_t n;
16+
struct iovec iovsend[2];
17+
18+
sendhdr.seq = seq_num;
19+
sendhdr.ts = timestamp;
20+
sendhdr.fin = 0;
21+
22+
msgsend.msg_iov = iovsend;
23+
msgsend.msg_iovlen = 2;
24+
25+
iovsend[0].iov_base = &sendhdr;
26+
iovsend[0].iov_len = sizeof(struct hdr);
27+
iovsend[1].iov_base = outbuff;
28+
iovsend[1].iov_len = outbytes;
29+
30+
if (sendmsg(fd, &msgsend, 0)<0){
31+
err_sys("[ERROR:] Send packet error");
32+
return(-1);
33+
}
34+
35+
return(0);
36+
}

0 commit comments

Comments
 (0)