Download CS4514 B06: TCP/IP Socket Programming - HELP Session 1 and more Study notes Data Communication Systems and Computer Networks in PDF only on Docsity!
CS4514 B
HELP Session 1
Presented by Mingzhe Li [email protected]
CS4514 – TCP/IP Socket Programming
Outline
•^
Project 1 Overview
•^
Unix Network Programming^ –
TCP Client – TCP Server
•^
Processing commands
•^
How to find help and other tips.
Project 1 missions (in handout) •^
The Client: 1.^
Reading a command from a script fileor from console.
Sending the command to the server.
Receiving and displaying theinformation from the server.
Writing the results to the log file^ LClient.log.
Project 1 missions (in handout) •^
Server:^ 1.Processing the command from the
client and return the result to the client.
2.Maintaining the records to keep the
location information.
3.Writing the complete database to the
file
LDatabase.txt
when the server
received the “quit EOF” command.
read()
connection establishment
Server (connection-oriented protocol)blocks until connection
from client
Client
socket()bind()listen()accept() read() write()
socket()connect()write()
process request
data (request)data (reply)
Socket system calls forSocket system calls forconnectionconnection-
-orientedoriented
protocol ( TCP )protocol ( TCP )
What Do We Need?
-^
Data communication between two hostson the Internet require the fivecomponents : { protocol, local-addr, local-process, foreign-addr, foreign-process
-^
The different system calls for socketsprovides values for one or more of thesecomponents.
TCP Connection (Client)
•^
Connection Oriented^ –
Specify transportaddress once atconnection
•^
Use File Operations
-^ read() / write()
or
-^ recv() / send()
•^
Reliable Protocol
socket() connect() read() / write()^ recv() /send()
close()
Example: TCP Client
int sd; struct hostent *hp;
/ /usr/include/netdb.h /
struct sockaddr_in server;
/ /usr/include/netinet/in.h /
/ prepare a socket / if ( (sd =
socket
( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
perror( strerror(errno) ); exit(-1);
}
Example: TCP Client
(Continued)
/ connect to the server / if (
connect
(^ sd, (struct sockaddr*) &server, sizeof(server) ) < 0
perror( strerror(errno) ); exit(-1);
} / send/receive data / while (1) {
read/write();
} / close socket / close
( sd );
TCP Connection (Server)
•^
Bind transportaddress to socket
•^
Listen to the socket
•^
Accept connection ona new socket
socket()^ bind()^ listen() accept()^ close() read()/write()
Example: TCP Server
(Continued)
bind
( sd, (struct sockaddr*) &server, sizeof(server) ); listen
( sd, backlog ); while (1) {^ nsd =
accept
( sd, (struct sockaddr *) &client, sizeof(client) );
read
write
close( nsd ); } close( sd );
Outline
•^
Project 1 Overview
•^
Unix Network Programming^ –
TCP Client – TCP Server
•^
Processing commands
•^
How to find help and other tips.
Commands
-^
In the
login, add, remove
, and
quit
commands: The server only returns one message to the client.
-^
In the
list command,
The server could
return multiple messages to the client.^ “Each entry, which meets the search condition, is sent as aseparate
TCP message back to the Client
.”
Login Command
•^
Login Command Format.^ login
•^
Login Command Handling^ –
For The Client: When the Client reads alogin command, the client establishes aTCP connection to the Server. – For The Server: When the Serverreceives a “login”, it replies “Hello!” tothe client.