NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

server

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

int main(){
int welcomeSocket, newSocket, portNum, clientLen, nBytes;
char buffer[1024],sz[50];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
int i;

welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

portNum = 7891;

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);

bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

if(listen(welcomeSocket,5)==0)
printf("Listeningn");
else
printf("Errorn");

addr_size = sizeof serverStorage;

/*loop to keep accepting new connections*/
while(1){
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*fork a child process to handle the new connection*/
if(!fork()){
nBytes = 1;
/*loop while connection is live*/
while(nBytes!=0){
nBytes = recv(newSocket,buffer,1024,0);
FILE *fp;
int size = 0;
fp = fopen(buffer, "r");
if (fp == NULL)
printf("nFile unable to open ");
else
printf("nFile opened : %sn",buffer);
fseek(fp, 0, 2); /* file pointer at the end of file */
size = ftell(fp)-1; /* take a position of file pointer un size variable */
snprintf (sz, sizeof(sz), "%d",size);
printf("The size of given file is : %dn", size);
fclose(fp);
send(newSocket,sz,nBytes,0);
}
close(newSocket);
exit(0);
}
/*if parent, close the socket and go back to listening new requests*/
else{
close(newSocket);
}
}

return 0;
}



client

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

int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

portNum = 7891;

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);

addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

while(1){
printf("Type a file name to send to server: ");
scanf("%s",buffer);
//printf("You typed: %s",buffer);

nBytes = strlen(buffer) + 1;

send(clientSocket,buffer,nBytes,0);

recv(clientSocket, buffer, 1024, 0);

printf("Size of file received from server: %snn",buffer);
}

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.