Created
November 8, 2019 07:30
-
-
Save harsh-98/5044c0d5ea8d50cac5cebe27c00a2036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <arpa/inet.h> | |
int main(int argc, char *argv[]){ | |
int sfd =0, n=0, b; | |
char rbuff[1024]; | |
char sendbuffer[100000]; | |
struct sockaddr_in serv_addr; | |
memset(rbuff, '0', sizeof(rbuff)); | |
sfd = socket(AF_INET, SOCK_STREAM, 0); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(5000); | |
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
b=connect(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); | |
if (b==-1) { | |
perror("Connect"); | |
return 1; | |
} | |
FILE *fp = fopen("100MB.img", "rb"); | |
if(fp == NULL){ | |
perror("File"); | |
return 2; | |
} | |
while( (b = fread(sendbuffer, 1, sizeof(sendbuffer), fp))>0 ){ | |
send(sfd, sendbuffer, b, 0); | |
} | |
fclose(fp); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <sys/types.h> | |
int main(int argc, char *argv[]){ | |
int fd =0, confd = 0,b,tot; | |
struct sockaddr_in serv_addr; | |
char buff[100001]; | |
int num; | |
fd = socket(AF_INET, SOCK_STREAM, 0); | |
printf("Socket created\n"); | |
memset(&serv_addr, '0', sizeof(serv_addr)); | |
memset(buff, '0', sizeof(buff)); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
serv_addr.sin_port = htons(5000); | |
bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); | |
listen(fd, 10); | |
while(1){ | |
confd = accept(fd, (struct sockaddr*)NULL, NULL); | |
if (confd==-1) { | |
perror("Accept"); | |
continue; | |
} | |
FILE* fp = fopen( "a.jpg", "wb"); | |
tot=0; | |
if(fp != NULL){ | |
while( (b = recv(confd, buff, 100000,0))> 0 ) { | |
tot+=b; | |
fwrite(buff, 1, b, fp); | |
} | |
printf("Received byte: %d\n",tot); | |
if (b<0) | |
perror("Receiving"); | |
fclose(fp); | |
} else { | |
perror("File"); | |
} | |
close(confd); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## TCP server/client | |
server writes the file(tcp_server.c) / client reads and sends the file to server(tcp_client.c) | |
## UNIX server/client | |
server reads and sends teh file over the network(unix_server.c) / client writes the file server(unix_client.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include "connection.h" | |
#include <errno.h> | |
#include <sys/types.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
struct sockaddr_un addr; | |
int i; | |
int ret; | |
int data_socket; | |
char buffer[BUFFER_SIZE]; | |
/* Create local socket. */ | |
data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); | |
if (data_socket == -1) { | |
perror("socket"); | |
exit(EXIT_FAILURE); | |
} | |
/* | |
* For portability clear the whole structure, since some | |
* implementations have additional (nonstandard) fields in | |
* the structure. | |
*/ | |
memset(&addr, 0, sizeof(struct sockaddr_un)); | |
/* Connect socket to socket address */ | |
addr.sun_family = AF_UNIX; | |
strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1); | |
ret = connect (data_socket, (const struct sockaddr *) &addr, | |
sizeof(struct sockaddr_un)); | |
if (ret == -1) { | |
fprintf(stderr, "The server is down.\n"); | |
exit(EXIT_FAILURE); | |
} | |
/* Send arguments. */ | |
strcpy (buffer, "END"); | |
ret = write(data_socket, buffer, strlen(buffer) + 1); | |
if (ret == -1) { | |
perror("write"); | |
exit(EXIT_FAILURE); | |
} | |
/* Receive result. */ | |
int b; | |
int tot=0; | |
FILE* fp = fopen( "a.jpg", "wb"); | |
while( (b = recv(data_socket, buffer, 100000,0))> 0 ) { | |
tot+=b; | |
fwrite(buffer, 1, b, fp); | |
} | |
printf("%d", tot); | |
/* Ensure buffer is 0-terminated. */ | |
buffer[BUFFER_SIZE - 1] = 0; | |
printf("Result = %s\n", buffer); | |
/* Close socket. */ | |
close(data_socket); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File server.c | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#include "connection.h" | |
#include <sys/types.h> | |
#include <errno.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
struct sockaddr_un name; | |
int down_flag = 0; | |
int ret; | |
int connection_socket; | |
int data_socket; | |
int result; | |
char buffer[BUFFER_SIZE]; | |
/* | |
* In case the program exited inadvertently on the last run, | |
* remove the socket. | |
*/ | |
unlink(SOCKET_NAME); | |
/* Create local socket. */ | |
connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); | |
if (connection_socket == -1) { | |
perror("socket"); | |
exit(EXIT_FAILURE); | |
} | |
/* | |
* For portability clear the whole structure, since some | |
* implementations have additional (nonstandard) fields in | |
* the structure. | |
*/ | |
memset(&name, 0, sizeof(struct sockaddr_un)); | |
/* Bind socket to socket name. */ | |
name.sun_family = AF_UNIX; | |
strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1); | |
ret = bind(connection_socket, (const struct sockaddr *) &name, | |
sizeof(struct sockaddr_un)); | |
if (ret == -1) { | |
perror("bind"); | |
exit(EXIT_FAILURE); | |
} | |
/* | |
* Prepare for accepting connections. The backlog size is set | |
* to 20. So while one request is being processed other requests | |
* can be waiting. | |
*/ | |
ret = listen(connection_socket, 20); | |
if (ret == -1) { | |
perror("listen"); | |
exit(EXIT_FAILURE); | |
} | |
/* This is the main loop for handling connections. */ | |
/* Wait for incoming connection. */ | |
data_socket = accept(connection_socket, NULL, NULL); | |
if (data_socket == -1) { | |
perror("accept"); | |
exit(EXIT_FAILURE); | |
} | |
result = 0; | |
/* Wait for next data packet. */ | |
ret = read(data_socket, buffer, BUFFER_SIZE); | |
if (ret == -1) { | |
perror("read"); | |
exit(EXIT_FAILURE); | |
} | |
int b; | |
FILE *fp = fopen("1GB.img", "rb"); | |
while( (b = fread(buffer, 1, sizeof(buffer), fp))>0 ){ | |
send(data_socket, buffer, b, 0); | |
} | |
/* Ensure buffer is 0-terminated. */ | |
buffer[BUFFER_SIZE - 1] = 0; | |
/* Handle commands. */ | |
/* Send result. */ | |
/* Close socket. */ | |
close(data_socket); | |
close(connection_socket); | |
/* Unlink the socket. */ | |
unlink(SOCKET_NAME); | |
exit(EXIT_SUCCESS); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment