NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#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 PORT 4950 /*TCP port for communication*/
#define BUFFER 1024 /*Buffer size used*/

void send_and_recv(int i, int fd)
{
char message[BUFFER]; /*buffer to store the sending messages*/
char reply[BUFFER]; /*buffer to store the messages from other clients*/
int no_bytes; /*number of byte received*/

if (i == 0){ /*indicates the flag which will be set whenever there is data from the other client */
fgets(message, BUFFER, stdin); /*input message*/
if (strcmp(message , "byen") == 0) {/*quit immediately if you client types bye*/
exit(0);
}else
send(fd, message, strlen(message), 0); /*sending message to the server*/
}else {
no_bytes = recv(fd, reply, BUFFER, 0); /*if flag is not set receive the message from other clients through server*/
reply[no_bytes] = '';/*indicating end of message*/
printf("%sn" , reply);/*prints the message*/
fflush(stdout);
}
}


void connect_request(int *fd, struct sockaddr_in *server_addr)
{
if ((*fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {/*creating socket*/
printf("Error in creating Socket");
exit(1);
}
server_addr->sin_family = AF_INET; /*Intiatializing environment for sockaddr structure*/
server_addr->sin_port = htons(PORT);
server_addr->sin_addr.s_addr = inet_addr("127.0.0.1");
memset(server_addr->sin_zero,'', sizeof server_addr->sin_zero);

if(connect(*fd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) == -1) {/*connecting to the listening server on the port*/
printf("Error while connectingn");
exit(1);
}
}

int main()
{
int fd, maximumfd, i; /*listening socket file descriptor and maximum file descriptor number*/
struct sockaddr_in server_addr; /*sockaddr structure declaration*/
fd_set master; /*master file descriptor set*/
fd_set read_fds; /*list of file descriptor for select*/

connect_request(&fd, &server_addr); /*connection establishment with server*/
FD_ZERO(&master); /*clearing master set*/
FD_ZERO(&read_fds); /*clearing temporary file descriptorr */
FD_SET(0, &master); /*Adding 0 to the master file descriptor*/
FD_SET(fd, &master);/*Adding socket file descriptor to master file descriptor*/
maximumfd = fd; /*Keeping track of maximum file descriptor*/

for(;;){
read_fds = master; /*copy*/
FD_SET(fd,&read_fds);/*Adding socket file descriptor*/
if(select(maximumfd+1, &read_fds, NULL, NULL, NULL) == -1){/*To see the number of file descriptor ready for read*/
printf("None of them are ready");
exit(4);
}

for(i=0; i <= maximumfd; i++ ) /*To keep track of everyone*/
if(FD_ISSET(i, &read_fds))
send_and_recv(i, fd);/*sending data to everyone*/
}
close(fd);
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.