Uploaded by Kiran Fatima Fast NU

DCN socket programmi ng

advertisement
SUBMITTED TO
Sir Zeeshan Asif
SUBMITTED BY
Kiran Fatima
18L1330
Sec:B
Data Communications and Networks Lab
LAB REPORT#12
Experiment#13
Introduction to Socket Programming
OBJECTIVES
 To understand how socket programming works .
 To learn how client server model can be implemented through socket
programming and test the connection using any connection message.
 To understand the provided code for client and server
INTRODUCTION
First of all we need to understand what a socket means and how it helps us to
implement a client server model .
 A socket is defined as an endpoint for communication. A pair of processes
communicating over a network employs a pair of sockets
 One for each process. A socket is identified by an IP address concatenated
with a port number.
 In general, sockets use a client–server architecture. The server waits for
incoming client requests by listening to a specified port.
 Once a request is received, the server accepts a connection from the client
socket to complete the connection.
The communication over the network in TCP/IP model takes place in form of a
client server architecture. ie, the client begins the communication and server
follows up and a connection is established.
Functions used in Socket Programming:







socket() Endpoint for communication
bind() Assign a unique telephone number
listen() Wait for a caller
connect() Dial a number
accept() Receive a call
send(), recv() Talk
close() Hang up
DESIGN
In task#1 we had to explain the codes given in manual for client and server and
test their connection in ubuntu .c files .
The code is explained as:
CLIENT CODE EXPLANATION
 Here we are including the required libraries for making a socket between a
client and a server.
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include <unistd.h>
int main()
{
int sd;
 Sd is just an integer type variable
char msg[100];
 Initializing a character array of 100 elements with name msg.
char* msg1="Also Connected";
 Defining a string and storing it where msg1 points as a pointer
struct sockaddr_in server_addr;
 Creating a struct variable named server_addr
server_addr.sin_family=AF_INET;
 Giving information of server ,Defining a family at server addr from internet
domain
server_addr.sin_port=htons(4999);
 Giving the port number of server at that address
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
 Giving the IP address of server
sd=socket(AF_INET,SOCK_STREAM,0);




Creating a socket with3 parameters
AF INET is the int type domain we are applying at this socket.
Sock stream is type (tcp)
0 is telling the tcp protocol
connect(sd,(struct sockaddr*)
 At clients end connect function connects socket to network of server.
&server_addr,sizeof(struct sockaddr));
 Sizeof function is taking the size of sockaddr ..
recv(sd,msg,100,0);
 Storing the received message sent by server in msg array
send(sd, msg1,100,0);
 Sending the message in msg1 string to server.
printf("%s\n",msg);
 Printing the message sent by server
return 0;
}
SERVER CODE EXPLANATION
 Here we are including the required libraries for making a socket between a
client and a server.
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include <unistd.h>
int main()
{
int sd;
char* msg="connected";
 Defining a string and storing it where msg points as a pointer
char msg1[100];
 A charcter array declaration of 100 elements.
struct sockaddr_in my_addr, client_addr;
my_addr.sin_family=AF_INET;
 Defining a family at my addr from internet domain.initializing a variable
client addr also of type struct.
my_addr.sin_port=htons(4999);
 Giving the port number to this variable for server.
my_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
 Setting the IP address of server
sd=socket(AF_INET,SOCK_STREAM,0);




Creating a socket with3 parameters
AF INET is the int type domain we are applying at this socket.
Sock stream is type (tcp)
0 is telling the tcp protocol
bind(sd,(struct sockaddr *) &my_addr,sizeof(struct
sockaddr_in));
 Sizeof is taking the size of sockaddr_in and the struct variable and bind it
with the server side.basically binding the respective ID and port number to
the socket.
listen(sd,5);
 Wait for the request from client ,5 is written, it describes the number of
connections to listen however we need 1 yet but 5 is more than 1 so it is
okay here.
int i=0;
while(i<2)
 To check the client server communication 2 times.
{
int size=sizeof(struct sockaddr);
int new_sd=accept(sd,(struct sockaddr*)&client_addr,&size);
 We have taken size of sockaddr and assigned it to size for giving acceptable
client size in the function accept and store result in avariable new_sd.
send(new_sd, msg,100,0);
 Sending the message in msg string to client.
recv(new_sd,msg1,100,0);
 Storing the received message sent by client in msg1 array
printf("%s\n",msg1);
 Printing the message sent by client
++i;
}
return 0;
}
In task# 2 we had to implement the same network but with an id and password
which must be required to the client to be connected to server.I am attaching its
code since it will elongate the report unneccessararily and it has already been
provided in lab tasks:
Some important changes in client code were:
send(a, usernameAndPassword,100,0);
if(usernameAndPassword==entered)
{
printf("%s\n",msg);
}
Some important changes in server code were:
char* usernameAndPassword="Kiran18L1330";
char entered[100];
char* msg="connected";
char msg1[100];
int new_a=accept(a,(struct sockaddr*)&client_addr1,&size1);
send(new_sd, msg,100,0);
recv(new_sd,msg1,100,0);
send(new_sd, usernameAndPassword,100,0);
recv(new_sd,entered,100,0);
if(usernameAndPassword==entered)
{
printf("%s\n",msg1)
SCREENSHOTS
Creating gcc files of both server and client with names clientT2,serverT2
Connecting
ISSUES
We need to create another socket if we want to include the username and
password too. One socket can not handle it all.
CONCLUSION
The communication over the network in TCP/IP model takes place in form of a
client server architecture. ie, the client begins the communication and server
follows up and a connection is established.Socket programming enables us to
observe the working and protocols of such programming ,virtually and test if our
algorithm is practically implementable for a client server model.
APPLICATIONS
Sockets are useful for both stand-alone and network applications.
 Sockets allow you to exchange information between processes on the same
machine or across a network,
 Distribute work to the most efficient machine
 They easily allow access to centralized data.
POSTLAB
We can write the server and client codes for UDP too by creating Udp socket
through command SOCK_DGRAM .
SERVER CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT
8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "listening";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
len);
printf("connected.\n");
return 0;
}
CLIENT CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT
8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "connecting";
struct sockaddr_in
servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Also connected.\n");
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}
Download