123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include <sys/socket.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define __USE_MISC
- #include <arpa/inet.h>
- #include <string.h>
- #include <unistd.h>
- #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;
- }
|