Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ws2tcpip.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <WinSock2.h>
- #define DEFAULT_BUFLEN 1024
- #define DEFAULT_PORT "27016"
- #define SERVER_SLEEP_TIME 50
- bool InitializeWindowsSockets();
- int RecieveData(SOCKET socket, char *recvbuf, int size);
- int RecieveSize(SOCKET socket);
- int Send(SOCKET socket, char*recvbuf, int size);
- int Select(SOCKET socket, char funkcija);
- int main(void)
- {
- // Socket used for listening for new clients
- SOCKET listenSocket = INVALID_SOCKET;
- // Socket used for communication with client
- SOCKET acceptedSocket = INVALID_SOCKET;
- // variable used to store function return value
- int iResult;
- // Buffer used for storing incoming data
- char recvbuf[DEFAULT_BUFLEN];
- if(InitializeWindowsSockets() == false)
- {
- // we won't log anything since it will be logged
- // by InitializeWindowsSockets() function
- return 1;
- }
- // Prepare address information structures
- addrinfo *resultingAddress = NULL;
- addrinfo hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET; // IPv4 address
- hints.ai_socktype = SOCK_STREAM; // Provide reliable data streaming
- hints.ai_protocol = IPPROTO_TCP; // Use TCP protocol
- hints.ai_flags = AI_PASSIVE; //
- // Resolve the server address and port
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &resultingAddress);
- if ( iResult != 0 )
- {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- return 1;
- }
- // Create a SOCKET for connecting to server
- listenSocket = socket(AF_INET, // IPv4 address famly
- SOCK_STREAM, // stream socket
- IPPROTO_TCP); // TCP
- if (listenSocket == INVALID_SOCKET)
- {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- freeaddrinfo(resultingAddress);
- WSACleanup();
- return 1;
- }
- // Setup the TCP listening socket - bind port number and local address
- // to socket
- iResult = bind( listenSocket, resultingAddress->ai_addr, (int)resultingAddress->ai_addrlen);
- if (iResult == SOCKET_ERROR)
- {
- printf("bind failed with error: %d\n", WSAGetLastError());
- freeaddrinfo(resultingAddress);
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- // Set socket to nonblocking mode
- unsigned long int nonBlockingMode = 1;
- iResult = ioctlsocket(listenSocket, FIONBIO, &nonBlockingMode);
- if (iResult == SOCKET_ERROR)
- {
- printf("ioctlsocket failed with error: %ld\n", WSAGetLastError());
- return 1;
- }
- // Since we don't need resultingAddress any more, free it
- freeaddrinfo(resultingAddress);
- // Set listenSocket in listening mode
- iResult = listen(listenSocket, SOMAXCONN);
- if (iResult == SOCKET_ERROR)
- {
- printf("listen failed with error: %d\n", WSAGetLastError());
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- printf("Server initialized, waiting for clients.\n");
- do
- {
- // Wait for clients and accept client connections.
- // Returning value is acceptedSocket used for further
- // Client<->Server communication. This version of
- // server will handle only one client.
- Select(listenSocket, 'r');
- acceptedSocket = accept(listenSocket, NULL, NULL);
- if (acceptedSocket == INVALID_SOCKET)
- {
- printf("accept failed with error: %d\n", WSAGetLastError());
- closesocket(listenSocket);
- WSACleanup();
- return 1;
- }
- unsigned long int nonBlockingMode = 1;
- iResult = ioctlsocket(acceptedSocket, FIONBIO, &nonBlockingMode);
- if (iResult == SOCKET_ERROR)
- {
- printf("ioctlsocket failed with error: %ld\n", WSAGetLastError());
- return 1;
- }
- int size = RecieveSize(acceptedSocket);
- printf("Primljena velicina: %d\n", size);
- iResult = RecieveData(acceptedSocket, recvbuf, size);
- } while (1);
- // shutdown the connection since we're done
- iResult = shutdown(acceptedSocket, SD_SEND);
- if (iResult == SOCKET_ERROR)
- {
- printf("shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(acceptedSocket);
- WSACleanup();
- return 1;
- }
- // cleanup
- closesocket(listenSocket);
- closesocket(acceptedSocket);
- WSACleanup();
- return 0;
- }
- bool InitializeWindowsSockets()
- {
- WSADATA wsaData;
- // Initialize windows sockets library for this process
- if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
- {
- printf("WSAStartup failed with error: %d\n", WSAGetLastError());
- return false;
- }
- return true;
- }
- int RecieveSize(SOCKET socket) {
- int iResult;
- int pristigliBajtovi = 0;
- int size;
- char recvbuf[sizeof(int)];
- // clientAddress will be populated from recvfrom
- sockaddr_in clientAddress;
- memset(&clientAddress, 0, sizeof(sockaddr_in));
- // set whole buffer to zero
- memset(recvbuf, 0, sizeof(int));
- // Izvuci size (prva 4)
- do
- {
- Select(socket, 'r');
- iResult = recv(socket, (recvbuf + pristigliBajtovi), sizeof(int) - pristigliBajtovi, 0);
- pristigliBajtovi += iResult;
- } while (pristigliBajtovi < sizeof(int));
- size = *(int*)recvbuf;
- return size;
- }
- int RecieveData(SOCKET socket, char *recvbuf, int size) {
- int iResult;
- int pristigliBajtovi = 0;
- // clientAddress will be populated from recvfrom
- sockaddr_in clientAddress;
- memset(&clientAddress, 0, sizeof(sockaddr_in));
- // set whole buffer to zero
- memset(recvbuf, 0, size);
- do
- {
- Select(socket, 'r');
- iResult = recv(socket, (recvbuf + pristigliBajtovi), size - pristigliBajtovi, 0);
- pristigliBajtovi += iResult;
- } while (pristigliBajtovi < size);
- printf("Primljena poruka: %s\n", recvbuf);
- return iResult;
- }
- int Select(SOCKET socket, char funkcija) {
- int iResult;
- do
- {
- // Initialize select parameters
- FD_SET set;
- timeval timeVal;
- FD_ZERO(&set);
- // Add socket we will wait to read from
- FD_SET(socket, &set);
- // Set timeouts to zero since we want select to return
- // instantaneously
- timeVal.tv_sec = 0;
- timeVal.tv_usec = 0;
- if (funkcija == 'r')
- {
- iResult = select(0 /* ignored */, &set, NULL, NULL, &timeVal);
- }
- else
- {
- iResult = select(0 /* ignored */, NULL, &set, NULL, &timeVal);
- }
- // lets check if there was an error during select
- if (iResult == SOCKET_ERROR)
- {
- fprintf(stderr, "select failed with error: %ld\n", WSAGetLastError());
- continue;
- }
- // now, lets check if there are any sockets ready
- if (iResult == 0)
- {
- // there are no ready sockets, sleep for a while and check again
- Sleep(SERVER_SLEEP_TIME);
- continue;
- }
- } while (iResult <= 0);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement