Assignment 5 Slides - 1
Assignment 5 Slides - 1
1
Lab 5: Client/Server
} You will write a networked client/server application
which allows a client program to download and upload
files from and to a remote server.
2
Overview of the Sockets Interface
Client Server
socket socket
bind open_listenfd
open_clientfd
listen
Connection
request
connect accept
4
Server: socket, bind, listen, accept
// Forcefully attaching socket to the port 8080
// Set socket options
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
This helps in manipulating options for the socket referred by the file descriptor sockfd.
This is completely optional, but it helps in reuse of address and port. Prevents error
such as: “address already in use”.
} To set options at the socket level, specify the level argument as SOL_SOCKET
} REUSEADDR and REUSEPORT when binding
5
Server: socket, bind, listen, accept
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( 8080 );
} After creation of the socket, bind function binds the socket to the address and port
number specified in addr(custom data structure). In the example code, we bind the
server to the localhost, hence we use INADDR_ANY to specify the IP address.
6
Server: socket, bind, listen, accept
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
} It puts the server socket in a passive mode, where it waits for the client to
approach the server to make a connection.
} The backlog, defines the maximum length to which the queue of pending
connections for sockfd may grow. If a connection request arrives when the queue is
full, the client may receive an error with an indication of ECONNREFUSED.
7
Server: socket, bind, listen, accept
if ((new_socket = accept(server_fd,
(struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
} It extracts the first connection request on the queue of pending
connections for the listening socket, sockfd, creates a new connected
socket, and returns a new file descriptor referring to that socket.
} At this point, connection is established between client and server, and they
are ready to transfer data.
8
Overview of the Sockets Interface
Client Server
socket socket
bind open_listenfd
open_clientfd
listen
Connection
request
connect accept
close
Server: Main Loop
} The server loops endlessly, waiting for connection
requests, then reading input from the client, and
respond back to the client.
main() {
while(1) {
/* Accept(): wait for a connection request */
/* echo(): read and echo input lines from client till EOF */
/* Close(): close the connection */
}
}
Client: socket, connect
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
11
Client: socket, connect
} // Connect
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
} The connect() system call connects the socket referred to by the file descriptor
sockfd to the address specified by addr. Server’s address and port is specified in
addr.
12