Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WinSockTest.cpp : Defines the entry point for the console application.
- //
- #define _CRT_SECURE_NO_DEPRECATE
- #define WIN32_LEAN_AND_MEAN
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include "stdafx.h"
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- //Winsock libs
- // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "27015"
- //test sending getrequest to google server
- //int callGoogle(FILE* fn, SOCKET ConnectSocket)
- //{
- // //Send some data to google
- // char *message, server_reply[2000];
- // int recv_size;
- // message = "GET / HTTP/1.1\r\n\r\n";
- // if (send(ConnectSocket, message, strlen(message), 0) < 0)
- // {
- // fprintf(fn, "Send failed");
- // return 1;
- // }
- // puts("Data Send\n");
- //
- // //Receive a reply from the server
- // if ((recv_size = recv(ConnectSocket, server_reply, 2000, 0)) == SOCKET_ERROR)
- // {
- // fprintf(fn, "Recv failed");
- // return 1;
- // }
- //
- // fprintf(fn, "Reply received\n");
- //
- // //Add a NULL terminating character to make it a proper string before printing
- // server_reply[recv_size] = '\0';
- // fprintf(fn, server_reply);
- // return 0;
- //}
- int main(int argc, char *argv[])
- {
- WSADATA wsa;
- SOCKET ConnectSocket;
- FILE* fn;
- int iResult;
- struct addrinfo *result = NULL,
- *ptr = NULL;
- time_t rawtime;
- struct tm * timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- fn = fopen("D:\\log.txt", "a+");
- fprintf(fn, "-------------------------\n%s", asctime(timeinfo));
- fprintf(fn, "Initialising Winsock...\n");
- //-----------------------------
- // Initialiaze a WinSock
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- fprintf(fn, "Initialization socker failed with error: %d\n", WSAGetLastError());
- return 1;
- }
- fprintf(fn, "Winsock initialised.\n");
- ConnectSocket = INVALID_SOCKET;
- //----------------------
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (ConnectSocket == INVALID_SOCKET) {
- fprintf(fn, "Socket function failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- fprintf(fn, "Socket created.\n");
- // IPv4 AF_INET sockets:
- // Address Family : AF_INET(this is IP version 4)
- // Type : SOCK_STREAM(this means connection oriented TCP protocol)
- // Protocol : 0[or IPPROTO_TCP, IPPROTO_UDP]
- // The sockaddr_in structure specifies the address family,
- // IP address, and port of the server to be connected to.
- sockaddr_in clientService;
- clientService.sin_family = AF_INET;
- //google address
- //clientService.sin_addr.s_addr = inet_addr("216.58.214.206");
- clientService.sin_addr.s_addr = inet_addr("192.168.0.14");
- clientService.sin_port = htons(8888);
- //----------------------
- // Connect to server.
- iResult = connect(ConnectSocket, (SOCKADDR *)& clientService, sizeof(clientService));
- if (iResult == SOCKET_ERROR) {
- fprintf(fn, "Connection to remote server failed with error %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- return 1;
- }
- fprintf(fn, "Connected to remote server\n");
- //9bytes
- char *sendbuf = "T\0";
- char recvbuf[DEFAULT_BUFLEN];
- int recvbuflen = DEFAULT_BUFLEN;
- // Send an initial buffer
- iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR) {
- fprintf(fn, "Send failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- fprintf(fn, "Bytes Sent: %ld\n", iResult);
- // shutdown the connection since no more data will be sent
- // Shutdown send operations.
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- fprintf(fn, "Shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- fprintf(fn, "Shutdown send operations.\n");
- // Receive until the peer closes the connection
- do {
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- fprintf(fn, "Bytes received: %d\n", iResult);
- else if (iResult == 0)
- fprintf(fn, "Connection closed\n");
- else
- fprintf(fn, "Recv failed with error: %d\n", WSAGetLastError());
- } while (iResult > 0);
- // shutdown the connection since no more data will be sent
- // Shutdown recieve operations.
- iResult = shutdown(ConnectSocket, SD_RECEIVE);
- if (iResult == SOCKET_ERROR) {
- fprintf(fn, "Shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- fprintf(fn, "Shutdown recieve operations.");
- fprintf(fn, recvbuf);
- // cleanup
- closesocket(ConnectSocket);
- WSACleanup();
- fprintf(fn, "Connection closed\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement