forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add echo server application (tcpechoserver/udpechoserver)
- Loading branch information
Showing
5 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,8 @@ NET_UPROGS=\ | |
_ifset\ | ||
_ifup\ | ||
_ifdown\ | ||
_tcpechoserver\ | ||
_udpechoserver\ | ||
|
||
UPROGS += $(NET_UPROGS) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "types.h" | ||
#include "user.h" | ||
#include "socket.h" | ||
|
||
int | ||
main (int argc, char *argv[]) | ||
{ | ||
int soc, acc, peerlen, ret; | ||
struct sockaddr_in self, peer; | ||
unsigned char *addr; | ||
char buf[2048]; | ||
|
||
printf(1, "Starting TCP Echo Server\n"); | ||
soc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
if (soc == 1) { | ||
printf(1, "socket: failure\n"); | ||
exit(); | ||
} | ||
printf(1, "socket: success, soc=%d\n", soc); | ||
self.sin_family = AF_INET; | ||
self.sin_addr = INADDR_ANY; | ||
self.sin_port = hton16(7); | ||
if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1) { | ||
printf(1, "bind: failure\n"); | ||
close(soc); | ||
exit(); | ||
} | ||
addr = (unsigned char *)&self.sin_addr; | ||
printf(1, "bind: success, self=%d.%d.%d.%d:%d\n", addr[0], addr[1], addr[2], addr[3], ntoh16(self.sin_port)); | ||
listen(soc, 100); | ||
printf(1, "waiting for connection...\n"); | ||
peerlen = sizeof(peer); | ||
acc = accept(soc, (struct sockaddr *)&peer, &peerlen); | ||
if (acc == -1) { | ||
printf(1, "accept: failure\n"); | ||
close(soc); | ||
exit(); | ||
} | ||
addr = (unsigned char *)&peer.sin_addr; | ||
printf(1, "accept: success, peer=%d.%d.%d.%d:%d\n", addr[0], addr[1], addr[2], addr[3], ntoh16(peer.sin_port)); | ||
while (1) { | ||
ret = recv(acc, buf, sizeof(buf)); | ||
if (ret <= 0) { | ||
printf(1, "EOF\n"); | ||
break; | ||
} | ||
printf(1, "recv: %d bytes data received\n", ret); | ||
hexdump(buf, ret); | ||
send(acc, buf, ret); | ||
} | ||
close(soc); | ||
exit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "types.h" | ||
#include "user.h" | ||
#include "socket.h" | ||
|
||
int | ||
main (int argc, char *argv[]) | ||
{ | ||
int soc, peerlen, ret; | ||
struct sockaddr_in self, peer; | ||
unsigned char *addr; | ||
char buf[2048]; | ||
|
||
printf(1, "Starting UDP Echo Server\n"); | ||
soc = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
if (soc == 1) { | ||
printf(1, "socket: failure\n"); | ||
exit(); | ||
} | ||
printf(1, "socket: success, soc=%d\n", soc); | ||
self.sin_family = AF_INET; | ||
self.sin_addr = INADDR_ANY; | ||
self.sin_port = hton16(7); | ||
if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1) { | ||
printf(1, "bind: failure\n"); | ||
close(soc); | ||
exit(); | ||
} | ||
addr = (unsigned char *)&self.sin_addr; | ||
printf(1, "bind: success, self=%d.%d.%d.%d:%d\n", addr[0], addr[1], addr[2], addr[3], ntoh16(self.sin_port)); | ||
printf(1, "waiting for message...\n"); | ||
while (1) { | ||
peerlen = sizeof(peer); | ||
ret = recvfrom(soc, buf, sizeof(buf), (struct sockaddr *)&peer, &peerlen); | ||
if (ret <= 0) { | ||
printf(1, "EOF\n"); | ||
break; | ||
} | ||
addr = (unsigned char *)&peer.sin_addr; | ||
printf(1, "recvfrom: %d bytes data received, peer=%d.%d.%d.%d:%d\n", ret, addr[0], addr[1], addr[2], addr[3], ntoh16(peer.sin_port)); | ||
hexdump(buf, ret); | ||
sendto(soc, buf, ret, (struct sockaddr *)&peer, peerlen); | ||
} | ||
close(soc); | ||
exit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters