Here is my code.

#include <iostream>
#include <math.h>
#include <winsock.h>
#include <windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


using namespace std;

int main()
{
SOCKET s;
WSADATA wsadata;

    int error = WSAStartup(0x0202, &wsadata);

    //Did something happen?
    if (error)
        return false;

    //Did we get the right Winsock version?
    if (wsadata.wVersion != 0x0202)
    {
        WSACleanup(); //Clean up Winsock
        return false;
    }

    //Fill out the information needed to initialize a socket…
    SOCKADDR_IN target; //Socket address information

    target.sin_family = AF_INET; // address family Internet
    target.sin_port = htons (53000); //Port to connect on
    target.sin_addr.s_addr = inet_addr ("192.168.29.1"); //Target IP

    s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket

    if (s == INVALID_SOCKET)
    {
        return false; //Couldn't create the socket
    }

    //Try connecting...

    if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
    {
        return false; //Couldn't connect
    }

    while(1)
    {
    memset(buffer,'0',255);
    cin>>buffer;

    int n=send(s,buffer,strlen(buffer),0);

    if(n<0)
        cout<<endl<<"problem sending"<<endl;

    }
    closesocket(s);

return 0;
}

In the above code the problem is that the values are sent only when the program is closed, before that even though the connection is getting established, at the receiving end no values are read.

If i write the same program as

while(1)
{
    connect
    send
    closesocket
}

then it poses absolutely no problem, except the fact that connection is established again and again which results in a certain delay which is not desireable.

In the above code the problem is that the values are sent only when the program is closed, before that even though the connection is getting established, at the receiving end no values are read.

I can't recreate your problem. I've used your posted code to connect to a server without any problems. The only issue I found while compiling your posted code is that the buffer variable was not declared. I would suggest that you post the code being used at the "receiving end" for review.

This article has been dead for over six months. Start a new discussion instead.