1. DO these work?
2. Do i need to loop RECV or will it keep recv till it recvs a buffer?
#include <winsock.h>
#include <stdio.h>
#include <iostream>
int main(int argc, char** argv) {
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
char* IP=new char[32];
std::cin>>IP;
//
// Start up Winsock as before
//
WSAStartup(version, &wsaData);
//
// Store information about the server
//
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(IP); // Specifying server by its name
if (lpHostEntry == NULL) {
printf("Error at gethostbyaddr()\n");
WSACleanup();
system("pause");
return 0;
}
//
// Create the socket
//
SOCKET theSocket;
theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (theSocket == INVALID_SOCKET) {
printf("Error at socket()\n");
WSACleanup();
system("pause");
return 0;
}
//
// Use SOCKADDR_IN to fill in address information
//
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Address of the server being inserted into the address field
saServer.sin_port = htons(80);
//
// Connect to the server
//
nRet = connect(theSocket,
(LPSOCKADDR)&saServer, // Server address
sizeof(struct sockaddr)); // Length of address structure
if (nRet == SOCKET_ERROR) {
printf("Error at connect()");
WSACleanup();
system("pause");
return 0;
}else{
printf("Connected \n");
}
// Successfully connected!
printf("\nReceiveing...\n");
char* myRBuf = new char[256];
// strcpy(myRBuf, "hahahahaahhahahaaa" );
HRESULT res;
res = recv(theSocket, // Connected socket
myRBuf, // Receive buffer
sizeof(myRBuf), // Size of the buffer
0);
while( !res )
{
res = recv(theSocket, // Connected socket
myRBuf, // Receive buffer
sizeof(myRBuf), // Size of the buffer
0);
}
if(myRBuf)
printf(myRBuf);
closesocket(theSocket);
printf("\nDisconnected \n");
//
// Shutdown Winsock
//
WSACleanup();
system("pause");
return 0;
}
and:
#include <winsock.h>
#include <stdio.h>
#include <iostream>
int main(int argc, char** argv) {
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
char* IP=new char[32];
std::cin>>IP;
//
// Start up Winsock as before
//
WSAStartup(version, &wsaData);
//
// Store information about the server
//
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(IP); // Specifying server by its name
if (lpHostEntry == NULL) {
printf("Error at gethostbyaddr()\n");
WSACleanup();
system("pause");
return 0;
}
//
// Create the socket
//
SOCKET theSocket;
theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (theSocket == INVALID_SOCKET) {
printf("Error at socket()\n");
WSACleanup();
system("pause");
return 0;
}
//
// Use SOCKADDR_IN to fill in address information
//
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Address of the server being inserted into the address field
saServer.sin_port = htons(80);
//
// Connect to the server
//
nRet = connect(theSocket,
(LPSOCKADDR)&saServer, // Server address
sizeof(struct sockaddr)); // Length of address structure
if (nRet == SOCKET_ERROR) {
printf("Error at connect()");
WSACleanup();
system("pause");
return 0;
}else{
printf("Connected \n");
}
// Successfully connected!
printf("\nReceiveing...\n");
char* myRBuf = new char[256];
// strcpy(myRBuf, "hahahahaahhahahaaa" );
HRESULT res;
res = recv(theSocket, // Connected socket
myRBuf, // Receive buffer
sizeof(myRBuf), // Size of the buffer
0);
while( !res )
{
res = recv(theSocket, // Connected socket
myRBuf, // Receive buffer
sizeof(myRBuf), // Size of the buffer
0);
}
if(myRBuf)
printf(myRBuf);
closesocket(theSocket);
printf("\nDisconnected \n");
//
// Shutdown Winsock
//
WSACleanup();
system("pause");
return 0;
}
Plz help!