Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <winsock2.h>
- #pragma comment(lib,"ws2_32.lib") //Winsock Library
- //#define SERVER "127.0.0.1" //ip address of udp server
- #define BUFLEN 512 //Max length of buffer
- #define POLY 0x8408
- //#define PORT 8888 //The port on which to listen for incoming data
- unsigned short crc(unsigned char *sprava, unsigned short length){
- unsigned char i;
- unsigned int data;
- unsigned int crc = 0xffff;
- if (length == 0)
- return(~crc);
- do
- {
- for (i = 0, data = (unsigned int)0xff & *sprava++;
- i < 8;
- i++, data >>= 1)
- {
- if ((crc & 0x0001) ^ (data & 0x0001))
- crc = (crc >> 1) ^ POLY;
- else crc >>= 1;
- }
- } while (--length);
- crc = ~crc;
- data = crc;
- crc = (crc << 8) | (data >> 8 & 0xff);
- return (crc);
- }
- int skontroluj_crc(char* sprava)
- {
- int crcc, dlzka = sprava[1] * 256 + sprava[2] + 3;
- crcc = crc((unsigned char*)sprava, dlzka);
- if ((unsigned char)sprava[dlzka] == crcc / 256 && (unsigned char)sprava[dlzka + 1] == crcc % 256){
- return 1;
- }
- else{
- return 0;
- }
- }
- unsigned char* vytvor_spravu(char* data, int typ, unsigned short velkost_fragmentu, int offset){
- unsigned char *sprava;
- unsigned short crcc;
- int velkost;
- if (strlen(data+velkost_fragmentu*offset) < velkost_fragmentu){
- velkost = strlen(data+velkost_fragmentu*offset)+1;
- }
- else{
- velkost = velkost_fragmentu;
- }
- switch (typ){
- case 1:/*textova sprava*/
- sprava = (unsigned char*)malloc(sizeof(unsigned char)* (5 + velkost));
- sprava[0] = typ;
- sprava[1] = velkost / 256;
- sprava[2] = velkost % 256;
- strcpy((char*)&sprava[3], data + velkost_fragmentu*offset);
- crcc = crc(sprava, velkost + 3);
- sprava[velkost + 3] = crcc / 256;
- sprava[velkost + 4] = crcc % 256;
- break;
- case 9: //posledna textova sprava
- sprava = (unsigned char*)malloc(sizeof(unsigned char)* (5 + velkost));
- sprava[0] = typ;
- sprava[1] = velkost / 256;
- sprava[2] = velkost % 256;
- strcpy((char*)&sprava[3], data + velkost_fragmentu*offset);
- crcc = crc(sprava, velkost + 3);
- sprava[velkost + 3] = crcc / 256;
- sprava[velkost + 4] = crcc % 256;
- break;
- default:/*spravy bez dat*/
- sprava = (unsigned char*)malloc(sizeof(unsigned char)* (5));
- sprava[0] = typ;
- sprava[1] = 0;
- sprava[2] = 0;
- crcc = crc(sprava, 3);
- sprava[3] = crcc / 256;
- sprava[4] = crcc % 256;
- break;
- }
- return sprava;
- }
- int rozbal_spravu(char* sprava){
- unsigned char *ACK_sprava, *potvrdenie_cinnosti, *potvrdenie_nadviazania;
- switch (sprava[0]){
- case 1:
- ACK_sprava = vytvor_spravu(NULL, 2, 0, 0);//vztvorenie ACK spravy
- return 1;
- break;
- case 3: //prijatie chybneho paketu
- return -1;
- break;
- case 4: //keep alive sprava
- potvrdenie_cinnosti = vytvor_spravu(NULL, 5, 0, 0);
- return 1;
- break;
- case 6: //nadviazanie spojenia
- potvrdenie_nadviazania = vytvor_spravu(NULL, 7, 0, 0);
- return 2;
- break;
- case 9: //posledny fragment textovej spravy
- printf("%s\n", sprava + 3);
- ACK_sprava = vytvor_spravu(NULL, 2, 0, 0);//vztvorenie ACK spravy
- }
- }
- int keep_alive(struct sockaddr_in si_other, int slen, int s,char* buf){
- char *keep_alive;
- keep_alive = (char*)vytvor_spravu("", 4, 0, 0);
- if (sendto(s, keep_alive, strlen(keep_alive), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (buf[0] != 5)
- printf("Nadviazanie nebolo udržané");
- }
- void posli_spravu(char *message, struct sockaddr_in si_other, int slen, int s, int velkost_fragmentu){
- int bity = 0, offset = 0;
- unsigned char *sprava;
- char *buf;
- buf = (char*)malloc(sizeof(char)* 500);
- while (bity+velkost_fragmentu < strlen(message)+1){
- sprava = vytvor_spravu(message, 1, velkost_fragmentu, offset);
- if (sendto(s, (char*)sprava, strlen(message)+5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- /*TODO: pridat funkcionalitu ze to daco urobi*/
- offset++;
- bity += velkost_fragmentu;
- }
- sprava = vytvor_spravu(message, 9, velkost_fragmentu, offset);
- if (sendto(s, (char*)sprava, strlen(message)+6, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- }
- /*
- void client(){
- struct sockaddr_in si_other;
- int s, slen = sizeof(si_other), velkostfrag, port;
- char buf[BUFLEN], IP[15];
- char message[BUFLEN];
- WSADATA wsa;
- printf("Zadajte IP serveru:");
- scanf("%s", &IP);
- if (strcmp(IP, "l") == 0)strcpy(IP, "127.0.0.1");
- printf("Zadajte port:");
- scanf("%d", &port);
- printf("Zadajte max veľkosť fragmentu(1-65502B):");
- scanf("%d", &velkostfrag);
- //Initialise winsock
- printf("\nInitialising Winsock...");
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf("Failed. Error Code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf("Initialised.\n");
- //create socket
- if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
- {
- printf("socket() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- //setup address structure
- memset((char *)&si_other, 0, sizeof(si_other));
- si_other.sin_family = AF_INET;
- si_other.sin_port = htons(port);
- si_other.sin_addr.S_un.S_addr = inet_addr(IP);
- //start communication
- while (1)
- {
- printf("Enter message : ");
- //gets(message);
- scanf("%s", &message);
- //send the message
- if (sendto(s, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- //receive a reply and print it
- //clear the buffer by filling null, it might have previously received data
- memset(buf, '\0', BUFLEN);
- //try to receive some data, this is a blocking call
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf("Druha strana : ");
- puts(buf);
- }
- closesocket(s);
- WSACleanup();
- }*/
- int client(){
- struct sockaddr_in si_other;
- int s, slen = sizeof(si_other), velkostfrag, port;
- char buf[BUFLEN], IP[15];
- char message[BUFLEN];
- WSADATA wsa;
- printf("Zadajte IP serveru:");
- //vymazat scanf("%s", &IP);
- strcpy(IP,"l");
- if (strcmp(IP, "l") == 0)strcpy(IP, "127.0.0.1");
- printf("Zadajte port:");
- //vymazat scanf("%d", &port);
- port = 8888;
- printf("Zadajte max veľkosť fragmentu(1-65502B):");
- //vymazat scanf("%d", &velkostfrag);
- velkostfrag = 5;
- //Initialise winsock
- printf("\nInitialising Winsock...");
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf("Failed. Error Code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf("Initialised.\n");
- //create socket
- if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
- {
- printf("socket() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- //setup address structure
- memset((char *)&si_other, 0, sizeof(si_other));
- si_other.sin_family = AF_INET;
- si_other.sin_port = htons(port);
- si_other.sin_addr.S_un.S_addr = inet_addr(IP);
- //start communication
- char *nadviazanie_spojenia;
- //nadviazanie spojenia
- nadviazanie_spojenia = (char*)vytvor_spravu("",6,0,0);
- if (sendto(s, nadviazanie_spojenia, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (buf[0] != 7){
- printf("Nadviazanie spojenia nebolo úspešné");//pridat timeout ak to bude trvat moc dlho
- return -1;
- }
- char akcia;
- printf("s -posielanie sprav\np -prijimanie sprav\nc -poslanie chybneho fragmentu\nz -zmena klienta/servera\nk -koniec\n");
- printf("Vyberte si akciu:");
- while (scanf("%c",&akcia)>0)//akcia-> posielaj spravy, primaj spravy, zmen max velkost, zmen klienta//server
- {
- int prvy_krat=0;
- switch (akcia)
- {
- case 's': //sprava, ked je prazdna prejdi do vyberu
- {
- char* sprava;
- std::string input;
- int znovuposlanie = 0;
- sprava = (char*)malloc(sizeof(char)* 100);
- while (1){
- printf("Ja\t: ");
- if(prvy_krat==0)getchar();
- prvy_krat = 1;
- gets(sprava);
- /*std::getline(std::cin, input);
- char *sprava = input.c_str();*/
- //scanf("%s", sprava);
- if (strcmp(sprava, "\n") == 0){
- break;
- }
- posli_spravu(sprava, si_other, slen, s, velkostfrag);
- //prijatie ACK
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- while (buf[0] == 3 && znovuposlanie < 3){
- znovuposlanie++;
- posli_spravu(sprava, si_other, slen, s, velkostfrag);
- if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- }
- }
- break;
- }
- case'p': //prijmi spravu
- break;
- case 'c': //posli chybny paket
- break;
- case'f': //zmen max velkost dat vo fragmente
- break;
- case 'z': //zmen klienta/server
- break;
- case 'k': //koniec
- break;
- default:
- break;
- }
- }
- char *ukoncenie_spojenia;
- ukoncenie_spojenia =(char*) vytvor_spravu("",8,0,0);
- if (sendto(s, ukoncenie_spojenia, strlen(ukoncenie_spojenia), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- closesocket(s);
- WSACleanup();
- }
- /*
- void server(){
- SOCKET s;
- struct sockaddr_in server, si_other;
- int slen, recv_len, port;
- char buf[BUFLEN];
- WSADATA wsa;
- slen = sizeof(si_other);
- printf("Zadajte port:");
- scanf("%d", &port);
- //Initialise winsock
- printf("\nInitialising Winsock...");
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf("Failed. Error Code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf("Initialised.\n");
- //Create a socket
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
- {
- printf("Could not create socket : %d", WSAGetLastError());
- }
- printf("Socket created.\n");
- //Prepare the sockaddr_in structure
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons(port);
- //Bind
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
- {
- printf("Bind failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- puts("Bind done");
- //keep listening for data
- while (1)
- {
- printf("Waiting for data...");
- fflush(stdout);
- //clear the buffer by filling null, it might have previously received data
- memset(buf, '\0', BUFLEN);
- //try to receive some data, this is a blocking call
- if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- char *sprava;
- if (buf[0] == 6)
- {
- sprava = (char*)vytvor_spravu("", 7, 0, 0);
- if (sendto(s, sprava, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- }
- //print details of the client/peer and the data received
- //printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
- printf("You: %s\n", buf + 3);
- //char sprava[BUFLEN];
- /*printf("poslite spravu:");
- scanf("%s", sprava);
- if (sendto(s, sprava, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }*/
- //now reply the client with the same data
- /*
- if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- }
- closesocket(s);
- WSACleanup();
- }
- */
- void server(){
- SOCKET s;
- struct sockaddr_in server, si_other;
- int slen, recv_len, port;
- char buf[BUFLEN];
- WSADATA wsa;
- slen = sizeof(si_other);
- printf("Zadajte port:");
- //scanf("%d", &port);
- port = 8888;
- //Initialise winsock
- printf("\nInitialising Winsock...");
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf("Failed. Error Code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf("Initialised.\n");
- //Create a socket
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
- {
- printf("Could not create socket : %d", WSAGetLastError());
- }
- printf("Socket created.\n");
- //Prepare the sockaddr_in structure
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons(port);
- //Bind
- if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
- {
- printf("Bind failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- puts("Bind done");
- //keep listening for data
- char *final_sprava;
- int pismen = 0;
- final_sprava = (char*)malloc(sizeof(char)* 500);
- while (1)
- {
- fflush(stdout);
- //clear the buffer by filling null, it might have previously received data
- memset(buf, '\0', BUFLEN);
- //try to receive some data, this is a blocking call
- if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
- {
- printf("recvfrom() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- if (skontroluj_crc(buf) == 0){
- char* msg;
- msg = (char*)vytvor_spravu("", 3, 0, 0);
- if (sendto(s, msg, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- }
- else
- {
- char *sprava;
- switch (buf[0]){
- case 6:
- sprava = (char*)vytvor_spravu("", 7, 0, 0);
- if (sendto(s, sprava, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- break;
- case 1:
- sprava = (char*)vytvor_spravu("", 2, 0, 0);
- if (sendto(s, sprava, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- strcpy(final_sprava + pismen, buf + 3);
- pismen += (unsigned char)buf[1] * 256 + (unsigned char)buf[2];
- break;
- case 9:
- sprava = (char*)vytvor_spravu("", 2, 0, 0);
- if (sendto(s, sprava, 5, 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- strcpy(final_sprava + pismen, buf + 3);
- printf("You\t: %s\n", final_sprava);
- pismen = 0;
- break;
- }
- }
- //print details of the client/peer and the data received
- //printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
- //printf("You\t: %s\n", buf + 3);
- //char sprava[BUFLEN];
- /*printf("poslite spravu:");
- scanf("%s", sprava);
- if (sendto(s, sprava, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }*/
- //now reply the client with the same data
- /*
- if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
- {
- printf("sendto() failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }*/
- }
- closesocket(s);
- WSACleanup();
- }
- int main(void)
- {
- unsigned char *c;
- /*c = vytvor_spravu("abcdefghijk", 1, 7, 1);
- vytvor_spravu("",3, 0, 0);//velkost aj s \0 takze o jedno vacsie ako je pocet pismen
- printf("%s",(char*)( c+ 3));*/
- char rezim;
- printf("Vyberte si rezim aplikácie(s-server/c-klient):");
- scanf("%c", &rezim);
- switch (rezim){
- case 's':
- server();
- break;
- case 'c':
- client();
- break;
- default:
- printf("Zadali ste zlú hodnotu.\nVyberte si režim aplikácie(s-server/c-klient):");
- break;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement