cw2zad3
This commit is contained in:
commit
b0ccba1b1b
BIN
cw2zad3/drugie/client
Executable file
BIN
cw2zad3/drugie/client
Executable file
Binary file not shown.
63
cw2zad3/drugie/client.c
Normal file
63
cw2zad3/drugie/client.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define BUFFER_SIZE 30000
|
||||
#define ATTEMPTS 100
|
||||
|
||||
char buf[BUFFER_SIZE];
|
||||
|
||||
/*
|
||||
argv[1] - nazwa hosta
|
||||
argv[2] - numer portu
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in endpoint;
|
||||
int sdsocket, addrlen, i, received;
|
||||
struct hostent *he;
|
||||
struct timeval time_b, time_e;
|
||||
|
||||
if (argc<3) {
|
||||
printf("podaj nazwe hosta i numer portu jako parametry\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
he = gethostbyname(argv[1]);
|
||||
if (he == NULL) {
|
||||
printf("Nieznany host: %s\n",argv[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
endpoint.sin_family = AF_INET;
|
||||
endpoint.sin_port = htons(atoi(argv[2]));
|
||||
endpoint.sin_addr = *(struct in_addr*) he->h_addr;
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
gettimeofday(&time_b, NULL);
|
||||
for (i=0; i<ATTEMPTS; i++) {
|
||||
|
||||
if ((sdsocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
printf("socket() %d nie powiodl sie\n", i);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sendto(sdsocket, buf, BUFFER_SIZE, 0, (struct sockaddr*) &endpoint, addrlen);
|
||||
|
||||
received = 0;
|
||||
}
|
||||
gettimeofday(&time_e, NULL);
|
||||
|
||||
printf("czas: %.6f s\n",
|
||||
(((double) (time_e.tv_sec - time_b.tv_sec) * 1000000) +
|
||||
((double) (time_e.tv_usec - time_b.tv_usec)))
|
||||
/ (1000000.0 * ATTEMPTS));
|
||||
return 0;
|
||||
}
|
BIN
cw2zad3/drugie/server
Executable file
BIN
cw2zad3/drugie/server
Executable file
Binary file not shown.
63
cw2zad3/drugie/server.c
Normal file
63
cw2zad3/drugie/server.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
/* rozmiar bufora */
|
||||
#define BUFFER_SIZE 30000
|
||||
|
||||
/* liczba powtorzen */
|
||||
#define ATTEMPTS 100
|
||||
|
||||
char buf[BUFFER_SIZE];
|
||||
|
||||
/*
|
||||
argv[1] - numer portu
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in myaddr, endpoint;
|
||||
int sdsocket, sdconnection, addrlen, received;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("podaj numer portu jako parametr\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sdsocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
myaddr.sin_family = AF_INET;
|
||||
myaddr.sin_port = htons(atoi(argv[1]));
|
||||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (bind(sdsocket,(struct sockaddr*) &myaddr,addrlen) < 0) {
|
||||
printf("bind() nie powiodl sie\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
received = 0;
|
||||
|
||||
while(1) {
|
||||
/* odbior moze odbywac sie w mniejszych
|
||||
segmentach */
|
||||
while (received < BUFFER_SIZE)
|
||||
{
|
||||
received += recvfrom(sdsocket,
|
||||
buf+received,
|
||||
BUFFER_SIZE-received,
|
||||
0,
|
||||
(struct sockaddr*) &endpoint,
|
||||
&addrlen);
|
||||
}
|
||||
|
||||
sendto(sdsocket, buf, BUFFER_SIZE, 0, (struct sockaddr*) &endpoint, addrlen);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
cw2zad3/pierwsze/client
Executable file
BIN
cw2zad3/pierwsze/client
Executable file
Binary file not shown.
74
cw2zad3/pierwsze/client.c
Normal file
74
cw2zad3/pierwsze/client.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define BUFFER_SIZE 30000
|
||||
#define ATTEMPTS 100
|
||||
|
||||
char buf[BUFFER_SIZE];
|
||||
|
||||
/*
|
||||
argv[1] - nazwa hosta
|
||||
argv[2] - numer portu
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in endpoint;
|
||||
int sdsocket, addrlen, i, received;
|
||||
struct hostent *he;
|
||||
struct timeval time_b, time_e;
|
||||
|
||||
if (argc<3) {
|
||||
printf("podaj nazwe hosta i numer portu jako parametry (provide hostname and port number in command line)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
he = gethostbyname(argv[1]);
|
||||
if (he == NULL) {
|
||||
printf("Nieznany host (unknown kost): %s\n",argv[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
endpoint.sin_family = AF_INET;
|
||||
endpoint.sin_port = htons(atoi(argv[2]));
|
||||
endpoint.sin_addr = *(struct in_addr*) he->h_addr;
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
gettimeofday(&time_b, NULL);
|
||||
for (i=0; i<ATTEMPTS; i++) {
|
||||
|
||||
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
printf("socket() %d nie powiodl sie (failed)\n", i);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (connect(sdsocket,(struct sockaddr*) &endpoint, addrlen) < 0) {
|
||||
printf("connect() %d nie powiodl sie (failed)\n", i);
|
||||
return 0;
|
||||
}
|
||||
send(sdsocket, buf, BUFFER_SIZE, 0);
|
||||
received = 0;
|
||||
while (received < BUFFER_SIZE)
|
||||
{
|
||||
received += recv(sdsocket,
|
||||
buf+received,
|
||||
BUFFER_SIZE-received,
|
||||
0);
|
||||
}
|
||||
close(sdsocket);
|
||||
}
|
||||
gettimeofday(&time_e, NULL);
|
||||
|
||||
printf("czas: %.6f s\n",
|
||||
(((double) (time_e.tv_sec - time_b.tv_sec) * 1000000) +
|
||||
((double) (time_e.tv_usec - time_b.tv_usec)))
|
||||
/ (1000000.0 * ATTEMPTS));
|
||||
return 0;
|
||||
}
|
BIN
cw2zad3/pierwsze/server
Executable file
BIN
cw2zad3/pierwsze/server
Executable file
Binary file not shown.
71
cw2zad3/pierwsze/server.c
Normal file
71
cw2zad3/pierwsze/server.c
Normal file
@ -0,0 +1,71 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
/* rozmiar bufora */
|
||||
#define BUFFER_SIZE 30000
|
||||
|
||||
/* liczba powtorzen */
|
||||
#define ATTEMPTS 100
|
||||
|
||||
char buf[BUFFER_SIZE];
|
||||
|
||||
/*
|
||||
argv[1] - numer portu
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct sockaddr_in myaddr, endpoint;
|
||||
int sdsocket, sdconnection, addrlen, received;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("podaj numer portu jako parametr\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sdsocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
addrlen = sizeof(struct sockaddr_in);
|
||||
|
||||
myaddr.sin_family = AF_INET;
|
||||
myaddr.sin_port = htons(atoi(argv[1]));
|
||||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (bind(sdsocket,(struct sockaddr*) &myaddr,addrlen) < 0) {
|
||||
printf("bind() nie powiodl sie\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(sdsocket, 10) < 0) {
|
||||
printf("listen() nie powiodl sie\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while ((sdconnection =
|
||||
accept(sdsocket,
|
||||
(struct sockaddr*) &endpoint,
|
||||
&addrlen)) >= 0)
|
||||
{
|
||||
received = 0;
|
||||
/* odbior moze odbywac sie w mniejszych
|
||||
segmentach */
|
||||
while (received < BUFFER_SIZE)
|
||||
{
|
||||
received += recv(sdconnection,
|
||||
buf+received,
|
||||
BUFFER_SIZE-received,
|
||||
0);
|
||||
}
|
||||
send(sdconnection, buf, BUFFER_SIZE, 0);
|
||||
close(sdconnection);
|
||||
}
|
||||
|
||||
close(sdsocket);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user