diff --git a/cw4zadDom/client b/cw4zadDom/client new file mode 100755 index 0000000..df1d0b1 Binary files /dev/null and b/cw4zadDom/client differ diff --git a/cw4zadDom/client.c b/cw4zadDom/client.c new file mode 100644 index 0000000..7097b53 --- /dev/null +++ b/cw4zadDom/client.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/cw4zadDom/server b/cw4zadDom/server new file mode 100755 index 0000000..b7be55f Binary files /dev/null and b/cw4zadDom/server differ diff --git a/cw4zadDom/server.c b/cw4zadDom/server.c new file mode 100644 index 0000000..55de69b --- /dev/null +++ b/cw4zadDom/server.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; +}