B&D's Networking Lab

Embed Size (px)

Citation preview

  • 8/7/2019 B&D's Networking Lab

    1/14

    NETWORKING LABASSIGNMENT

    NAME:

    UNIV. ROLL:

    YEAR: 3

    RD

    STREAM : IT

  • 8/7/2019 B&D's Networking Lab

    2/14

    IP configuration

    In Windows:Steps:1.Start-> Connect to-> Show all network->selectconnection(lan/wireless)2. Rt click on that -> go to Properties3. Click on tcp/ip4 -> click on properties4. New window will pop up , Change ip,subnet,gateway,server address accordingly5. click on OK-> click on CLOSE

    IN LINUX:

    Command mode:# ifconfig eth0 netmask upEg. # ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up

    OR

    GUI Mode:$ system-config-network

    New window pop up Steps:1. Select the connection(eth0/eth1/wireless etc)2.Change the IP ,Subnet,Gateway , server address as requied3. Click on OK.

  • 8/7/2019 B&D's Networking Lab

    3/14

    Socket Theory

    Socket: An interface between an application process and

    transport layerThe application process can send/receive messages

    to/from another applicationprocess (local or remote)via a socket.

    In Unix jargon, a socket is a file descriptor an integer associated with an openfile

    Types of Sockets: Internet Sockets, unix sockets, X.25 sockets etc

    Internet sockets characterized by IP Address (4 bytes) and port number (2bytes)

    Types of Internet SocketsStream Sockets (SOCK_STREAM) - Connection oriented,

    Rely on TCP toprovide reliable two-way connected communication

    Datagram Sockets (SOCK_DGRAM) - Rely on UDP , Connection is unreliable

    bind() - what port am I on?

    Used to associate a socket with a port on the local machine. The port numberis used by the kernel to match an incoming packet to aprocess.

    int bind(int sockfd, struct sockaddr *my_addr, int addrlen)sockfd is the socket descriptor returned by socket()my_addr is pointer to struct sockaddr that contains

    information aboutyour IP address and port

    addrlen is set to sizeof(struct sockaddr)returns -1 on errormy_addr.sin_port = 0; //choose an unused port at randommy_addr.sin_addr.s_addr = INADDR_ANY; //use my IP addrconnect() - Hello! Connects to a remote host

  • 8/7/2019 B&D's Networking Lab

    4/14

    int connect(int sockfd, struct sockaddr *serv_addr, int addrlen)

    sockfd is the socket descriptor returned by socket()serv_addr is pointer to struct sockaddr that contains

    information ondestination IP address and portaddrlen is set to sizeof(struct sockaddr)returns -1 on errorAt times, you don't have to bind() when you are using

    connect()send() and recv() - Let's talk!The two functions are for communicating over stream

    sockets or connecteddatagram sockets.

    int send(int sockfd, const void *msg, int len, int flags);sockfd is the socket descriptor you want to send data to

    (returned bysocket() or got with accept())

    msg is a pointer to the data you want to sendlen is the length of that data in bytesset flags to 0 for nowsent() returns the number of bytes actually sent (may be

    less than the

    number you told it to send) or -1 on errorsend() and recv() - Let's talk!int recv(int sockfd, void *buf, int len, int flags);sockfd is the socket descriptor to read frombuf is the buffer to read the information len is the

    maximum length of the bufferset flags to 0 for nowrecv() returns the number of bytes actually read into the

    buffer or -1 onerror

    If recv() returns 0, the remote side has closed connection on you

    close() - Bye Bye!int close(int sockfd);Closes connection corresponding to the socket descriptor

    and frees the

  • 8/7/2019 B&D's Networking Lab

    5/14

    socket descriptorWill prevent any more sends and recvssocket() -- Get the file descriptorint socket(int domain, int type, int protocol);

    domain should be set to AF_INET

    type can be SOCK_STREAM or SOCK_DGRAMset protocol to 0 to have socket choose the correct

    protocol based on typesocket() returns a socket descriptor for use in later systemcalls or -1 on error.

    Q. WAP to add 2 nos. in server passed by client and thenprint the result on client.

    Server side

    #include#include#include#include#include

    int main(){int server_sockfd,client_sockfd;int server_len,client_len;

    struct sockaddr_un server_address;struct sockaddr_un client_address;unlink("server_socket");server_sockfd=socket(AF_UNIX,SOCK_STREAM,0);server_address.sun_family=AF_UNIX;strcpy(server_address.sun_path,"server_socket");server_len=sizeof(server_address);bind(server_sockfd, (struct sockaddr

    *)&server_address,server_len);listen(server_sockfd,5);

    while(1)

    { char ch; int n1,n2,result;

    system("clear");printf("Server Waiting...");client_len=sizeof(client_address);client_sockfd=accept(server_sockfd,(struct sockaddr

    *)&client_address,

  • 8/7/2019 B&D's Networking Lab

    6/14

    &client_len);read(client_sockfd,&n1,1);read(client_sockfd,&n2,1);

    /* read(client_sockfd,&ch,1);*/result = n1 + n2;

    write(client_sockfd,&result,1);close(client_sockfd);}

    }

    Output:

    $ cc addserver.c -o ads.out$ ./ads.out

    Client Side

    #include#include#include#include#include

    int main(){ int sockfd; int len; struct sockaddr_un address; int result,n1,n2; char ch;sockfd=socket(AF_UNIX,SOCK_STREAM,0);address.sun_family=AF_UNIX;strcpy(address.sun_path,"server_socket");len=sizeof(address);result=connect(sockfd, (struct sockaddr

    *)&address,len); if(result==-1)

    {perror("oops:client1");

  • 8/7/2019 B&D's Networking Lab

    7/14

    exit(1);}

    printf("\nEnter A Number:");scanf("%d",&n1);printf("\nEnter Another Number:");scanf("%d",&n2);fflush(stdin);

    /*printf("\nEnter Operator(+,-,*,/ etc.):");scanf("%c",&ch);*/write(sockfd,&n1,1);write(sockfd,&n2,1);

    /*write(sockfd,&ch,1);*/read(sockfd,&result,1);printf("\nResult, Addition of two numbers ::

    %d",result);close(sockfd);exit(0);

    }

    output:$ cc addclient.c -o adc.out$ ./adc.outEnter A number: 12

    Enter Another number:13Result , Addition of two numbers:: 25$

  • 8/7/2019 B&D's Networking Lab

    8/14

    Q. WAP in C (socket programming) to make a chatapplication in which two Host(server & client) can passmessage to each other.

    #include#include#include#include#include#include#includeint main()

    {int server_sd,server_len,client_len,b,a,client_sd;struct sockaddr_in server_addr,client_addr;char ipadd[]={"10.10.76.116"};server_len=sizeof(server_addr);int port=5499;char recvmessg[20],sendmessg[20];server_sd=socket(AF_INET,SOCK_STREAM,0);if(server_sd>=0)printf("socket created successfully\n");

    //server_len=sizeof(server_addr);server_addr.sin_family=AF_INET;server_addr.sin_port=htons(port);inet_aton(ipadd,(&server_addr.sin_addr));server_len=sizeof(server_addr);

  • 8/7/2019 B&D's Networking Lab

    9/14

    b=bind(server_sd,(struct sockaddr*)&server_addr,server_len);

    if(b>=0)printf("\nbinded successfully\n\n");listen(server_sd,5);client_len=sizeof(client_addr);a=accept(server_sd,(struct sockaddr

    *)&client_addr,&client_len); while(1){

    read(a,&recvmessg,20);printf("%s",recvmessg);

    printf("\nEnter message for reply: ");gets(sendmessg);write(a,&sendmessg,20);

    if(strcmp(recvmessg,"bye")==0){close(server_sd);exit(0);

    }}

    }

    Out put:$cc chat_serv.c o chts.out$./chts.outsocket created successfullybinded successfullyhiEnter message for reply: hello

    Wats upEnter message for reply: bye$

    Client Side:

    #include

  • 8/7/2019 B&D's Networking Lab

    10/14

    #include#include#include#include#include#include#include#include

    int main(){int client_sd,client_len;char ipadd[]={"10.10.76.116"};int port=5499;

    char recvmssg[20],sendmssg[20];struct sockaddr_in client_addr;client_sd=socket(AF_INET,SOCK_STREAM,0);if(client_sd>0)printf("socket created successfully");

    client_len=sizeof(client_addr);client_addr.sin_family=AF_INET;client_addr.sin_port=htons(port);inet_aton(ipadd,(&client_addr.sin_addr));

    connect(client_sd,(struct sockaddr*)&client_addr,client_len);while(1){

    printf("\nenter the msg : ");gets(sendmssg);write(client_sd,&sendmssg,20);read(client_sd,&recvmssg,20);puts(recvmssg);

    if(strcmp(recvmssg,"bye")==0){close(client_sd);exit(0);}

  • 8/7/2019 B&D's Networking Lab

    11/14

    }}

    Output:$ cc chat_cl.c o chtc.out$ ./chtc.outsocket created successfullyenter the msg : hihelloenter the msg : wats up$

    Q. WAP in C (Socket Programming) in Which Client machine ask and

    get the date and time from a server machine.

    Server Side:

    #include#include#include#include#include#include#include#include#include#include#include#include#includestruct sockaddr_in serv_addr,cli_addr;int sockfd,cli_addr_len,connfd,bb;

    char buff[1024];time_t ticks;char serv_ip[]={"10.10.76.116"};unsignedshort serv_port=5045;int main(){bzero(&serv_addr,sizeof(serv_addr));

  • 8/7/2019 B&D's Networking Lab

    12/14

    sockfd = socket(AF_INET,SOCK_STREAM,0);serv_addr.sin_family=AF_INET;serv_addr.sin_port=htons(serv_port);inet_aton(serv_ip,(&serv_addr.sin_addr));bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));listen(sockfd,5);cli_addr_len=sizeof(cli_addr);while(1){connfd= accept(sockfd,(structsockaddr*)&cli_addr,&cli_addr_len);ticks=time(NULL);snprintf(buff,(int)

    (sizeof(buff)),"%.24s\r\n",ctime(&ticks));write(connfd,buff,(int)(sizeof(buff)));close(connfd);}}

    Output:$ cc date_serv.c o dts.out$ ./ dts.out

    Client Side:

    #include#include#include#include#include#include#include

  • 8/7/2019 B&D's Networking Lab

    13/14

    #include#include#include#include

    struct sockaddr_in serv_addr;int client_sd,r,w;int serv_port = 5045;char server_ip[]={"10.10.76.116"};char rcvmsg[1024];int main(int args,char **argv){bzero(&serv_addr,sizeof(serv_addr));if((client_sd=socket(AF_INET,SOCK_STREAM,0))

  • 8/7/2019 B&D's Networking Lab

    14/14

    }

    Output:$ cc date_clt.c o dtc.out$ ./dtc.outTCP ECHO CLINTrecieved msg :Sat Apr 16 21:39:30 2011$