SHARE
TWEET

Untitled

a guest Dec 13th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock2.h>
  4. #include <unistd.h>
  5.  
  6. //Must add followin line to linker parameters: -lws2_32
  7. //links the winsock 2 library
  8.  
  9. //#define PORT 5000
  10. #define NETWORK_ERROR -1
  11. #define BUFFERSIZE 1
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     //basic declarations
  16.     SOCKET clientSocket;
  17.     SOCKADDR_IN ipAddr;
  18.     WSADATA wsaData;
  19.     int status;
  20.     int port;
  21.     char *dataBuffer = argv[3];
  22.     char readBuffer[BUFFERSIZE];
  23.     //readBuffer[BUFFERSIZE] = *"";
  24.      
  25.     port = strtoul(argv[2],NULL,0);
  26.  
  27.     // WSAStartup function initiates use of WS2_32.DLL by a process
  28.     // MAKEWORD(2,2) - use version 2.2 of winsockets
  29.     if((status = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
  30.     {
  31.                printf("failed startup\n");
  32.                WSACleanup();
  33.                return NETWORK_ERROR; //starup failed
  34.     }
  35.    
  36.     if((clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
  37.     {
  38.                printf("invalid socket...");
  39.                WSACleanup();
  40.                return NETWORK_ERROR;              
  41.     }
  42.    
  43.     ipAddr.sin_family = AF_INET;
  44.     ipAddr.sin_port = htons(port);
  45.     ipAddr.sin_addr.s_addr = inet_addr(argv[1]);
  46.    
  47.     if((connect(clientSocket, (SOCKADDR*)&ipAddr, sizeof(SOCKADDR_IN))) != 0)
  48.     {
  49.                printf("could not connect...\n");
  50.                WSACleanup();
  51.                return NETWORK_ERROR;                          
  52.     }
  53.  
  54.     status = send(clientSocket, dataBuffer, strlen(dataBuffer), 0);
  55.      
  56.     if(status < strlen(dataBuffer))
  57.               printf("data not sent...\n");
  58.    
  59.    
  60.    
  61.     printf("Message received:\n");
  62.     while (recv(clientSocket, readBuffer, BUFFERSIZE, 0) > 0)
  63.     {
  64.               write(1,readBuffer, 1);
  65.     }
  66.     printf("\n");
  67.    
  68.              
  69.     closesocket(clientSocket);
  70.     WSACleanup();
  71.    
  72.   return 0;
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top