0% found this document useful (0 votes)
7 views2 pages

17bce1324 Ex3

The document contains C code for a simple server and client using sockets. The server listens on port 7891 and sends a greeting message to the client upon connection. The client connects to the server, receives the message, and prints it to the console.

Uploaded by

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

17bce1324 Ex3

The document contains C code for a simple server and client using sockets. The server listens on port 7891 and sends a greeting message to the client upon connection. The client connects to the server, receives the message, and prints it to the console.

Uploaded by

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

Ajay Shankar.

R
17BCE1324
EX3

SERVER

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in address;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_port = htons(7891);
address.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(welcomeSocket, (struct sockaddr *) &address, sizeof(address));
if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(buffer,"Hi\n");
send(newSocket,buffer,13,0);
return 0;
}

CLIENT

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int clisoc;
char buffer[1024];
struct sockaddr_in address;
socklen_t addr_size;
clisoc=socket(PF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_port = htons(7891);
address.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_size = sizeof address;
connect(clisoc, (struct sockaddr *) &address, addr_size);
recv(clisoc, buffer, 1024, 0);
printf("Data received: %s",buffer);
return 0;
}

OUTPUT:

You might also like