Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #include <unistd.h>
- //Must add followin line to linker parameters: -lws2_32
- //links the winsock 2 library
- //#define PORT 5000
- #define NETWORK_ERROR -1
- #define BUFFERSIZE 1
- int main(int argc, char *argv[])
- {
- //basic declarations
- SOCKET clientSocket;
- SOCKADDR_IN ipAddr;
- WSADATA wsaData;
- int status;
- int port;
- char *dataBuffer = argv[3];
- char readBuffer[BUFFERSIZE];
- //readBuffer[BUFFERSIZE] = *"";
- port = strtoul(argv[2],NULL,0);
- // WSAStartup function initiates use of WS2_32.DLL by a process
- // MAKEWORD(2,2) - use version 2.2 of winsockets
- if((status = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
- {
- printf("failed startup\n");
- WSACleanup();
- return NETWORK_ERROR; //starup failed
- }
- if((clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
- {
- printf("invalid socket...");
- WSACleanup();
- return NETWORK_ERROR;
- }
- ipAddr.sin_family = AF_INET;
- ipAddr.sin_port = htons(port);
- ipAddr.sin_addr.s_addr = inet_addr(argv[1]);
- if((connect(clientSocket, (SOCKADDR*)&ipAddr, sizeof(SOCKADDR_IN))) != 0)
- {
- printf("could not connect...\n");
- WSACleanup();
- return NETWORK_ERROR;
- }
- status = send(clientSocket, dataBuffer, strlen(dataBuffer), 0);
- if(status < strlen(dataBuffer))
- printf("data not sent...\n");
- printf("Message received:\n");
- while (recv(clientSocket, readBuffer, BUFFERSIZE, 0) > 0)
- {
- write(1,readBuffer, 1);
- }
- printf("\n");
- closesocket(clientSocket);
- WSACleanup();
- return 0;
- }
Add Comment
Please, Sign In to add comment