Title: COP4600 Project 5 Networking: Socket Programming
1COP4600 Project 5NetworkingSocket Programming
- InKwan Yu
- Iyu_at_cise.ufl.edu
2Todays Topics
- Project Overview
- Socket Functions
- UDP Client/Server
- TCP Client/Server
- Unix Domain Client/Server
3Project 5 Sequence Diagram
Credit card is a command line argument for
clem Ticket is a ticket number that sam gave
clem and that clem passes along to sue
Same host
The end of text \0
ticket
ticket
clem
sam
sue
printf ticket fgets in 1..4
printf ticket
credit card
printf line 1
printf credit card fgets ticket
line 1
ack
4For Socket Programming
- For basic socket programming information look at
- http//www.cise.ufl.edu/cop4600sp05/prj/5/info.ht
m (simple sample codes are included) - Make use of man command
5Whats Socket
- Whats socket?
- A socket is an IPC(Inter Process Communication)
facility between hosts - Functions
- socket(), bind(), listen(), accept(),
recv()/read(), send()/write(), connect(),
recvfrom(), sendto(), close()
6Socket Functions
- socket()
- Sets the type of socket, protocol
- Returns a socket descriptor
- bind()
- Make an association of a socket descriptor and
struct sockaddr - Two types of sockaddr
- struct sockaddr_in for (IP, port)
- struct sockaddr_un for file path
- In a UDP client this can be omitted (AF_LOCAL UDP
client should call this function though)
7Socket Functions
- listen()
- For TCP server
- Determine the size of waiting queue of clients
for a socket - accept()
- For TCP server
- Blocks until a new client comes in
- Returns a new duplicate socket descriptor
- The listening socket will continue to accept
connection (until the queue fills up) while the
new socket descriptor will be used for
communication
8Socket Functions
- recv()
- TCP based
- Receive data through the new socket
- If the sender has closed the connection, 0
octect(byte) is received - Similar to read()
- send()
- TCP based
- Send data through the new socket
- Similar to write()
9Socket Functions
- connect()
- For TCP client
- Make a communication link with a server
- The server is specified using struct sockaddr
- close()
- Close a socket
10Socket Functions
- recvfrom()
- UDP based
- Gets message from a sender
- struct sockaddr will contain the sender
information after the call - sendto()
- UDP based
- Sends a message to a receiver
- struct sockaddr specifies the receiver
11UDP Connection
Client
Server
socket()
socket()
bind()
bind()
recvfrom()
sendto()
recvfrom()
sendto()
12UDP Server
int main(void) int sockfd struct
sockaddr_in my_addr // my address
information struct sockaddr_in their_addr //
connector's address information isockfd
socket(AF_INET, SOCK_DGRAM, 0))
bzero(my_addr, sizeof(my_addr))
my_addr.sin_family AF_INET // host
byte order my_addr.sin_port htons(MYPORT)
// short, network byte order
my_addr.sin_addr.s_addr INADDR_ANY //
automatically set IP bind(sockfd, (struct
sockaddr )my_addr, sizeof(struct sockaddr))
addr_len sizeof(struct sockaddr)
recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct
sockaddr )their_addr, addr_len)
sendto(sockfd, buf, MAXBUFLEN-1, 0, (struct
sockaddr )their_addr, addr_len)
close(sockfd) return 0
socket()
bind()
recvfrom()
sendto()
13UDP Client
int main(int argc, char argv) int sockfd
struct sockaddr_in their_addr // connector's
address information struct hostent he
hegethostbyname(argv1)) perror("gethostbyname"
) sockfd socket(AF_INET, SOCK_DGRAM, 0))
bzero((their_addr, sizeof(their_addr))
their_addr.sin_family AF_INET // host byte
order their_addr.sin_port htons(MYPORT) //
short, network byte order their_addr.sin_addr
((struct in_addr )he-gth_addr) sendto(sockfd,
argv2, strlen(argv2), 0, (struct sockaddr
)their_addr, sizeof(struct
sockaddr)) recvfrom(sockfd, buf, MAXBUFLEN-1,
0, NULL, NULL) close(sockfd) return 0
socket()
bind()
sendto()
recvfrom()
14TCP Connection
Server
socket()
bind()
Client
listen()
socket()
accept()
connect()
recv()/read()
send()/write()
send()/write()
recv()/read()
15TCP Server
void main(void) int sockfd, new_fd //
listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr, their_addr //
server, client address information int yes1
// boolean value 1 sockfd socket(AF_INET,
SOCK_STREAM, 0) bzero(my_addr,
sizeof(my_addr)) my_addr.sin_family AF_INET
// host byte order my_addr.sin_port
htons(MYPORT) // short, network byte order
my_addr.sin_addr.s_addr INADDR_ANY //
automatically fill with my IP bind(sockfd,
(struct sockaddr )my_addr, sizeof(struct
sockaddr)) listen(sockfd, BACKLOG) while(1)
int sin_size sizeof(struct
sockaddr_in) new_fd accept(sockfd,
(struct sockaddr )their_addr, sin_size))
int rc recv(new_fd, buf, MAXBUFSIZE-1, 0)
send(new_fd, buf, rc, 0) close(new_fd) //
parent doesn't need this
socket()
bind()
listen()
accept()
recv()/read()
send()/write()
16TCP Client
int main(int argc, char argv) int sockfd,
numbytes struct hostent he struct
sockaddr_in their_addr // connector's address
information hegethostbyname(argv1))
sockfd socket(AF_INET, SOCK_STREAM, 0))
bzero(their_addr, sizeof(their_addr))
their_addr.sin_family AF_INET // host byte
order their_addr.sin_port htons(PORT) //
short, network byte order their_addr.sin_addr
((struct in_addr )he-gth_addr)
connect(sockfd, (struct sockaddr )their_addr,
sizeof(struct sockaddr)) send(sockfd, argv2,
strlen(argv2), 0) recv(sockfd, buf,
MAXDATASIZE-1, 0) close(sockfd) return 0
socket()
connect()
send()/write()
recv()/read()
17Unix Domain UDP Server
define SOCKET_PATH "/tmp/myserver" main()
int sock struct sockaddr_un my_addr,
their_addr int sock socket(AF_UNIX,
SOCK_DGRAM, 0)) bzero(my_addr,
sizeof(my_addr)) my_addr.sun_family
AF_UNIX strcpy(my_addr.sun_path,
SOCKET_PATH) bind(sock, (struct sockaddr )
my_addr, sizeof(my_addr)) int len
sizeof(their_addr) recvfrom(sock, buf,
MAXBUFSIZE, 0, (struct sockaddr )
their_addr, len) sendto(sock, buf,
strlen(buf), 0, (struct sockaddr ) their_addr,
len) unlink(SOCKET_PATH) close(sock)
exit(0)
18Unix Domain UDP Client
define SOCKET_PATH "/tmp/myserver" define
SOCKET_PATHCLNT "/tmp/my_af_unix_client" main(int
argc, char argv) int sock struct
sockaddr_un my_addr, their_addr sock
socket(AF_UNIX, SOCK_DGRAM, 0))
bzero(my_addr, sizeof(my_addr))
my_addr.sun_family AF_UNIX
strcpy(my_addr.sun_path, SOCKET_PATHCLNT)
bind(sock, (struct sockaddr ) my_addr,
sizeof(my_addr)) bzero(their_addr,
sizeof(their_addr)) their_addr.sun_family
AF_UNIX strcpy(their_addr.sun_path,
SOCKET_PATH) sendto(sock, argv1,
strlen(argv1), 0, (struct sockaddr )
their_addr, sizeof(struct sockaddr))
recvfrom(sock, buf, MAXBUFSIZE-1, 0, NULL,
NULL) unlink(SOCKET_PATHCLNT) close(sock)
exit(0)