Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock Programming

Status
Not open for further replies.

Harlequinn

Programmer
Oct 17, 2003
4
GB
I was wondering if anyone would be able to point me to some good winsock tutorials for use in C++?
I have searched the net and have come up with very little. I only need an explination of the syntax as im familiar with the concepts involved.
 
this is the code of simpliest chat program, in console mode:
Code:
////////////client////////////////
#include<io.h>
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
#define PORTNUM 888
#include<windows.h>
bool run = true;
DWORD WINAPI ThreadProcToSvr(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	while(1)
	{
		int nbytes = recv(ns, buf, sizeof(buf), 0);
		printf(&quot;%s\n&quot;, buf);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
DWORD WINAPI ThreadProcCln(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	HANDLE hThreadCln = 0;
	ULONG th_id;
	while(1)
	{
		gets(buf);
		send(ns, buf, sizeof(buf), 0);
		if(!hThreadCln)hThreadCln = CreateThread(0, 0, ThreadProcToSvr, (void*)&ns, 0, &th_id);
		if(!hThreadCln){printf(&quot;error on creating thread\n&quot;);exit(1);}
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}

int main(HINSTANCE, HINSTANCE, LPSTR, int)
{
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSADATA wsd;
	WSAStartup(wVersionRequested, &wsd);

	int s;//SOCKET

	sockaddr_in svr_addr;//sei-filipski
	hostent *hp;
	char buf[80] = &quot;salut la toti&quot;;
	if((hp = gethostbyname(&quot;hostname&quot;)) == 0)
	{
		printf(&quot;error on calling gethostbyname()\n&quot;);
		exit(1);
	}
	strncpy((char*)&svr_addr.sin_addr.S_un.S_un_b, hp->h_addr_list[0], hp->h_length);


	svr_addr.sin_family = hp->h_addrtype;
	svr_addr.sin_port = htons((u_short)PORTNUM);
	if((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		printf(&quot;error on creating socket\n&quot;);
		exit(1);
	}
	if(connect(s, (const sockaddr*)&svr_addr, sizeof(svr_addr)) == -1)
	{
		printf(&quot;error on calling connect()\n&quot;);
		exit(1);
	}
	ULONG th_id;
	HANDLE hThreadSvr = 0;
	hThreadSvr = CreateThread(0, 0, ThreadProcCln, (void*)&s, 0, &th_id);
	if(!hThreadSvr){printf(&quot;error on creating thread&quot;);exit(1);}
	while(run);
	close(s);
	return 0;
}
////////////client////////////////
////////////server////////////////
#include<conio.h>
#include<io.h>
#include<stdlib.h>
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
//WS2_32.Lib required
#define PORTNUM 888;
//server
bool run = true;
DWORD WINAPI ThreadProcCln(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	while(1)
	{
		gets(buf);
		send(ns, buf, sizeof(buf), 0);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
DWORD WINAPI ThreadProcSvr(void* xx)
{
	int ns = *(int*)xx;
	char buf[80];
	HANDLE hThreadCln = 0;
	ULONG th_id;
	while(1)
	{
		int nbytes = recv(ns, buf, sizeof(buf), 0);
		if(!hThreadCln)hThreadCln = CreateThread(0, 0, ThreadProcCln, (void*)&ns, 0, &th_id);
		if(!hThreadCln){printf(&quot;error on creating thread\n&quot;);exit(1);}
		printf(&quot;%s\n&quot;, buf);
		if(strcmp(&quot;exit&quot;, buf) == 0)break;
	}
	run = false;
	return 0;
}
int main()
{
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSADATA wsd;
	WSAStartup(wVersionRequested, &wsd);
	int s, ns;
	int nport;
	nport = PORTNUM;
	nport = htons((u_short)nport);
	sockaddr_in svr_addr, cln_addr;
	s = socket(AF_INET, SOCK_STREAM, 0);
	if((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
	{
		printf(&quot;error on calling socket()\n&quot;);
		exit(1);
	}
	svr_addr.sin_family = AF_INET;
	svr_addr.sin_addr.S_un.S_addr = (u_long)INADDR_ANY;
	svr_addr.sin_port = nport;
	if(bind(s, (const sockaddr*)&svr_addr, sizeof(svr_addr)) == -1)
	{
		printf(&quot;error on calling bind()&quot;);
		exit(1);
	}
	printf(&quot;server ready: %s\n&quot;, inet_ntoa(svr_addr.sin_addr));
	if(listen(s, 5) == -1)
	{
		printf(&quot;error on calling listen()\n&quot;);
		exit(1);
	}

	while(1)
	{
		int addrlen;
		addrlen = sizeof(cln_addr);
		if((ns = accept(s, (sockaddr*)&cln_addr, &addrlen)) == -1)
		{
			printf(&quot;error on calling accept\n&quot;);
			exit(1);
		}
		printf(&quot;client: %s\n&quot;, inet_ntoa(cln_addr.sin_addr));
		int nbytes;
		char buf[80];
		ULONG th_id;
		HANDLE hThreadSvr = 0;
		hThreadSvr = CreateThread(0, 0, ThreadProcSvr, (void*)&ns, 0, &th_id);
		if(!hThreadSvr){printf(&quot;error on creating thread&quot;);exit(1);}
		while(run);
		close(ns);
		close(s);
		exit(0);
	}
	return 0;
}
////////////server////////////////

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top