Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Client.cpp
- //
- // Extremely simple, stream client example.
- // Works in conjunction with Server.cpp.
- //
- // The program attempts to connect to the server and port
- // specified on the command line. The Server program prints
- // the needed information when it is started. Once connected,
- // the program sends data to the server, waits for a response
- // and then exits.
- //
- // Compile and link with wsock32.lib
- //
- // Pass the server name and port number on the command line.
- //
- // Example: Client MyMachineName 2000
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <winsock2.h>
- // Function prototype
- void StreamClient(char *szServer, short nPort);
- // Helper macro for displaying errors
- #define PRINTERROR(s) \
- fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
- ////////////////////////////////////////////////////////////
- int main(int argc, char **argv)
- {
- WORD wVersionRequested = MAKEWORD(1,1);
- WSADATA wsaData;
- int nRet;
- short nPort;
- //
- // Check for the host and port arguments
- //
- if (argc != 3)
- {
- fprintf(stderr,"\nSyntax: client ServerName PortNumber\n");
- return -1;
- }
- nPort = atoi(argv[2]);
- //
- // Initialize WinSock and check the version
- //
- nRet = WSAStartup(wVersionRequested, &wsaData);
- if (wsaData.wVersion != wVersionRequested)
- {
- fprintf(stderr,"\n Wrong version\n");
- return -1;
- }
- //
- // Go do the stuff a stream client does
- //
- StreamClient(argv[1], nPort);
- //
- // Release WinSock
- //
- WSACleanup();
- system("pause");
- return 0;
- }
- ////////////////////////////////////////////////////////////
- void StreamClient(char *szServer, short nPort)
- {
- printf("\nStream Client connecting to server: %s on port: %d",
- szServer, nPort);
- //
- // Find the server
- //
- LPHOSTENT lpHostEntry;
- lpHostEntry = gethostbyname(szServer);
- if (lpHostEntry == NULL)
- {
- PRINTERROR("gethostbyname()");
- return;
- }
- //
- // Create a TCP/IP stream socket
- //
- SOCKET theSocket;
- theSocket = socket(AF_INET, // Address family
- SOCK_DGRAM, // Socket type
- IPPROTO_UDP); // Protocol
- if (theSocket == INVALID_SOCKET)
- {
- PRINTERROR("socket()");
- return;
- }
- //
- // Fill in the address structure
- //
- SOCKADDR_IN saServer;
- saServer.sin_family = AF_INET;
- saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
- // ^ Server's address
- saServer.sin_port = htons(nPort); // Port number from command line
- //
- // connect to the server
- //
- int nRet;
- //
- // Send data to the server
- //
- char szBuf[256];
- int a = sizeof(sockaddr);
- n strcpy(szBuf, "From the Client");
- nRet = sendto(theSocket, // Connected socket
- szBuf, // Data buffer
- strlen(szBuf), // Length of data
- 0,
- (LPSOCKADDR)&saServer,
- a); // Flags
- if (nRet == SOCKET_ERROR)
- {
- PRINTERROR("send()");
- closesocket(theSocket);
- return;
- }
- memset(szBuf, 0, sizeof(szBuf));
- //
- // Wait for a reply
- //
- nRet = recvfrom(theSocket, // Connected socket
- szBuf, // Receive buffer
- sizeof(szBuf), // Size of receive buffer
- 0, (sockaddr*)&saServer, &a); // Flags
- if (nRet == SOCKET_ERROR)
- {
- PRINTERROR("recv()");
- closesocket(theSocket);
- return;
- }
- //
- // Display the received data
- //
- printf("\nData received: %s", szBuf);
- closesocket(theSocket);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement