03 BasicTCPSocket
03 BasicTCPSocket
Contents
• Stream socket
• TCP Application
• Functions in client side
• Iterating TCP server
TCP CLIENT
TCP (Transmission Control Protocol)
• WEB
• Image ACK
Data
Example (TCP) TCP Server
socket()
TCP client
bind()
socket()
listen()
connect() establish
accept()
write() data
read()
data write()
read()
end read()
close()
close()
Algorithm for TCP Client in C
• Find the IP address and port number of TCP server
• Create a TCP socket
• socket().
• Returns value
• A new socket file descriptor (a socket “handle”) that you can use to read/
receive data from/to
• If error occurs, return -1 (remember errno)
• The domain is AF_INET, AF_INET6, or AF_UNSPEC, …
• The type argument can be:
• SOCK_STREAM: socket for TCP connection
• SOCK_DGRAM: Socket for datagram communication (UDP)
• SOCK_SEQPACKET: Establishes a reliable, connection based, two way
communication with maximum message size. (This is not available on
most machines.)
• protocol is usually zero, means that the protocol is automatically chosen
according to communication type.
socket() - example
#include <sys/types.h>
#include <sys/socket.h>
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// create a TCP socket
connect()
• Connect a socket to a server
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
• sockfd
• The file descriptor of the local socket from which data will be sent.
• buf
• A buffer containing the data to be transmitted.
• len
• The length of the data in buff.
• flags
• Specifies the way in which the call is made.
• Usually 0
• Return value
• If no error occurs, send() returns the total number of characters sent
• Otherwise, return -1
send (2)
• More about “flags”
• MSG_OOB Send as “out of band” data. TCP supports this,
and it's a way to tell the receiving system that this data
has a higher priority than the normal data. The receiver
will receive the signal SIGURG and it can then receive this
data without first receiving all the rest of the normal data
in the queue.
• MSG_DONTROUTE Don't send this data over a router, just
keep it local.
• MSG_DONTWAIT If send() would block because outbound
traffic is clogged, have it return EAGAIN. This is like a
“enable non-blocking just for this send.”
• MSG_NOSIGNAL If you send() to a remote host which is
no longer recv(), you'll typically get the signal SIGPIPE.
Adding this flag prevents that signal from being raised.
recv
• Receive data on a socket
#include <sys/types.h>
#include <sys/socket.h>
• sockfd
• The descriptor of the socket to be closed.
• Return value
• If no error occurs, close() returns 0.
• Otherwise, return -1 (and errno will be set accordingly)
EXAMPLE & EXERCISE
Analyzing a client
#define SERV_PORT 3000
create a
int main(int argc, char **argv) client socket
{ int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
//Create a socket for the client create a socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) { addr info pointing
perror("Problem in creating the socket"); to server socket
exit(2);
}
//Creation of the remote server socket information structure
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order
Analyzing a client (cont.)
// Connect the client to the server socket
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
write() data
read()
data write()
read()
end read()
close()
close()
bind()
• Associate a socket with an IP address and port number
#include <sys/types.h>
#include <sys/socket.h>
• Where
• sockfd : is the file descriptor of the socket to be bound with the
address in my_addr
• my_addr : is a pointer to the structure of the address to be assigned
to the socket sockfd .
• addrlen : is the size of *my_addr
• Return value
• 0 if no errors
• -1 if has errors
listen()
• Establish a socket to listen for incoming connection.
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
SYN_RECEIVED
SYN/ACK
accept() called
Head connection is hold
send(), recv()
#include <unistd.h>
•s
• A descriptor identifying the socket to be closed.
• Return value
• If no error occurs, close() returns 0.
• Otherwise, return -1 (and errno will be set accordingly)
Analyzing a TCP server
int listenfd, connfd, n;
pid_t childpid;
socklen_t clilen;
char buf[MAXLINE];
struct sockaddr_in cliaddr, servaddr;
creation of
listenfd = socket (AF_INET, SOCK_STREAM, 0);
server socket
servaddr.sin_family = AF_INET;
Preparation of
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); the socket
servaddr.sin_port = htons(SERV_PORT); address struct
Bind the socket
bind (listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); to the port in
address
Listen for connection
listen (listenfd, LISTENQ); to the socket
printf("%s\n","Server running...waiting for connections.");
Analyzing a TCP server
for ( ; ; ) {
clilen = sizeof(cliaddr);
Accept a connection
connfd = accept (listenfd, (struct sockaddr *) &cliaddr, &clilen);
request à return a
printf("%s\n","Received request...");
File Descriptor (FD)
while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
printf("%s","String received from and resent to the client:");
puts(buf); Send and receive
send(connfd, buf, n, 0); data from the FD
}
if (n < 0) {
perror("Read error");
exit(1);
}
close(connfd); // close the file descriptor.
}
close (listenfd); //close listening socket
Exercise 1
• Write your own Echo server to communicate with the
existing Echo client
• Leave your servers to work with the client you have
created in previous lab.
• Get Address IP and port of the client and sent them back
to client.
Exercise 2
• Revise the TCP server and TCP client so that
• User at client side can choose a file to send to server
• Client send the file to server.
• On the server, the file should be readable.
• Hint:
• Read file gradually on Client side and send to server small
messages.
• Use fread and fwrite
Exercise 3
- Go back with application Study Schedule Management program that
we did in Lecture 1.
- Now we modify it so that user can consult Study Schedule over
Internet from everywhere meanwhile to schedules are stored in a
server.
- User interface will be on client side
- Schedule storage and processing will be on server side.
- Login: client take username, password from user and send to server. Server
verify username and password if they match.
- Read schedule:
- Client provide interface for user to enter a week day (or ALL)
- Server extract schedule of the day (or busy schedule) and send back to student
Conclusion:
- Do not send compound datatype.