I've tried using Dsock. It would work if I could make Turbo C++ work. However what i'm using is DJGPP. Are there any socket libraries for DJGPP out there? I tried to search for some via google, but couldn't find any. Also I have tried this. However it doesn't compile.
Code:
#include <windows.h>
#include <windowsx.h>
#include <winsock.h>
#include "tcp.h"
/*
* init_winsock() Will try to initialize winsock 2.0 and returns true if it succeeds
* connect_to_host(host,port) Creates a socket, sets the options for non-blocking, and connects
it returns true if it succeeds and error codes defined in the header
if it fails (a check for < 0 works too)
read_from_socket(buf, size) Reads from the socket if data is ready and stores it in the buffer
provided. Size should be the size of the char array being used is
dimensioned to. It returns false if no data is ready or an error
occurs. It returns -1 if a lost connection is detected
write_to_socket(buf) Wrapper to send() to the socket, the entire buffer is written
shutdown_winsock() Closes the socket and uninitializes winsock
close_connection() Wrapper for closesocket()
*/
SOCKET sock;
void close_connection(void)
{
closesocket(sock);
}
int init_winsock(void)
{
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
wVersionRequested = MAKEWORD(1,1);
WSACleanup();
WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
return false;
}
return true;
}
int connect_to_host(char *hostname, int port)
{
LPHOSTENT lpHostEntry;
SOCKADDR_IN saServer;
int arg;
unsigned long arg2;
// Perform DNS lookup
lpHostEntry = gethostbyname(hostname);
if (lpHostEntry == NULL)
{
return DNS_FAILURE;
}
// Create the socket
sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
// set the recv buf to 4k
arg = 4096;
setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *) &arg,sizeof(arg));
// set the send buf to 1k
arg = 1024;
setsockopt(sock, SOL_SOCKET,SO_SNDBUF, (char *) &arg, sizeof(arg));
if (sock == INVALID_SOCKET)
{
return SOCKET_FAILURE;
}
// Connect to host
saServer.sin_family = PF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(port);
if ( connect(sock,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr)) == SOCKET_ERROR)
{
closesocket(sock);
return CONNECT_FAILURE;
}
// Turn off blocking mode
ioctlsocket(sock,FIONBIO,&arg2);
return true;
}
int read_from_socket(char * buf, int bufsize)
{
int cnt;
// unsigned long arg;
// if (ioctlsocket(sock, FIONREAD, &arg) == SOCKET_ERROR)
// return -1;
// if (arg <= 0) return false;
cnt = recv(sock,buf, bufsize,0);
if (cnt == SOCKET_ERROR || cnt == 0)
{
int error;
error = WSAGetLastError();
// Might should check for other error codes too, but these seem most likely
// to indicate lost connection
if (error == WSAENETRESET ||
error == WSAECONNABORTED ||
error == WSAECONNRESET ||
error == WSAEINVAL ||
cnt == 0)
return -1;
return false;
}
buf[cnt] = '\0';
return true;
}
void write_to_socket(char * buf)
{
send(sock, buf, strlen(buf),0);
return;
}
void shutdown_winsock(void)
{
// cleanup winsock
closesocket(sock);
WSACleanup();
return;
}
Something to do with parse errors in winsock.h
Does that mean I need to find a new winsock.h?