0% found this document useful (0 votes)
69 views15 pages

Implementation of Client Side and Server Side Program With Command Line Arguments For Connection Less Socket Using C

The document provides implementation of client-server programs for connection-oriented and connectionless sockets in C. It includes code for UDP and TCP client-server programs, along with code for a multi-client TCP server handling multiple connections.

Uploaded by

achintasat
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)
69 views15 pages

Implementation of Client Side and Server Side Program With Command Line Arguments For Connection Less Socket Using C

The document provides implementation of client-server programs for connection-oriented and connectionless sockets in C. It includes code for UDP and TCP client-server programs, along with code for a multi-client TCP server handling multiple connections.

Uploaded by

achintasat
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
You are on page 1/ 15

/*

Implementation of client side and server side program with


command line arguments for connection less socket using C
*/
Client Side:

// udp client driver program


#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>

#define PORT 5000


#define MAXLINE 1000

// Driver code
int main()
{
char buffer[100];
char *message = "Hello Server";
int sockfd, n;
struct sockaddr_in servaddr;

// clear servaddr
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;

// create datagram socket


sockfd = socket(AF_INET, SOCK_DGRAM, 0);

// connect to server
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("\n Error : Connect Failed \n");
exit(0);
}
// request to send datagram
// no need to specify server address in sendto
// connect stores the peers IP and port
sendto(sockfd, message, MAXLINE, 0, (struct sockaddr*)NULL, sizeof(servaddr));

// waiting for response


recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)NULL, NULL);
puts(buffer);

// close the descriptor


close(sockfd);
}

Server side:

// server program for udp connection


#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#define PORT 5000
#define MAXLINE 1000

// Driver code
int main()
{
char buffer[100];
char *message = "Hello Client";
int listenfd, len;
struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr, sizeof(servaddr));

// Create a UDP Socket


listenfd = socket(AF_INET, SOCK_DGRAM, 0);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
servaddr.sin_family = AF_INET;

// bind server address to socket descriptor


bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

//receive the datagram


len = sizeof(cliaddr);
int n = recvfrom(listenfd, buffer, sizeof(buffer),
0, (struct sockaddr*)&cliaddr,&len); //receive message from server
buffer[n] = '\0';
puts(buffer);

// send the response


sendto(listenfd, message, MAXLINE, 0,
(struct sockaddr*)&cliaddr, sizeof(cliaddr));
}
Implementation of the client side and server side program for
the connection oriented socket using C to the students. Set of
two program of TCP/IP socket program
Client side:

#include <arpa/inet.h> // inet_addr()


#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // bzero()
#include <sys/socket.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;

// socket create and verification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
!= 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");

// function for chat


func(sockfd);

// close the socket


close(sockfd);
}

Server Side:

#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr

// Function designed for chat between client and server.


void func(int connfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);

// read the message from client and copy it in buffer


read(connfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;

// and send that buffer to client


write(connfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}

// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;

// socket create and verification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

// Binding newly created socket to given IP and verification


if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");

// Now server is ready to listen and verification


if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);

// Accept the data packet from client and verification


connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
else
printf("server accept the client...\n");

// Function for chatting between client and server


func(connfd);

// After chatting close the socket


close(sockfd);
}
Implementation of the multi-client and single server program
for the connection oriented socket programming using C. Set
of two programs of TCP/IP socket programs listed below.

Client side:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>

#define BUFSIZE 1024

void send_recv(int i, int sockfd)


{
char send_buf[BUFSIZE];
char recv_buf[BUFSIZE];
int nbyte_recvd;

if (i == 0){
fgets(send_buf, BUFSIZE, stdin);
if (strcmp(send_buf , "quit\n") == 0) {
exit(0);
}else
send(sockfd, send_buf, strlen(send_buf), 0);
}else {
nbyte_recvd = recv(sockfd, recv_buf, BUFSIZE, 0);
recv_buf[nbyte_recvd] = 0;
printf("%s\n" , recv_buf);
fflush(stdout);
}
}

void connect_request(int *sockfd, struct sockaddr_in *server_addr)


{
if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(4950);
server_addr->sin_addr.s_addr = inet_addr("127.0.0.1");
memset(server_addr->sin_zero, 0, sizeof server_addr->sin_zero);

if(connect(*sockfd, (struct sockaddr *)server_addr, sizeof(struct


sockaddr)) == -1) {
perror("connect");
exit(1);
}
}

