# 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
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
service.sin_family = AF_INET;
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
clientService.sin_family = AF_INET;
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 on other sites
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 on other sites
Quote:
 Original post by Anonymous Poster1) 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 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 on other sites
Just remember to run the server first...

##### Share on other sites
Winsock Multi-Client Servers

A tut about handling multiple clients on Win32 servers, should be intresting..

##### 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 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 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 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982127

• 24
• 9
• 9
• 13
• 11