Starting WINSOCK - UltraNewbie Question

Started by
9 comments, last by OrthoDiablo 19 years, 7 months ago
Go here Out of curiosity, I decided to look at a WinSock tutorial. The tutorial is from MSDN, and can be found here. My question has to do with the pure basics. Where can I connect to for learning? Is it possible to network to... myself? Here's my basic WinSock App:

#include <stdio.h>
#include "winsock2.h"

int main()
{
	// Make WSA
	WSADATA wsaData;	
	int iResult = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
	if( iResult != NO_ERROR )
	{
		printf( "Error at WSAStartup()\n" );
		return 0;
	}

	// Make a socket
	SOCKET m_socket;
	m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
	if( m_socket == INVALID_SOCKET )
	{
		printf( "Error at socket(): %ld\n", WSAGetLastError() );
		WSACleanup();
		return 0;
	}

	// Bind the socket
	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
	service.sin_port = htons( 27015 );
	if( bind( m_socket, (SOCKADDR*)&service, sizeof( service ) ) == SOCKET_ERROR )
	{
		printf( "bind() failed.\n" );
		closesocket( m_socket );
		return 0;
	}

	// Listen on the socket
	if( listen( m_socket, 1 ) == SOCKET_ERROR )
	{
		printf( "Error listening on socket.\n" );
		return 0;
	}

	// Accept the connection
	SOCKET AcceptSocket;
	printf( "Waiting for a client to connect...\n" );
	while( 1 )
	{
		AcceptSocket = SOCKET_ERROR;
		while( AcceptSocket == SOCKET_ERROR )
			AcceptSocket = accept( m_socket, NULL, NULL );
		printf( "Client connected.\n" );
		m_socket = AcceptSocket;
		break;
	}

	// Connecting to the socket
	sockaddr_in clientService;
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
	clientService.sin_port = htons( 27015 );
	if( connect( m_socket, (SOCKADDR*)&clientService, sizeof( clientService ) ) == SOCKET_ERROR )
	{
		printf( "Failed to connect().\n" );
		WSACleanup();
		return 0;
	}

	// Sending and receiving data
	int bytesSent;
	int bytesRecv = SOCKET_ERROR;
	char sendbuf[ 32 ] = "Server: Sending Data.";
	char recvbuf[ 32 ] = "";
	bytesRecv = recv( m_socket, recvbuf, 32, 0 );
	printf( "Bytes Recv: %ld\n", bytesRecv );
	bytesSent = send( m_socket, sendbuf, strlen( sendbuf ), 0 );
	printf( "Bytes Sent: %ld\n", bytesSent );

	return 0;
}



However, it has nowhere to connect to. As far as I can tell, I'm connecting to 127.0.0.1. As far as I heard, that IP is special in some way, is it not? Anyways, I'd like to make little app's such as this one for learning. But where should I connect to? How do YOU learn WinSock? EDIT: Forgot to mention, all my program does is display "Waiting for a client to connect...". Who can this client be for learning purposes? [Edited by - v0dKA on September 19, 2004 1:04:57 PM]
.:<<-v0d[KA]->>:.
Advertisement
1) 127.0.0.1 is you.
2) You can try your programs by writing a client and a server and use connect on the same port on IP 127.0.0.1
Quote:Original post by Anonymous Poster
1) 127.0.0.1 is you.
2) You can try your programs by writing a client and a server and use connect on the same port on IP 127.0.0.1


So for my programs to be able to connect to each other, I should run both of them at once? And that would be networking in action?
.:<<-v0d[KA]->>:.
That's the idea. It's how internet connections work... A client application connects to a server application, whether they're on the same computer or not. When you connect to a website, that's basically your browser connecting with Apache or some other server application... And it's the same deal with any server and / or client you make.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Just remember to run the server first...
Winsock Multi-Client Servers

A tut about handling multiple clients on Win32 servers, should be intresting..
______________________________________________________________________________________________________
[AirBash.com]
127.0.0.1 is also known as the 'loopback' address, cos your network card instead of firing packets outwards just spits them back up to your software.

Sometimes it's easier to just refer to it as 'localhost' - that's equivalent to 127.0.0.1 but easier to type :)
---PS3dev
OK, and I'm also worried a bit about security here, too. Does networking open me up to some bored hackers to gain access to my computer? If yes, how can it be prevented?
.:<<-v0d[KA]->>:.
Only if your server is insecure, really. You want to make sure of course, that there's no way someone can make your server do something it shouldn't... Either by giving it commands that might not do what you'd want them to do, or by overflows that could allow other problems. It's similar to the question I asked here and I bet the answers there will help you too.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Are there any articles on the matter?

How does this work? When I set up a network, what exactly happens? To draw an analogy, is it like opening up a road that anyone can use? For someone to gain access to my computer, is the only means to do that through a server's recv(), or can it gain access by other means? What if I only want 1 isp to connect to my computer, can I block all others out?

Again, an article would be great, if anyone knows a good one they would recommend. I'll search some myself too.
.:<<-v0d[KA]->>:.

This topic is closed to new replies.

Advertisement