0% found this document useful (0 votes)
12 views3 pages

Soln 2018 - Q1 - TCP Client and Server Program

The document contains a TCP client-server program written in C, demonstrating socket programming. The server listens on port 8080, accepts a connection, converts received messages from uppercase to lowercase, and sends the modified message back to the client. The client connects to the server, sends a message, and prints the response received from the server.

Uploaded by

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

Soln 2018 - Q1 - TCP Client and Server Program

The document contains a TCP client-server program written in C, demonstrating socket programming. The server listens on port 8080, accepts a connection, converts received messages from uppercase to lowercase, and sends the modified message back to the client. The client connects to the server, sends a message, and prints the response received from the server.

Uploaded by

ShresthA BaburaM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

$sagarMalla CITE 2016 Batch 19171R

om
t.c
s po
Solution of Q.No : 1 TCP Client Server Program [2018] Network Programming (PU)
// Server side program to demonstrate Socket programming
og
//Compiled By SAGAR MALLA, BIT 7th 2016 Batch, CITE
#include <unistd.h>
#include <stdio.h>
bl

#include <sys/socket.h>
#include <stdlib.h>
s.

#include <netinet/in.h>
#include <string.h>
#define PORT 8080
er

int main(int argc, char const *argv[])


{
int server_fd, new_socket, valread,i;
ap

struct sockaddr_in address;


int opt = 1;
int addrlen = sizeof(address);
t-p

char buffer[100] = {0};


//char *hello = "Hello from server";

// Creating socket file descriptor


bi

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)


{
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080


if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,&opt, sizeof(opt)))
{
https://bit-papers.blogspot.com/ ©17171R
$sagarMalla CITE 2016 Batch 19171R

perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

// Forcefully attaching socket to the port 8080


if (bind(server_fd, (struct sockaddr *)&address,

om
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}

t.c
if (listen(server_fd, 3) < 0)
{
perror("listen");

po
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
s
og
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("Message From Client :: %s\n",buffer );
bl

//UPPERCASE to lowercase logic


s.

for(i=0;i<=strlen(buffer);i++)
{
if(buffer[i]>=65 && buffer[i]<=92)
er

{
buffer[i]=buffer[i]+32;
}
ap

}
send(new_socket , buffer , strlen(buffer) , 0 );
printf("purbanchal sent to Client\n");
t-p

return 0;
}
bi

https://bit-papers.blogspot.com/ ©17171R
$sagarMalla CITE 2016 Batch 19171R

// Client side program to demonstrate Socket programming


//Compiled By SAGAR MALLA, BIT 7th 2016 Batch, CITE
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080

om
int main(int argc, char const *argv[])
{
int sock = 0, valread;

t.c
struct sockaddr_in serv_addr;
char *hello = "PURBANCHAL";
char buffer[100] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)

po
{
printf("\n Socket creation error \n");
return -1;
}
s
og
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form


bl

if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)


{
s.

printf("\nInvalid address/ Address not supported \n");


return -1;
}
er

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)


ap

{
printf("\nConnection Failed \n");
return -1;
}
t-p

send(sock , hello , strlen(hello) , 0 );


printf("PURBANCHAL sent to Server\n");
valread = read( sock , buffer, 1024);
printf("Message from Server :: %s\n",buffer );
bi

return 0;
}

https://bit-papers.blogspot.com/ ©17171R

You might also like