int main()
{
int sockfd, fdmax, i;
struct sockaddr_in server_addr;
fd_set master;
fd_set read_fds;

connect_request(&sockfd, &server_addr);
FD_ZERO(&master);
FD_ZERO(&read_fds);
FD_SET(0, &master);
FD_SET(sockfd, &master);
fdmax = sockfd;

while(1){
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
perror("select");
exit(4);
}

for(i=0; i <= fdmax; i++ )


if(FD_ISSET(i, &read_fds))
send_recv(i, sockfd);
}
printf("client-quited\n");
close(sockfd);
return 0;
}

Server side:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define PORT 4950


#define BUFSIZE 1024

void send_to_all(int j, int i, int sockfd, int nbytes_recvd, char *recv_buf,


fd_set *master)
{
if (FD_ISSET(j, master)){
if (j != sockfd && j != i) {
if (send(j, recv_buf, nbytes_recvd, 0) == -1) {
perror("send");
}
}
}
}

void send_recv(int i, fd_set *master, int sockfd, int fdmax)


{
int nbytes_recvd, j;
char recv_buf[BUFSIZE], buf[BUFSIZE];

if ((nbytes_recvd = recv(i, recv_buf, BUFSIZE, 0)) <= 0) {


if (nbytes_recvd == 0) {
printf("socket %d hung up\n", i);
}else {
perror("recv");
}
close(i);
FD_CLR(i, master);
}else {
// printf("%s\n", recv_buf);
for(j = 0; j <= fdmax; j++){
send_to_all(j, i, sockfd, nbytes_recvd, recv_buf,
master );
}
}
}

void connection_accept(fd_set *master, int *fdmax, int sockfd, struct


sockaddr_in *client_addr)
{
socklen_t addrlen;
int newsockfd;

addrlen = sizeof(struct sockaddr_in);


if((newsockfd = accept(sockfd, (struct sockaddr *)client_addr,
&addrlen)) == -1) {
perror("accept");
exit(1);
}else {
FD_SET(newsockfd, master);
if(newsockfd > *fdmax){
*fdmax = newsockfd;
}
printf("new connection from %s on port %d \
n",inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
}
}

void connect_request(int *sockfd, struct sockaddr_in *my_addr)


{
int yes = 1;

if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {


perror("Socket");
exit(1);
}

my_addr->sin_family = AF_INET;
my_addr->sin_port = htons(4950);
my_addr->sin_addr.s_addr = INADDR_ANY;
memset(my_addr->sin_zero, 0, sizeof (my_addr->sin_zero));

if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,


sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}

if (bind(*sockfd, (struct sockaddr *)my_addr, sizeof(struct


sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
if (listen(*sockfd, 10) == -1) {
perror("listen");
exit(1);
}
printf("\nTCPServer Waiting for client on port 4950\n");
fflush(stdout);
}
int main()
{
fd_set master;
fd_set read_fds;
int fdmax, i;
int sockfd= 0;
struct sockaddr_in my_addr, client_addr;

FD_ZERO(&master);
FD_ZERO(&read_fds);
connect_request(&sockfd, &my_addr);
FD_SET(sockfd, &master);

fdmax = sockfd;
while(1){
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
perror("select");
exit(4);
}

for (i = 0; i <= fdmax; i++){


if (FD_ISSET(i, &read_fds)){
if (i == sockfd)
connection_accept(&master, &fdmax,
sockfd, &client_addr);
else
send_recv(i, &master, sockfd, fdmax);
}
}
}
return 0;
}

Take user input to send a message from client to server

Client Side:
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#define PORT 5000
#define MAXLINE 1000
int main()
{ char buffer[100];
char message[MAXLINE];
printf("Enter message");
fgets(message,MAXLINE,stdin);
int sockfd,n;
struct sockaddr_in servaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
servaddr.sin_family=AF_INET;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
{printf("\n Error");
exit(0);}
sendto(sockfd,message,MAXLINE,0,(struct
sockaddr*)NULL,sizeof(servaddr));
close(sockfd);
}

Server side:
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 5000
#define MAXLINE 1000

int main()
{
char buffer[100];
int listenfd, len;
struct sockaddr_in servaddr,cliaddr;
bzero(&servaddr, sizeof(servaddr));

listenfd=socket(AF_INET, SOCK_DGRAM,0);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
servaddr.sin_family=AF_INET;

bind(listenfd,(struct sockaddr*)&servaddr, sizeof(servaddr));


len=sizeof(cliaddr);
int n=recvfrom(listenfd, buffer, sizeof(buffer),0,(struct sockaddr*)&cliaddr,&len);
buffer[n]='\0';
puts(buffer);
}

You might also like