#include #include #include #include #define __USE_MISC #include #include #include #include "Stream.h" #include "Client.h" #include "SocketUtils.h" void clientInitDefaults(Client* c) { c->port = -1; c->socket = -1; } int clientInit(Client* c, char* ip, short port) { c->port = port; c->socket = socket(AF_INET, SOCK_STREAM, 0); if(c->socket == -1) { perror("Cannot create socket"); clientRemove(c); return -1; } struct sockaddr_in socketData; memset(&socketData, 0, sizeof(struct sockaddr_in)); socketData.sin_family = AF_INET; socketData.sin_port = htons(port); if(inet_aton(ip, &socketData.sin_addr) == 0) { printf("'%s' is not a valid ip\n", ip); clientRemove(c); return -1; } if(connect(c->socket, (struct sockaddr*) &socketData, sizeof(struct sockaddr_in)) == 0) { printf("Connection with server (%s) established\n", inet_ntoa(socketData.sin_addr)); Stream in; streamInit(&in, 1024); if(clientReceive(c, &in) == -1) { printf("Server did not respond"); streamRemove(&in); clientRemove(c); return -1; } else { char answer; if(streamGetChar(&in, &answer) == -1) { printf("Server sent an invalid response"); streamRemove(&in); clientRemove(c); return -1; } else if(answer > 0) { printf("Successfully received server response\n"); char welcome[100]; streamGetChars(&in, welcome, 100); printf("%s\n", welcome); } else { char error[100]; streamGetChars(&in, error, 100); printf("%s\n", error); streamRemove(&in); clientRemove(c); return -1; } } streamRemove(&in); } else { perror("No server is available"); clientRemove(c); return -1; } return 0; } int clientReceive(Client* c, Stream* in) { int size = receiveAll(c->socket, in); if(size > 0) { return 0; } return -1; } void clientRemove(Client* c) { c->port = -1; if(c->socket != -1) { if(close(c->socket) != 0) { perror("Cannot close client socket"); } else { printf("socket closed\n"); } } } void clearInput() { int c = getchar(); while(c != EOF && c != '\n') { c = getchar(); } } int readInt() { int type; while(scanf("%d", &type) == 0) { printf("Not a number\n"); clearInput(); } clearInput(); return type; } void clientWaitForData(Client* c) { while(1) { printf("Choose a package: "); int type = readInt(); if(type == -1) { break; } else if(type >= 0 && type < 4) { Stream out; streamInit(&out, 16); streamWriteChar(&out, type); switch(type) { case 0: printf("first number: "); streamWriteInt(&out, readInt()); printf("second number: "); streamWriteInt(&out, readInt()); break; case 1: printf("text: "); char buffer[128]; fgets(buffer, 128, stdin); if(strlen(buffer) == 127 && buffer[126] != '\n') { clearInput(); } streamWriteChars(&out, buffer); break; case 2: for(int i = 0; i < 260; i++) { streamWriteInt(&out, i); } break; } if(clientSendData(c, &out) == -1) { streamRemove(&out); break; } streamRemove(&out); } else { printf("%d is not a valid package number\n", type); } } } int clientSendData(Client* c, Stream* s) { int bytes = sendAll(c->socket, s); if(bytes == -1) { perror("Cannot send data"); return -1; } //printf("%d Bytes from %d sent\n", bytes, s->index); return 0; }