Winsock
I was wondering if someone could point me in the right direction to learn about winsock. Been looking for beginner tutorials on the net, but most of them are just CPP files, or pre compiled .exe or they just dont work.
Tried to follow this one, gets a bit confusing plus it doesnt compile correctly.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2241&lngWId=3
Btw, heres what i have so far:
#include <iostream>
using namespace std;
#include "winsock.h"
void main()
{
WSADATA wsaDat;
if(WSAStartup(MAKEWORD(1,1), &wsaDat) != 0)
{
cout << "WSA Initialization Failed." << endl;
}
SOCKET ListenSocket;
ListenSocket = (AF_INET, SOCK_STREAM, 0);
if(ListenSocket == INVALID_SOCKET)
{
cout << "Socket Creation Failed." << endl;
WSACleanup();
return;
}
SOCKADDR_IN service;
service.sin_port = htons(27015);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
Will
[1136 byte] By [
minor59er] at [2007-11-11 10:14:16]

# 5 Re: Winsock
.cpp file. Hope you can see something useful from this.
//Udp and tcpip class to wrap up the old style sockets in Visual studio.
//.NET has a new class for doing these tasks. I do not know if tcpip
//portion works or not. The client/server thing was not my
//idea but came from the examples -- honestly you can just open
//every socket to send and receive and have less complications.
//It works for our UDP needs and can be abandoned if we need
//something better (use the .net class).
#include "Udp-Tcp.h"
#include <iostream>
using namespace std;
void jcomm_t::jcomm_client_init(int portnum, char * host_name, bool is_udp)//func
{
// Notice that almost nothing in this code is specific to whether we
// are using UDP or TCP. The socket open command is the only place.
// When connect() is called on a udp socket, it does not
// actually establish the connection as a TCP socket
// would. Instead, TCP establishes the remote half of the
// ( LocalIPAddress, LocalPort, RemoteIP, RemotePort) mapping.
// This enables us to use send() and recv() on datagram sockets,
// instead of recvfrom() and sendto() -- making this a generic object
//suited for either tcp or udp transmission
port = portnum;
host = host_name;
udp = is_udp;
WSAStartup(0x202,&WSAData); //X202 is 514 decimal, it is the 'version required' of something or other.
if (isalpha(host[0])) //if its a Name or a number, we can open it either way
{
phostent = gethostbyname(host);
}
else
{
Addr = inet_addr(host);
phostent = gethostbyaddr((char *)&Addr,4,AF_INET);
}
if(phostent == NULL)
{
failed = true;
return;
}
failed = false;
memset(&server,0,sizeof(server));
memcpy(&(server.sin_addr),phostent ->h_addr,phostent ->h_length);
server.sin_family = phostent ->h_addrtype;
server.sin_port = htons(port);
if(udp)
WinSocket = socket(AF_INET,SOCK_DGRAM,0); // Open a socket udp style
else
{
WinSocket = socket(AF_INET,SOCK_STREAM,0); // Open a socket tcp style
}
/*
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY; //inet_addr("204.130.141.139"); //could be set to an ip address
local.sin_port = htons(port);
cout << local.sin_port << endl;
bool val = true;
setsockopt(WinSocket,IPPROTO_IP,SO_REUSEADDR, (char *) &val, sizeof(bool));
bind(WinSocket,(sockaddr*)&local,sizeof(local) ); */
connect(WinSocket ,(sockaddr*)&server,sizeof(server)) ;
/*
sockaddr getsa;
int namelen;
getsockname (WinSocket,&getsa, &namelen );
int x;
for(x = 0; x < namelen; x++)
printf("%x",getsa.sa_data[x]);
cout << endl; */
jcomm_show_error();
}
void jcomm_t::jcomm_client_send()//func
{
if(!failed)
send(WinSocket, data, size_of_datagram ,0);
}
void jcomm_t::jcomm_server_init(int portnum, bool is_udp)//func
{
port = portnum;
udp = is_udp;
WSAStartup(0x202,&WSAData);
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY; //could be set to an ip address
local.sin_port = htons(port);
if(udp)
WinSocket= socket(AF_INET, SOCK_DGRAM ,0); //udp
else
WinSocket= socket(AF_INET, SOCK_STREAM,0); //tcp
bind(WinSocket,(sockaddr*)&local,sizeof(local) );
// We cannot listen() on a UDP socket.
fromlen =sizeof(from);
if (!udp)
{
listen(WinSocket,100);
tcsocket = accept(WinSocket,(struct sockaddr*)&from, &fromlen);
inet_ntoa(from.sin_addr),
htons(from.sin_port) ;
}
else
tcsocket = WinSocket;
}
UINT jcomm_tcpip_thread(LPVOID pointer)//func
{
jcomm_t * pj = (jcomm_t *) pointer;
listen(pj->WinSocket,5);
pj->fromlen =sizeof(pj->from);
pj->tcsocket = accept(pj->WinSocket,(sockaddr*)&(pj->from), &(pj->fromlen));
inet_ntoa(pj->from.sin_addr);
htons(pj->from.sin_port) ;
/*
for(;;)
{
recv(pj->tcsocket,pj->data,size_of_datagram,0 );
//process data here
}
*/
return 0;
}
UINT jcomm_udp_thread(LPVOID pointer)//func
{
jcomm_t * pj = (jcomm_t *) pointer;
for(;;)
{
recvfrom(pj->tcsocket,pj->data,size_of_datagram,0,
(sockaddr *)&(pj->from),&(pj->fromlen));
//process data here
}
return 0;
}
void jcomm_t::jcomm_server_start()//func
{
if(udp)
AfxBeginThread(jcomm_udp_thread, this, THREAD_PRIORITY_LOWEST, 0, 0, NULL);
else
AfxBeginThread(jcomm_tcpip_thread, this, THREAD_PRIORITY_LOWEST, 0, 0, NULL);
}
void jcomm_t::jcomm_cleanup()//func
{
closesocket(WinSocket);
WSACleanup();
}
bool jcomm_t::jcomm_show_error()//func
{
//removed function to print name of getlasterror enum to make it fit
}
jonnin at 2007-11-11 21:03:14 >

# 6 Re: Winsock
.h file
/*simple udp and tcp library
This is supposed to be able to perform most udp and tcp functions.
I am not sure how much of tcp works anymore; UDP is working well.
Much here is simply for example -- apps need more customization than the
objects allow. I define a server as one who waits for a connection and listens
while a client connects and talks.
TODO: Add, at least in example form, the code we have discovered for
broadcast groups, non-blocking sockets, one socket 2-way comm, sendto
*/
#ifndef jcomm_file
#define jcomm_file
#define WIN32_LEAN_AND_MEAN
#ifdef TARGET_OS_WIN2K
typedef int socklen_t;
#include <afxwin.h> //threads
#include <winsock.h> //this one messes up in .net, the other in 6.0 so whatever
//#include <winsock2.h> //the example included this but it causes problems!
#endif
#ifdef TARGET_OS_LINUX //fedora -- others??
#include <sys/socket.h>
#include <netinet/in.h> //a list of protocols
#include <arpa/inet.h> //who knows what an arpa is (retired person?)
#include <netdb.h> //no one knows what this is either.
#include <sys/ioctl.h> //for non blocking control stuff.
#include <errno.h>
#define ioctlsocket ioctl
#endif
typedef float float_data_t[1024]; //you could make this something else, a struct, etc.
const int size_of_datagram = 4096;
//I assume here that a float is 4 bytes ! seems fair as this stuff needs visual studio to compile...
//tc uses send to send from server back to client, udp uses sendto, I did not have a use for this yet.
struct jcomm_t
{
char *host;
unsigned int port;
char data[size_of_datagram];
bool udp; //udp or tcpip?
bool failed; //gracefully deal with bad address
void jcomm_t::jcomm_client_init(int portnum, char * host_name, bool is_udp);
void jcomm_t::jcomm_client_send(); //example mostly (use sendto instead)
void jcomm_t::jcomm_server_init(int portnum, bool is_udp);
void jcomm_t::jcomm_server_start(); //example do not use
void jcomm_t::jcomm_cleanup(); //?? it may break all sockets not just this one
static bool jcomm_show_error(); //the help text for each code is printed
sockaddr_in server, local, from;
jcomm_t(){failed = true;}
#ifdef TARGET_OS_WIN2K
PHOSTENT phostent;
WSADATA WSAData;
SOCKET WinSocket,tcsocket;
#else
// hostent *phostent;
int WinSocket,tcsocket;
#endif
unsigned int Addr;
int fromlen;
};
#endif
jonnin at 2007-11-11 21:04:19 >
