short connections = something like echo it is very short term connection
long connections = telnet , ssh is very long term connection it goes on until client send quit message to server
Socket
communciation end point to which an application can write data and from which an application can read data.
Stream Sockets
provide a reliable , full duplex , steram oriented communciation channel.
do not support broadcasting and muticasting
stream = no concept of message boundary
reliable = can be reconstructed
flow control using sliding window =
congestion control = slow start , lot of strategies.
we can check with strace command how connection works when we try to conenct echo (7)
when type something it just echo back, and we can see read, write method calling
4way hand shake can be checked when closing socket with wire shark.
when the local address is stored in socket data strcuture we say that the socket is half associated
when both local and remote address are stored in socket data strcuture, fully associated.
network = big endian
x6 = little endian
so we use byte ordering function like htons to synchronize
#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 <unistd.h>
#include <string.h>
int main(int argc, char** argv){
//*** STEP-I: Create a socket
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
//*** STEP-II: Populate Socket's DS for remote IP and Port, and
//*** let the local IP and Port be selected by the OS itself
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(54154);
inet_aton("192.168.100.20", &dest_addr.sin_addr);
memset(&(dest_addr.sin_zero), '\0', sizeof dest_addr.sin_zero);
//*** STEP-III: Connect this socket to the server's socket
connect(sockfd, (struct sockaddr*)& dest_addr, sizeof dest_addr);
//*** STEP-IV: Client reads string from stdin and send it to server
//*** then read string returned by server and display on stdout
char buff1[128],buff2[128] ;
while(1){
int n = read(0, buff1, sizeof buff1);
buff1[n] = '\0';
write(sockfd, buff1, strlen(buff1));
n = read(sockfd, buff2, strlen(buff1));
buff2[n] = '\0';
write(1, buff2, n);
}
//*** STEP-V: Close socket
close(sockfd);
exit(0);
}
echo client code
#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 <unistd.h>
#include <string.h>
#define BACKLOG 10 //how many pending connections queue will hold
int main(){
int server_sockfd;//socket on which server process will listen for incoming con
int client_sockfd; //socket on which the server will be comm with the client
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
//*** STEP-I: Create passive socket for the server
server_sockfd = socket (AF_INET, SOCK_STREAM, 0);
//*** STEP-II: Create a address structure containing server IP addr
//*** and port, then bind the server_sockfd with this address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(54154);
inet_aton("192.168.100.20", &server_addr.sin_addr);
memset(&(server_addr.sin_zero), '\0', sizeof server_addr.sin_zero);
bind(server_sockfd, (struct sockaddr*)&server_addr, sizeof server_addr);
//*** STEP-III: Create a connection queue and wait for clients
listen(server_sockfd, BACKLOG);
while(1){
fprintf(stderr,"\nServer waiting for client connection...");
//*** STEP-IV: Accept a connection, blocks until connection from client is established
//**** will return a brand new descriptor for comm with this single connection
int client_len = sizeof client_addr;
client_sockfd=accept(server_sockfd,(struct sockaddr*)&client_addr,&(client_len));
fprintf(stderr, "\n********* CLIENT CONNECTION ESTABLISHED ********");
//STEP-V ***** Read from sockfd and write back
char buf[100];
int rv;
while (rv = read(client_sockfd, buf, sizeof buf))
write(client_sockfd, buf, rv);
close(client_sockfd);
fprintf(stderr, "\n********* CLIENT CONNECTION TERMINATED ********");
}
return 0;
}
echo server code
with command
scp ./tcpechoserver root@ip.port: ~/
we can securely copy file to remote server
'Operating System > System Programming(Arif Butt)' 카테고리의 다른 글
Lec35) Socket Programming Part-2 (0) | 2021.10.31 |
---|---|
Lec33) Overview of TCP/IP Architecture and Service (0) | 2021.10.23 |
Lec31) Synchronization among Threads (0) | 2021.07.25 |
Lec29, 30) Programming With Shared Memory , Memory Mapped Files (0) | 2021.07.24 |
Lec28) Message Queues (0) | 2021.07.21 |