NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include<sys/times.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int serverfd, new_socket, valread;
struct sockaddr_in server,client;;
int addrlen = sizeof(server);
int opt=1;
// Creating socket file descriptor
if ((serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR,&opt,sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( PORT );

// Forcefully attaching socket to the port 8080
if (bind(serverfd, (struct sockaddr *)&server,
sizeof(server))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(serverfd, 10) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}

fd_set readfd,copyfd;
FD_ZERO(&readfd);
FD_SET(serverfd,&readfd);
struct timeval tv;
int maxfd=serverfd+1;
int clientfd[10];
int i=0,j,size=sizeof(server);
char rcv[20],snd[20];
int ret;
char ipaddr[50];
while(1)
{
copyfd=readfd;
tv.tv_sec=5;
tv.tv_usec=5000000;
ret=select(maxfd,&copyfd,NULL,NULL,&tv);
if(ret<0)
printf("Select errorn");
if(ret==0)
printf("Timeoutn");
else
{
if(FD_ISSET(serverfd,&copyfd))
{
clientfd[i]=accept(serverfd,(struct sockaddr *)&client,&size);
if(clientfd[i]<0)
{
perror("Accept error");
exit(1);
}
printf("Connection accepted from %s clientn",inet_ntoa(client.sin_addr));
if(maxfd<=clientfd[i])
{
maxfd=clientfd[i]+1;
FD_SET(clientfd[i],&readfd);
i++;
}
}
for(j=0;j<i;j++)
{
if(FD_ISSET(clientfd[j],&copyfd))
{
int r=recv(clientfd[j],rcv,sizeof(rcv),0);
rcv[r]='';
strcpy(ipaddr,inet_ntoa(client.sin_addr));
if(r==0)
{
printf("%s is closed the connectionn",ipaddr);
close(clientfd[j]);
FD_CLR(clientfd[j],&readfd);
break;
}
printf("Message from client is %sn",rcv);
}
}
}
}
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.