0% found this document useful (0 votes)
3 views9 pages

Program 5

The document provides a basic implementation of a TCP client-server program in C, where the client sends a string to the server. The server receives the string, converts its case (lowercase to uppercase and vice versa), and sends it back to the client. The program continues to run in a loop, allowing for continuous communication between the client and server.

Uploaded by

rajprithiv944
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Program 5

The document provides a basic implementation of a TCP client-server program in C, where the client sends a string to the server. The server receives the string, converts its case (lowercase to uppercase and vice versa), and sends it back to the client. The program continues to run in a loop, allowing for continuous communication between the client and server.

Uploaded by

rajprithiv944
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

basic client and server program using tcp (client

sending string and server receiving data from


client then server converting data to opposite
case after that send back to client continously)

header.h
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
// server.c
#include"header.h"
void client_info(struct sockaddr_in client_info)
{
printf("client port address: %d\
n",client_info.sin_port);
printf("client ip address: %s\
n",inet_ntoa(client_info.sin_addr));

}
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("usage: ./server server_port
server_ip_address\n");
return 0;
}
int sfd,len,nsfd,i;
struct sockaddr_in server_id,client_id;
/********** socket () ***************/
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
perror("Socket");
return 0;
}
perror("Socket");
/*********** bind() ***************/
server_id.sin_family=AF_INET;
server_id.sin_port=htons(atoi(argv[1]));
server_id.sin_addr.s_addr=inet_addr(argv[2]);
len=sizeof(server_id);
if(bind(sfd,(struct sockaddr*)
&server_id,len)<0)
{
perror("Bind");
return 0;
}
perror("Bind");

/************* listen **************/


if(listen(sfd,5)<0)
{
perror("Listen");
return 0;
}
perror("Listen");
/************ accept ****************/
printf("before accept client request...(waiting
for client)\n");
nsfd=accept(sfd,(struct
sockaddr*)&client_id,&len);
if(nsfd<0)
{
perror("Accept");
return 0;
}
printf("client request Accepted...\n");
client_info(client_id);
char s[20];
while(1)
{
read(nsfd,s,sizeof(s)); // reading data from
client
printf("client data: %s\n",s);
for(i=0;s[i];i++)
{
if(s[i]>='a' &&s[i]<='z')
s[i]=s[i]-32;
else if(s[i]>='A' && s[i]<='Z')
s[i]=s[i]+32;
}
write(nsfd,s,strlen(s)+1);
}
close(sfd);
close(nsfd);

}
//client.c

#include"header.h"
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("usage: ./client server_port
server_ip_address\n");
return 0;
}
int sfd,len;
struct sockaddr_in server_id;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
perror("Socket");
return 0;
}
perror("Socket");

server_id.sin_family=AF_INET;
server_id.sin_port=htons(atoi(argv[1]));
server_id.sin_addr.s_addr=inet_addr(argv[2]);
len=sizeof(server_id);
if(connect(sfd,(struct
sockaddr*)&server_id,len)<0)
{
perror("connect");
return 0;
}
perror("connect");
char s[20];
while(1)
{
printf("enter string from user\n");
scanf("%s",s);
write(sfd,s,strlen(s)+1); //writing data to
server
read(sfd,s,sizeof(s)); //reading data from
server
printf("Updated data: %s\n",s);
}
close(sfd);

You might also like