First, the PowerPoint slides.
For Internet services using TCP or UDP, your service name is your port number. Important services use well-known port numbers, assigned by the IANA.
// Server code in C
// adapted from http://en.wikipedia.org/wiki/Berkeley_sockets
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
int main()
{
struct addrinfo hint;
struct sockaddr_storage bindaddr_storage;
struct sockaddr_in6 *bindaddr6 =
(struct sockaddr_in6 *)&bindaddr_storage;
struct sockaddr_in *bindaddr =
(struct sockaddr_in *)&bindaddr_storage;
struct addrinfo *result;
struct sockaddr_storage peer;
int peersize;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
int retval;
int i32SocketFD = socket(PF_INET6, SOCK_STREAM, 0);
char msg[] = "Success!\n";
if(-1 == i32SocketFD)
{
perror("can not create socket");
exit(-1);
}
bzero(&bindaddr_storage, sizeof(bindaddr_storage));
bindaddr6->sin6_family = AF_INET6;
bindaddr6->sin6_addr = in6addr_any; /* struct assignment */
bindaddr6->sin6_port = htons(12345);
// *** attention: on MacOS, this has to be
// sizeof(struct sockaddr_in6)!!! ***
if(-1 == bind(i32SocketFD,(struct sockaddr*) bindaddr6,
sizeof(struct addrinfo)))
{
printf("error bind failed");
perror("main");
exit(-1);
}
if(-1 == listen(i32SocketFD, 10))
{
printf("error listen failed");
exit(-1);
}
// Here, you should print out your local address using
// getsockname() and getnameinfo() (and gai_strerror on error)
for(; ;)
{
int i32ConnectFD = accept(i32SocketFD, NULL, NULL);
if(0 > i32ConnectFD)
{
// improve error reporting here, using perror()
printf("error accept failed");
exit(-1);
}
// Here, you should print out your local address
// and the peer address using
// getsockname(), getpeername(), and getnameinfo()
// (and gai_strerror on error)
// perform read write operations ...
// add error checking and reporting here
write(i32ConnectFD, msg, sizeof(msg));
// wait one minute; this lets us see the connection in place, and
// even see the behavior as we try to start a second connection
// at the same time
sleep(60);
// add error checking and reporting here
shutdown(i32ConnectFD, 2);
// add error checking and reporting here
close(i32ConnectFD);
}
// add error checking and reporting here
close(i32SocketFD);
return 0;
}
Your operation, after some of the modifications in the homework,
should look something like this: