Sign in to follow this  
v0dKA

Starting WINSOCK - UltraNewbie Question

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Guest 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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:

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.


There are many books about network applications secutcurity. One of them is: Writing Secure code

No one can gain an access to your computer if you are simply opening a network connection. You must have some sort of security flaw in the server application and someone must figure out what it is before they can even start attempting to break in. You are in much more danger when browsing the web with IE than when learning winsock, so dont worry.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this