🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Winsock

Started by
2 comments, last by klubbkid 23 years, 9 months ago
I''m currently seeking a way to send strings back and forth between a server and a client. I''m hoping for TCP/IP, but am having trouble finding resources on how to do this...any help would be great! - Derek
Advertisement
Well, if you''re using TCP, you won''t get the benefit of message boundaries, so you''ll have to do some buffering / parsing on the receiving side, but here''s some pseudo-code for a TCP Server / Client:



//SERVER

SOCKET hListen;
hListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(hListen == INVALID_SOCKET)
return false;

if(bind(hListen, htons(0x45)) == SOCKET_ERROR)
return false;

if(listen(hListen, SOMAXCONN) == SOCKET_ERROR)
return false;

SOCKET hClient = accept(hListen, NULL, NULL);
if(hClient == INVALID_SOCKET)
return false;

char *pBuf = _T("Hello Mr. Client\n");
int cbBuf = strlen(pBuf);

if(send(hClient, pBuf, cbBuf, 0) == SOCKET_ERROR)
return false;

if(close(hClient) == SOCKET_ERROR)
return false;

if(close(hListen) == SOCKET_ERROR)
return false;



//CLIENT

char *pHost = _T("127.0.0.1");

SOCKET hServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(hServer == INVALID_SOCKET)
return false;

u_long lHost = inet_addr(pHost);
SOCKADDR_IN sinHost;
ZeroMemory(&sinHost, sizeof(SOCKADDR_IN));

sinHost.sin_family = AF_INET;
sinHost.sin_port = htons(0x45);

if(lHost != INADDR_NONE){
sinHost.sin_addr.S_un.S_addr = lHost;
}else{
LPHOSTENT phe = gethostbyname(pHost);
if(phe == NULL)
return false;
sinHost.sin_addr.S_un.S_addr = phe->h_addr_list[0];
}

if(connect(hServer, (LPSOCKADDR)&sinHost, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
return false;

char *pBuf = new char[256];
ZeroMemory(pBuf, 256);
int cbBuf = 256;

int nRet = recv(hServer, pBuf, cbBuf, 0);

if(nRet == 0 || nRet == SOCKET_ERROR)
return false;

//BIG assumption here, that we received all data in one recv
//Without message boundaries being preserved, that won''t always
//Be the case

printf(pBuf);

if(closesocket(hServer) == SOCKET_ERROR)
return false;

Ok, so that wasn''t very pseudo. =)

Anyway, that code will set up a listening socket on the server and wait for a connection. When a client conencts, it will send it a string of text and close the socket.

On the client, it connects to the local machine and receives data. It only does one receive, when in reallity you would have to loop on Receive with some kind of buffer until you found a newline character.

If you want another example, look for winsock code for an echo server/client. That should have a good example also.
Well, if you''re desperate for making a winsock app, then just use a winsock control. MSWINSCK.OCX worked for me. I''m currently running a server using that control.

---------------------------
"Don't die for your country, make some other dumb bastard die for his" -General George S. Patton

This topic is closed to new replies.

Advertisement