cw4zadDom

This commit is contained in:
Kamil Bartczak 2019-01-13 01:31:17 +01:00
parent b0ccba1b1b
commit 4d74375cf0
4 changed files with 173 additions and 0 deletions

BIN
cw4zadDom/client Executable file

Binary file not shown.

65
cw4zadDom/client.c Normal file
View File

@ -0,0 +1,65 @@
#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>
int main(int argc, char **argv)
{
if (argc < 2) {
printf("podaj numer portu jako parametr\n");
return 1;
}
struct sockaddr_in endpoint;
int sdsocket, addrlen, i, received;
struct hostent *he;
char *port = argv[1];
// char host[100] = "150.254.79.244";
char host[100] = "127.0.1.1";
long message = 426083, receivedMessage;
he = gethostbyname(host);
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(atoi(port));
endpoint.sin_addr = *(struct in_addr*) he->h_addr;
addrlen = sizeof(struct sockaddr_in);
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("socket() %d nie powiodl sie\n", i);
return 1;
}
if (connect(sdsocket,(struct sockaddr*) &endpoint, addrlen) < 0) {
printf("connect() %d nie powiodl sie\n", i);
return 0;
}
message = htonl(message);
send(sdsocket, &message, sizeof(message), 0);
received = 0;
while (received < sizeof(long))
{
received += recv(sdsocket,
&receivedMessage,
sizeof(long),
0);
}
receivedMessage = ntohl(receivedMessage);
receivedMessage += 1;
receivedMessage = htonl(receivedMessage);
send(sdsocket, &receivedMessage, sizeof(long), 0);
return 0;
}

BIN
cw4zadDom/server Executable file

Binary file not shown.

108
cw4zadDom/server.c Normal file
View File

@ -0,0 +1,108 @@
#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>
struct sockaddr_in endpoint;
FILE *flog;
char myhostname[1024];
int main(int argc, char **argv) {
long lnIn1, lnIn2, lhIn1, lhIn2, lhOut, lnOut;
int sdServerSocket, sdConnection, retval;
socklen_t sin_size;
struct sockaddr_in incoming;
struct hostent *heLocalHost;
char sign;
sin_size = sizeof(struct sockaddr_in);
sdServerSocket = socket(PF_INET, SOCK_STREAM, 0);
gethostname(myhostname, 1023);
heLocalHost = gethostbyname(myhostname);
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(14444);
endpoint.sin_addr = *(struct in_addr*)
heLocalHost->h_addr;
memset(&(endpoint.sin_zero),0,8);
printf("slucham na(listening on) %s:%d\n",
inet_ntoa(endpoint.sin_addr),
ntohs(endpoint.sin_port));
retval = bind(sdServerSocket,
(struct sockaddr*) &endpoint,
sizeof(struct sockaddr));
if (retval < 0) {
printf("bind nie powiodl sie (bind failed)\n");
return 1;
}
listen(sdServerSocket, 10);
sin_size = sizeof(struct sockaddr_in);
while ((sdConnection =
accept(sdServerSocket,
(struct sockaddr*) &incoming,
&sin_size))
> 0) {
printf("Polaczenie z (connection with)%s:%d\n",
inet_ntoa(incoming.sin_addr),
ntohs(incoming.sin_port));
if (recv(sdConnection, &lnIn1, sizeof(long),0)
!= sizeof(long)) {
printf("pierwszy recv nie powiodl sie (first recv failed)\n");
close(sdConnection);
continue;
}
lhIn1 = ntohl(lnIn1);
lhOut = random();
lnOut = htonl(lhOut);
if (send(sdConnection, &lnOut, sizeof(long), 0)
!= sizeof(long)) {
printf("send nie powiodl sie (send failed)\n");
close(sdConnection);
continue;
}
if (recv(sdConnection, &lnIn2, sizeof(long), 0)
!= sizeof(long)) {
printf("drugi recv nie powiodl sie (second recv failed)\n");
close(sdConnection);
continue;
}
lhIn2 = ntohl(lnIn2);
flog = fopen("zad.txt","a");
if (lhIn2 == lhOut + 1) sign = '+';
else sign = '-';
fprintf(flog,"%c %ld from %s:%d : %ld, %ld\n",
sign,
lhIn1,
inet_ntoa(incoming.sin_addr),
ntohs(incoming.sin_port),
lhOut,
lhIn2);
close(sdConnection);
fflush(flog);
fclose(flog);
}
printf("Blad sieci (network error)\n");
fclose(flog);
return 0;
}