// JP opened flex table

Click to See Complete Forum and Search --> : Winsock buffer problem


luckycharms
November 28th, 2007, 12:33 AM
CLIENT:

#pragma comment (lib, "ws2_32")
#include <windows.h>
#include <iostream>

#define DEBUG

int main()
{
char buf[1024];
char msgbuf[1024];
int port = 808;
SOCKET sock;
int len;

// WSAData Declaration
WSAData wsaData;

// Let's use WinSock 2.0
WORD wsver=MAKEWORD(2, 0);

// Version and Pointer
int nret=WSAStartup(wsver, &wsaData);

// Init Fail or Not
// Not = 0
if(nret != 0)
{
#ifdef DEBUG
std::cout << "Startup failed.\n"
// Whats the error code ?
<< "Error code: "
<< WSAGetLastError();
#endif
// Let's Cleanup Winsock library
WSACleanup();
return -1;
}

// Did we get the right Winsock Version ?
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
// NOPE
#ifdef DEBUG
std::cout << "Wrong Winsock Version!";
#endif
WSACleanup();
return FALSE;
}

// Winsock V2.0 Initialized!
#ifdef DEBUG
std::cout<<"Initialization OK!\n";
#endif

// Socket Creation with AF_INET Family
sock=socket(AF_INET, SOCK_STREAM, 0);

// Is Socket OK ?
if(sock == INVALID_SOCKET)
{
#ifdef DEBUG
std::cout<<"Socket init failed";
#endif
return -1;
}

// Socket is Fine.
#ifdef DEBUG
std::cout<<"Socket initialized\n";
#endif

// Define out sockaddr struct
sockaddr_in ssin;
ssin.sin_port=htons(port);
ssin.sin_addr.s_addr=inet_addr("127.0.0.1");
ssin.sin_family=AF_INET;


if(connect(sock,(sockaddr*)&ssin, sizeof(ssin)) == SOCKET_ERROR)
{
std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup the library
return -1;
}

#ifdef DEBUG
std::cout<<"Connection established!\n";
#endif

while (1)
{
std::cin >> buf;
send(sock, buf, sizeof(buf), 0);
}

// Close both Sockets
closesocket(sock);
closesocket(sock);

// Cleanup again
WSACleanup();

return 0;
}


SERVER:
#pragma comment (lib, "ws2_32")
#include <windows.h>
#include <iostream>

#define DEBUG
#define WIN32_LEAN_AND_MEAN



int main()
{
char buf[1024], *x, line[1024];
int ListenPort = 808;
SOCKET client,
kListen;
int len;

// WSAData Declaration
WSAData wsaData;

// Let's use WinSock 2.0
WORD wsver=MAKEWORD(2, 0);

// Version and Pointer
int nret=WSAStartup(wsver, &wsaData);

// Init Fail or Not
// Not = 0
if(nret != 0)
{
#ifdef DEBUG
std::cout << "Startup failed.\n"
// Whats the error code ?
<< "Error code: "
<< WSAGetLastError();
#endif
// Let's Cleanup Winsock library
WSACleanup();
return -1;
}

// Did we get the right Winsock Version ?
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
// NOPE
#ifdef DEBUG
std::cout << "Wrong Winsock Version!";
#endif
WSACleanup();
return FALSE;
}

// Winsock V2.0 Initialized!
#ifdef DEBUG
std::cout<<"Initialization OK!\n";
#endif

// Socket Creation with AF_INET Family
kListen=socket(AF_INET, SOCK_STREAM, 0);

// Is Socket OK ?
if(kListen == INVALID_SOCKET)
{
#ifdef DEBUG
std::cout<<"Socket init failed";
#endif
return -1;
}

// Socket is Fine.
#ifdef DEBUG
std::cout<<"Socket initialized\n";
#endif

// Define out sockaddr struct
sockaddr_in ssin;
ssin.sin_port=htons(ListenPort);
ssin.sin_addr.s_addr=INADDR_ANY;
ssin.sin_family=AF_INET;

if(bind(kListen,(sockaddr*)&ssin, sizeof(ssin)) == SOCKET_ERROR)
{
#ifdef DEBUG
std::cout<<"Failed to bind\n";
#endif
// Cleanup Winsock library
WSACleanup();
return -1;
}

#ifdef DEBUG
std::cout<<"Bind successful!\n";
#endif

len = sizeof(ssin);
while (1) // Common Loop-Forever
{
if (listen(kListen, 10) == SOCKET_ERROR) { return 0; }
client = accept(kListen, (SOCKADDR *)&ssin, &len);
// If client connects, we break out of the Forever Loop
if (client != INVALID_SOCKET) { break; }
}

#ifdef DEBUG
std::cout<<"Connection established!\n";
#endif




char a[256][1024];
int q = 0;



while (1)
{
memset(buf, 0, sizeof(buf));
if (recv(client, buf, sizeof(buf), 0) < 0) { break; }
std::cout << "buf is :" << buf << std::endl;
}

// Close both Sockets
closesocket(client);
closesocket(kListen);

// Cleanup again
WSACleanup();

return 0;
}

runtime:

client:
a b c <enter>

server:
buf is: a
buf is: b
buf is: c


How come its treating the "space" as newline ?

luckycharms
November 28th, 2007, 01:01 AM
lots of views, but no answers. sorry if im being impatient. anyone ?

cilu
November 28th, 2007, 02:04 AM
it doesn't see the space as new line, but data read with cin (strings, ints, chars, etc.) are delimited with white spaces. If you want to read "a b c" as a string, use getline() function.

luckycharms
November 28th, 2007, 02:51 AM
thanks a bunch!

MikeAThon
November 28th, 2007, 05:53 PM
In addition, TCP is a stream protocol, not a messaging protocol. So even if your client sent a single string, it's possilbe that your server will need to call recv() multiple times before all characters in the string are received.

Mike

Marc G
November 29th, 2007, 04:45 AM
[ moved thread ]

//JP added flex table