





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
manual programming
Typology: Study notes
1 / 45
This page cannot be seen from the preview
Don't miss anything!
STUDY OF HEADER FILES
1. stdio.h: Has standard input and output library providing simple and efficient buffered stream IO interface. 2. unistd.h: It is a POSIX standard for open system interface. 3. string.h: This header file is used to perform string manipulation operations on NULL terminated strings. 4. stdlib.h: This header file contains the utility functions such as string conversion routines, memory allocation routines, random number generator, etc. 5. sys/types.h: Defines the data type of socket address structure in unsigned long. 6. sys/socket.h: The socket functions can be defined as taking pointers to the generic socket address structure called sockaddr. 7. netinet/in.h: Defines the IPv4 socket address structure commonly called Internet socket address structure called sockaddr_in. 8. netdb.h: Defines the structure hostent for using the system call gethostbyname to get the network host entry. 9. time.h: Has structures and functions to get the system date and time and to perform time manipulation functions. We use the function ctime(), that is defined in this header file , to calculate the current date and time. 10. sys/stat.h: Contains the structure stat to test a descriptor to see if it is of a specified type. Also it is used to display file or file system status.
¾ An out-of-band data transmission mechanism, may be supported. 2.SOCK_DGRAM: ¾ Supports datagram (connectionless, unreliable messages of a fixed maximum length). 3.SOCK_SEQPACKET: ¾ Provides a sequenced , reliable, two-way connection based data transmission path for datagrams of fixed maximum length. 4.SOCK_RAW: ¾ Provides raw network protocol access. 5.SOCK_RDM: ¾ Provides a reliable datagram layer that doesn’t guarantee ordering. 6.SOCK_PACKET: ¾ Obsolete and shouldn’t be used in new programs.
2.man connect:
NAME: connect – initiate a connection on a socket.
SYNOPSIS:
#include<sys/types.h> #include<sys/socket.h> int connect(int sockfd,const (struct sockaddr*)serv_addr,socklen_t addrlen);
DESCRIPTION:
¾ The file descriptor sockfd must refer to a socket. ¾ If the socket is of type SOCK_DGRAM then the serv_addr address is the address to which datagrams are sent by default and the only addr from which datagrams are received. ¾ If the socket is of type SOCK_STREAM or SOCK_SEQPACKET , this call attempts to make a connection to another socket.
RETURN VALUE :
¾ If the connection or binding succeeds, zero is returned. ¾ On error , -1 is returned , and error number is set appropriately.
EBADF Not a valid Index. EFAULT The socket structure address is outside the user’s address space. ENOTSOCK Not associated with a socket. EISCONN Socket is already connected. ECONNREFUSED No one listening on the remote address.
3.man accept
accept/reject job is sent to a destination.
SYNOPSIS:
accept destination(s) reject[-t] [-h server] [-r reason] destination(s)
DESCRIPTION:
¾ accept instructs the printing system to accept print jobs to the specified destination. ¾ The –r option sets the reason for rejecting print jobs. ¾ The –e option forces encryption when connecting to the server.
4.man send
NAME:
send, sendto, sendmsg - send a message from a socket.
SYNOPSIS:
#include<sys/types.h> #include<sys/socket.h>
ssize_t send(int s, const void *buf, size_t len, int flags); ssize_t sendto(int s, const void buf, size_t len, int flags, const struct sock_addrto, socklen_t tolen); ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
¾ The system calls send, sendto and sendmsg are used to transmit a message to another socket. ¾ The send call may be used only when the socket is in a connected state. ¾ The only difference between send and write is the presence of flags. ¾ The parameter is the file descriptor of the sending socket.
5.man recv
NAME:
recv, recvfrom, recvmsg – receive a message from a socket.
SYNOPSIS:
#include<sys/types.h> #include<sys/socket.h> ssize_t recv(int s, void *buf, size_t len, int flags);
ifconfig interface[aftype] options | address……
DESCRIPTION:
¾ ifconfig is used to configure the kernel resident network interfaces. ¾ It is used at boot time to setup interfaces as necessary. ¾ After that, it is usually only needed when debugging or when system tuning is needed. ¾ If no arguments are given, ifconfig displays the status of the currently active interfaces.
9. man bind
SYNOPSIS:
bind[-m keymap] [-lp sv psv]
10. man htons/ man htonl
NAME:
htonl, htons, ntohl, ntohs - convert values between host and network byte order.
SYNOPSIS:
#include<netinet/in.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint32_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);
DESCRIPTION:
¾ The htonl() function converts the unsigned integer hostlong from host byte order to network byte order. ¾ The htons() converts the unsigned short integer hostshort from host byte order to network byte order. ¾ The ntohl() converts the unsigned integer netlong from network byte order to host byte order.
11. man gethostname
NAME:
gethostname, sethostname- get/set host name.
SYNOPSIS:
#include<unistd.h> int gethostname(char *name,size_t len); int sethostname(const char *name,size_t len);
DESCRIPTION:
¾ These functions are used to access or to change the host name of the current processor. ¾ The gethostname() returns a NULL terminated hostname(set earlier by sethostname()) in the array name that has a length of len bytes. ¾ In case the NULL terminated then hostname does not fit ,no error is returned, but the hostname is truncated. ¾ It is unspecified whether the truncated hostname will be NULL terminated.
12. man gethostbyname
NAME:
gethostbyname, gethostbyaddr, sethostent, endhostent, herror, hstr – error – get network host entry.
SYNOPSIS:
#include<netdb.h> extern int h_errno; struct hostent *gethostbyname(const char *name); #include<sys/socket.h> struct hostent *gethostbyaddr(const char *addr)int len, int type); struct hostent *gethostbyname2(const char *name,int af);
DESCRIPTION:
¾ The gethostbyname() returns a structure of type hostent for the given hostname. ¾ Name->hostname or IPV4/IPV6 with dot notation. ¾ gethostbyaddr()- struct of type hostent / host address length ¾ Address types- AF_INET, AF_INET6. ¾ sethostent() – stay open is true(1). ¾ TCP socket connection should be open during queries. ¾ Server queries for UDP datagrams. ¾ endhostent()- ends the use of TCP connection. ¾ Members of hostent structure: a) h_name b) h_aliases c) h_addrtype d) h_length e) h_addr-list f) h_addr.
RESULT :
Thus the basic functions used for Socket Programming was studied successfully.
int main(int asrgc,char*argv[]) { int bd,sd,ad; char buff[1024]; struct sockaddr_in cliaddr,servaddr; socklen_t clilen; clilen=sizeof(cliaddr); bzero(&servaddr,sizeof(servaddr));
/Socket address structure/** servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(1999);
/TCP socket is created, an Internet socket address structure is filled with wildcard address & server’s well known port/** sd=socket(AF_INET,SOCK_STREAM,0);
/Bind function assigns a local protocol address to the socket/** bd=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
/Listen function specifies the maximum number of connections that kernel should queue for this socket/** listen(sd,5); printf("Server is running….\n");
/The server to return the next completed connection from the front of the completed connection Queue calls it/** ad=accept(sd,(struct sockaddr)&cliaddr,&clilen); while(1) { bzero(&buff,sizeof(buff)); /Receiving the request message from the client/* recv(ad,buff,sizeof(buff),0); printf("Message received is %s\n",buff); } } Client: tcpclient.c #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<netinet/in.h> #include<netdb.h> #include<arpa/inet.h> int main(int argc,char * argv[]) { int cd,sd,ad; char buff[1024]; struct sockaddr_in cliaddr,servaddr;
struct hostent h; /This function looks up a hostname and it returns a pointer to a hostent structure that contains all the IPV4 address/* h=gethostbyname(argv[1]); bzero(&servaddr,sizeof(servaddr));
/Socket address structure/** servaddr.sin_family=AF_INET; memcpy((char *)&servaddr.sin_addr.s_addr,h->h_addr_list[0],h->h_length); servaddr.sin_port = htons(1999);
/Creating a socket, assigning IP address and port number for that socket/** sd = socket(AF_INET,SOCK_STREAM,0);
/Connect establishes connection with the server using server IP address/** cd=connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); while(1) { printf("Enter the message: \n");
/Reads the message from standard input/** fgets(buff,100,stdin);
/Send function is used on client side to send data given by user on client side to the server/** send(sd,buff,sizeof(buff)+1,0); printf("\n Data Sent "); //recv(sd,buff,strlen(buff)+1,0); printf("%s",buff); } } SAMPLE OUTPUT:
Server : (Host Name:Root1) [root@localhost 4ita33]# vi tcpserver.c [root@localhost 4ita33]# cc tcpserver.c [root@localhost 4ita33]# ./a.out Server is running…. Message received is hi
Message received is hi
Client: (Host Name:Root2)
[root@localhost 4ita33]# vi tcpclient.c [root@localhost 4ita33]# cc tcpclient.c [root@localhost 4ita33]# ./a.out 127.0.0. Enter the message:
Ex No: 4
Date:
UDP ECHO CLIENT SERVER COMMUNICATION
There are two hosts, Client and Server. The Client accepts the message from the user and sends it to the Server. The Server receives the message, prints it and echoes the message back to the Client.
TECHNICAL OBJECTIVE:
To implement an UDP Echo Client-Server application , where the Client on establishing a connection with the Server, sends a string to the Server. The Server reads the String, prints it and echoes it back to the Client.
METHODOLOGY:
Server: ¾ Include the necessary header files. ¾ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM. ¾ Initialize server address to 0 using the bzero function. ¾ Assign the sin_family to AF_INET, sin_addr to INADDR_ANY, sin_port to SERVER_PORT, a macro defined port number. ¾ Bind the local host address to socket using the bind function. ¾ Within an infinite loop, receive message from the client using recvfrom function, print it on the console and send (echo) the message back to the client using sendto function. Client: ¾ Include the necessary header files. ¾ Create a socket using socket function with family AF_INET, type as SOCK_DGRAM. ¾ Initialize server address to 0 using the bzero function. ¾ Assign the sin_family to AF_INET. ¾ Get the server IP address from the console. ¾ Using gethostbyname function assign it to a hostent structure, and assign it to sin_addr of the server address structure. ¾ Within an infinite loop, read message from the console and send the message to the server using the sendto function. ¾ Receive the echo message using the recvfrom function and print it on the console.
CODING: Server: udpserver.c
#include<sys/socket.h> #include<stdio.h> #include<unistd.h> #include<string.h> #include<netinet/in.h> #include<netdb.h>
#include<arpa/inet.h> #include<sys/types.h> int main(int argc,char *argv[]) { int sd; char buff[1024]; struct sockaddr_in cliaddr,servaddr; socklen_t clilen; clilen=sizeof(cliaddr);
/UDP socket is created, an Internet socket address structure is filled with wildcard address & server’s well known port/** sd=socket(AF_INET,SOCK_DGRAM,0); if (sd<0) { perror ("Cannot open Socket"); exit(1); } bzero(&servaddr,sizeof(servaddr));
/Socket address structure/** servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(5669);
/Bind function assigns a local protocol address to the socket/** if(bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) { perror("error in binding the port"); exit(1); } printf("%s","Server is Running…\n");
while(1) { bzero(&buff,sizeof(buff));
/Read the message from the client/** if(recvfrom(sd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&clilen)<0) { perror("Cannot rec data"); exit(1); } printf("Message is received \n",buff);
/Sendto function is used to echo the message from server to client side/** if(sendto(sd,buff,sizeof(buff),0,(struct sockadddr*)&cliaddr,clilen)<0) {
perror("Cannot send data"); exit(1);
} printf("Data sent to UDP Server:%s",buff); bzero(buff,sizeof(buff));
/Receiving the echoed message from server/** if(recvfrom (sd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&len)<0) { perror("Cannot receive data"); exit(1); } printf("Received Data from server: %s",buff); } close(sd); return 0; }
Server : (Host Name:Root1) [root@localhost 4ita33]# vi udpserver.c [root@localhost 4ita33]# cc udpserver.c [root@localhost 4ita33]# ./a.out Server is Running…
Message is received Send data to UDP Client: hi
Message is received Send data to UDP Client: how are u
Client: (Host Name:Root2)
[root@localhost 4ita33]# vi udpclient.c [root@localhost 4ita33]# cc udpclient.c [root@localhost 4ita33]# ./a.out 127.0.0. Enter input data : hi Data sent to UDP Server:hi Received Data from server: hi
Enter input data : how are u Data sent to UDP Server:how are u Received Data from server: how are u
Enter input data :
Thus, the UDP ECHO client server communication is established by sending the message from the client to the server and server prints it and echoes the message back to the client.