NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


#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 /*port we are listening on*/
#define BUFFER 1024 /*buffer size used*/

void send_to_everyone(int j, int i, int fd, int no_bytes, char *reply, fd_set *master)/*sending received data to everyone*/
{
if (FD_ISSET(j, master)){ /*sends message to everyone */
if (j != fd && j != i) { /*except the server and the client which sent data*/
if (send(j, reply, no_bytes, 0) == -1) {/*sending message to rest all clients*/
printf("error in the clientn");
}
}
}
}

void send_and_recv(int i, fd_set *master, int fd, int maximumfd)
{
int no_bytes, j;
char reply[BUFFER], buf[BUFFER]; /*buffer for client data*/

if ((no_bytes = recv(i, reply, BUFFER, 0)) <= 0) { /*number of bytes received*/
if (no_bytes == 0) {
printf("socket %d closed its connectionn", i);
}else {
printf("there is some error in the client");
}
close(i);
FD_CLR(i, master);
}else {

for(j = 0; j <= maximumfd; j++){ /*if there a data from any client*/
send_to_everyone(j, i, fd, no_bytes, reply, master );/*send it to everyone*/
}
}
}

void connection_accept(fd_set *master, int *maximumfd, int fd, struct sockaddr_in *client_addr)
{
socklen_t addrlen; /*handles upcoming connection*/
int newconn; /*newly accepted socket file descriptor*/

addrlen = sizeof(struct sockaddr_in);
if((newconn = accept(fd, (struct sockaddr *)client_addr, &addrlen)) == -1) { /*accepting the connection from client*/
printf("accept");
exit(1);
}else {
FD_SET(newconn, master);
if(newconn > *maximumfd){ /*keeping track of maximum*/
*maximumfd = newconn;
}
printf("n");
printf("New connection from Clientn");
printf("client address:%sn",inet_ntoa(client_addr->sin_addr));
printf("Client port:%dn",ntohs(client_addr->sin_port));
}
}

void connect_request(int *fd, struct sockaddr_in *server_addr)
{
int x = 1; /*for setsockopt*/

if ((*fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /*creating socket file descriptor*/
printf("socket not opened");
exit(1);
}

server_addr->sin_family = AF_INET; /*Initialize the environment for sockaddr structure*/
server_addr->sin_port = htons(PORT);
server_addr->sin_addr.s_addr = INADDR_ANY;
memset(server_addr->sin_zero, '', sizeof server_addr->sin_zero);

if (setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(int)) == -1) { /*To reuse the same address*/
printf("setsockopt not ok");
exit(1);
}

if (bind(*fd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) == -1) { /*Binding socket to the port*/
printf("Unable to bind");
exit(1);
}
if (listen(*fd, 10) == -1) { /*Server listening at defined port*/
printf("listen error");
exit(1);
}
printf("TCP server is waiting for client");
fflush(stdout);
}
int main()
{
fd_set master; /*master file descriptor set*/
fd_set read_fds; /*list of file descriptors for select*/
int maximumfd, i; /*maximum file descriptor number*/
int fd= 0; /*listening socket descriptor*/
struct sockaddr_in server_addr, client_addr; /*server and client address*/

FD_ZERO(&master); /*clears master set*/
FD_ZERO(&read_fds); /*clears select file descriptor*/
connect_request(&fd, &server_addr); /*connection establishment*/
FD_SET(fd, &master); /*Adding socket file descriptor to the master set*/

maximumfd = fd; /*keeping track of maximum file descriptor*/
for(;;){
read_fds = master;
if(select(maximumfd+1, &read_fds, NULL, NULL, NULL) == -1){ /*To see how many file descriptors are ready for read*/
printf("None of them are readyn");
exit(4);
}

for (i = 0; i <= maximumfd; i++){ /*Run through the existing connection*/
if (FD_ISSET(i, &read_fds)){ /*Looking for data to be read*/
if (i == fd)
connection_accept(&master, &maximumfd, fd, &client_addr); /*accept the connection from requested client*/
else
send_and_recv(i, &master, fd, maximumfd); /*broadcast the message recived*/
}
}
}
return 0;
}


     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.