I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever was typed it. My problem is that this "client" code receives ONE message for the "server" and won't receive any more than that. I'm lost at this point. I've overcommented the code intentionally and took out all the error codes, because I wasn't getting any...there's no error, it's just not receiving anything more. If anyone can help me out, or tell me where I've gone wrong I would really appreciate it.
[CODE=cpp]
#include<stdio. h>
#include<winsoc k2.h>
#include<iostre am>
using namespace std;
#pragma comment(lib, "ws2_32.lib ")
#define STRING_MAX 2048
#define MAX 640000
#define MAX_CON 16
int nret;
SOCKET MySock;
char *client_send(ch ar *targetip, int port)
{
WSADATA wsaData;
WORD wVersionRequest ed;
wVersionRequest ed = MAKEWORD(2, 2);
if(WSAStartup (wVersionReques ted, &wsaData) != 0){ }
MySock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sock;
////////////////////////////////////////////////////////////////////////
//hostent --> used by functions to store information about a given host
struct hostent* target_ptr;
if((target_ptr= gethostbyname(t argetip)) == NULL){ }
memcpy(&sock.si n_addr, target_ptr->h_addr, target_ptr->h_length);
sock.sin_family = AF_INET;
sock.sin_port=h tons(port);
////////////////////////////////////////////////////////////////////////
if((connect(MyS ock, (struct sockaddr *)&sock, sizeof(sock) ))){ }
//connect --> establishes a connection to a specified socket
//MySock --> an unconnected socket
//(struct sockaddr *)&sock -- Name of the socket in the sockaddr structure to which
// the connection should be established
//sizeof(sock) -- Length of name, in bytes
char *recvString= new char[MAX];
nret = recv(MySock, recvString, MAX + 1, 0);
while(nret < 1)
{
nret = recv(MySock, recvString, MAX + 1, 0);
// printf("\n %d bytes received", nret);
}
//recv --> receives data from a connected or bound socket
//MySock -- identifies a connected socket
//recvString -- buffer for incoming data
//MAX + 1 -- The length, in bytes, of the buf parameter
//0 -- pointer flag
char *output= new char[nret];
strcpy(output, " ");
strncat(output, recvString, nret);
delete [ ] recvString;
closesocket(MyS ock);
WSACleanup();
return (output);
delete[ ] output;
}
int main(int argc, char *argv[])
{
int port = 888;
char* targetip;
char* output = NULL;
targetip = argv[1];
while(1)
{
printf("--> ");
printf("%s \n", client_send(tar getip, port));
}
}
[/CODE]
[CODE=cpp]
#include<stdio. h>
#include<winsoc k2.h>
#include<iostre am>
using namespace std;
#pragma comment(lib, "ws2_32.lib ")
#define STRING_MAX 2048
#define MAX 640000
#define MAX_CON 16
int nret;
SOCKET MySock;
char *client_send(ch ar *targetip, int port)
{
WSADATA wsaData;
WORD wVersionRequest ed;
wVersionRequest ed = MAKEWORD(2, 2);
if(WSAStartup (wVersionReques ted, &wsaData) != 0){ }
MySock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sock;
////////////////////////////////////////////////////////////////////////
//hostent --> used by functions to store information about a given host
struct hostent* target_ptr;
if((target_ptr= gethostbyname(t argetip)) == NULL){ }
memcpy(&sock.si n_addr, target_ptr->h_addr, target_ptr->h_length);
sock.sin_family = AF_INET;
sock.sin_port=h tons(port);
////////////////////////////////////////////////////////////////////////
if((connect(MyS ock, (struct sockaddr *)&sock, sizeof(sock) ))){ }
//connect --> establishes a connection to a specified socket
//MySock --> an unconnected socket
//(struct sockaddr *)&sock -- Name of the socket in the sockaddr structure to which
// the connection should be established
//sizeof(sock) -- Length of name, in bytes
char *recvString= new char[MAX];
nret = recv(MySock, recvString, MAX + 1, 0);
while(nret < 1)
{
nret = recv(MySock, recvString, MAX + 1, 0);
// printf("\n %d bytes received", nret);
}
//recv --> receives data from a connected or bound socket
//MySock -- identifies a connected socket
//recvString -- buffer for incoming data
//MAX + 1 -- The length, in bytes, of the buf parameter
//0 -- pointer flag
char *output= new char[nret];
strcpy(output, " ");
strncat(output, recvString, nret);
delete [ ] recvString;
closesocket(MyS ock);
WSACleanup();
return (output);
delete[ ] output;
}
int main(int argc, char *argv[])
{
int port = 888;
char* targetip;
char* output = NULL;
targetip = argv[1];
while(1)
{
printf("--> ");
printf("%s \n", client_send(tar getip, port));
}
}
[/CODE]
Comment