Program 6
To write a program to a created file from the server to the client.
File transfer TCP Algorithm
Server side Filer Transfer TCP Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure for the socket.
STEP 3: Create a socket using socket functions
STEP 4: The socket is binded at the specified port.
STEP 5: Using the object the port and address are declared.
STEP 6: After the binding is executed the file is specified.
STEP 7: Then the file is specified.
STEP 8: Execute the client program.
Client File Transfer TCP programming
Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure.
STEP 3: Socket is created and connects function is executed.
STEP 4: If the connection is successful then server sends the message.
STEP 5: The file name that is to be transferred is specified in the client side.
STEP 6: The contents of the file is verified from the server side.
STEP 7: Stop the program
Server Side source code programming
#include<string.h>
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<net/if_arp.h>
.int main()
{
int sd,b,cd;
struct fname[50],op[1000];
struct sockaddr_in caddr,saddr;
FILE *fp;
socklen_t clen=sizeof(caddr);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd!=-1)
printf(“socket is created”);
else
printf(“socket is not created”);
saddr.sin_family=AF_INET;
saddr.sin_port=htons(2500);
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
b=bind(sd,(struct sockaddr*)&saddr,sizeof(saddr));
if(b==0)
printf(“binded successfully”);
else
printf(“binding failed’);
listen(sd,5);
cd=accept(sd,(struct sockaddr*)&caddr,&clen);
recv(cd,fname,sizeof(fnmae),0);
fp=open(fname,”w”);
fwrite(op,strlen(op),1,fp);
printf(“the file has been transferred”);
close(fd);
close(cd);
fclose(fp);
return 0;
}
Client Side Program:
#include<string.h>
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>
#include<net/if_arp.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb,h>
int main()
{
int sd,c,s;
char fname[50],sip[25],op[1000];
struct sockaddr_in caddr;
struct hostent *he;
FILE *fp;
printf(‘enter the server ip address”);
scanf(“%s”,sip);
he=gethostbyname(sip0;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd!=1)
printf(“socket created”);
else
printf(“socket is not created’);
caddr.sin_family=AF_INET;
caddr.sin_port=htons(2500);
caddr.sin_addr=*((struct in_addr*)he->h_addr);
c=connect(sd,(struct sockaddr*)&caddr,sizeof(caddr));
if(c==0)
printf(“connected to server”);
else
printf(“connection failed”);
printf(“enter the file name’);
scanf(“%s”,fname);
send(sd,fname,sizeof(fname),0);
fp=fopen(fname,”r”);
fopen(op,1000,1,fp);
send(sd,op,sizeof(op),0);
fclose(fp);
close(sd);
return 0;
}
INPUT OUTPUT CLIENT CS1305 Network Lab
Enter the server ip address 127.0.0.1
Socket created
Connected to the server
Enter the file name cli.txt
cli.txt
Network programming lab
B.tech I.T
Third year
06 sem
SERVER CS1305 Network Lab
Socket is created
Binded successfully
Enter the file name ser.txt
The file has been transferred
Ser.txt
Network programming lab
Program 7
Design a TCP concurrent server to convert a given text into upper case using
multiplexing system call “select”
1. Create TCP i.e Listening socket
2. Create a UDP socket
3. Bind both socket to server address.
4. Initialize a descriptor set for select and calculate maximum of 2
descriptor for which we will wait
5. Call select and get the ready descriptor(TCP or UDP)
6. Handle new connection if ready descriptor is of TCP OR receive data
gram if ready descriptor is of UDP
Server.c
// Server program
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define PORT 5000
#define MAXLINE 1024
int max(int x, int y)
if (x > y)
return x;
else
return y;
int main()
int listenfd, connfd, udpfd, nready, maxfdp1;
char buffer[MAXLINE];
pid_t childpid;
fd_set rset;
ssize_t n;
socklen_t len;
const int on = 1;
struct sockaddr_in cliaddr, servaddr;
char* message = "Hello Client";
void sig_chld(int);
/* create listening TCP socket */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// binding server addr structure to listenfd
bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
listen(listenfd, 10);
/* create UDP socket */
udpfd = socket(AF_INET, SOCK_DGRAM, 0);
// binding server addr structure to udp sockfd
bind(udpfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
// clear the descriptor set
FD_ZERO(&rset);
// get maxfd
maxfdp1 = max(listenfd, udpfd) + 1;
for (;;) {
// set listenfd and udpfd in readset
FD_SET(listenfd, &rset);
FD_SET(udpfd, &rset);
// select the ready descriptor
nready = select(maxfdp1, &rset, NULL, NULL, NULL);
// if tcp socket is readable then handle
// it by accepting the connection
if (FD_ISSET(listenfd, &rset)) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &len);
if ((childpid = fork()) == 0) {
close(listenfd);
bzero(buffer, sizeof(buffer));
printf("Message From TCP client: ");
read(connfd, buffer, sizeof(buffer));
puts(buffer);
write(connfd, (const char*)message, sizeof(buffer));
close(connfd);
exit(0);
}
close(connfd);
}
// if udp socket is readable receive the message.
if (FD_ISSET(udpfd, &rset)) {
len = sizeof(cliaddr);
bzero(buffer, sizeof(buffer));
printf("\nMessage from UDP client: ");
n = recvfrom(udpfd, buffer, sizeof(buffer), 0,
(struct sockaddr*)&cliaddr, &len);
puts(buffer);
sendto(udpfd, (const char*)message, sizeof(buffer), 0,
(struct sockaddr*)&cliaddr, sizeof(cliaddr));
}
}
}
TCP_Client.c
// TCP Client program
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
#define MAXLINE 1024
int main()
int sockfd;
char buffer[MAXLINE];
char* message = "Hello Server";
struct sockaddr_in servaddr;
int n, len;
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sockfd, (struct sockaddr*)&servaddr,
sizeof(servaddr)) < 0) {
printf("\n Error : Connect Failed \n");
}
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, "Hello Server");
write(sockfd, buffer, sizeof(buffer));
printf("Message from server: ");
read(sockfd, buffer, sizeof(buffer));
puts(buffer);
close(sockfd);
}
UDP_client.c
// UDP client program
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#define PORT 5000
#define MAXLINE 1024
int main()
{
int sockfd;
char buffer[MAXLINE];
char* message = "Hello Server";
struct sockaddr_in servaddr;
int n, len;
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("socket creation failed");
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr(INADDR_ANY);
// send hello message to server
sendto(sockfd, (const char*)message, strlen(message),
0, (const struct sockaddr*)&servaddr,
sizeof(servaddr));
// receive server's response
printf("Message from server: ");
n = recvfrom(sockfd, (char*)buffer, MAXLINE,
0, (struct sockaddr*)&servaddr,
&len);
puts(buffer);
close(sockfd);
return 0;