Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2005, 3:51 PM   #1
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
contacting computers over a network

How is it possible to contact other computers over a small network? Can someone point me to a tutorial or a website where i can learn? Basically i want several computers in a network to contact a 'server' designated computer and for that server to respond with some information.

Anyone know how i can learn?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Sep 28th, 2005, 3:58 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Beej's Guide is considered by many to be the tutorial for beginning networking.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 28th, 2005, 4:09 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
That's where I began
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Sep 28th, 2005, 9:26 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Review sockets, tcp/ip, udp, etc...

Yep, that tutorial was one of many that I read when I first got started.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 2nd, 2005, 7:01 AM   #5
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Thanks guys that really helped. I've read the whole lot and basically understood it, but i'm on a windows box and i'm not sure where to put the code that he suggests...

{
    WSADATA wsaData;   // if this doesn't work
    //WSAData wsaData; // then try this instead

    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
    }
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Oct 2nd, 2005, 7:30 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Beej is good. Go to MSDN also, since you're on Windows. Their examples will show you. I would also suggest you use winsock2 instead of the older winsock. If you can't find things by googling or searching MSDN, post back. I have a couple samples, and I believe there are probably a couple posts on the forum.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 2nd, 2005, 8:14 AM   #7
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
I've had a look.

I got this half-tute on MSDN:
http://msdn.microsoft.com/library/de...th_winsock.asp
but it was more confusing than Beej so i'm not too keen on continuing with it.

I'm finding it hard to find examples. Do you have any working windows ones?

The whole subject area seems really interesting but a bit tricky to wet up...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Oct 2nd, 2005, 8:25 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
As you can see, the initialization occurs the very first thing. That shouldn't be too surprising, since it's initialization. I don't want to appear too abrupt, but the ability to research and investigate these things, rather than ask for working examples, is key to being a successful programer. I know that MSDN, particularly, can be hard to navigate, but difficult things must be done by SOMEONE, and self-reliance is a nice employee trait.
int main (void)
{
   // Initialize Winsock.
   WSADATA wsaData;
   SOCKADDR_IN clientService;
   SOCKET uipSocket;
   int i;
   int bytesSent; 
   int bytesRecv;
   char sendbuf[32];
   char recvbuf[32];

   unsigned us;
   us = -1;
   std::cout << hex << us << endl;

   if (WSAStartup (MAKEWORD (2, 2), &wsaData)) return opAdvisory ("Socket startup failed\n");
   uipSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
   if ( uipSocket == INVALID_SOCKET )
   {
      WSACleanup();
      return opAdvisory ("Socket error\n");
   }
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 2nd, 2005, 10:23 AM   #9
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Yeah i completely understand and agree with, but thanks for the help. I did find my own workin example aswell (you'll be glad to know) so i think that i have i cracked... Well sort of

Cheers!
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Oct 2nd, 2005, 11:18 AM   #10
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Well i said i cracked it but i'm back!

I have it communicating over my local box, how would i extend it over a local wired network, for example the on at my school

Code for the 'server' part of the program:
#include <stdio.h>
#include <iostream>
#include "winsock2.h"

using namespace std;

int main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
       cout << "Error at WSAStartup()\n";

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        cout << "Error at socket(): \n"; 
        cout << 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 ) {
        cout << "bind() failed.\n";
        closesocket(m_socket);
        return 0;
    }
    
    // Listen on the socket.
    if ( listen( m_socket, 1 ) == SOCKET_ERROR )
        cout << "Error listening on socket.\n";

    // Accept connections.
    SOCKET AcceptSocket;

    cout << "Waiting for a client to connect...\n";
    while (1) {
        AcceptSocket = SOCKET_ERROR;
        while ( AcceptSocket == SOCKET_ERROR ) {
            AcceptSocket = accept( m_socket, NULL, NULL );
        }
        cout << "Client Connected.\n";
        m_socket = AcceptSocket; 
        break;
    }
    
    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32];
    char recvbuf[32] = "";
    
    bytesRecv = recv( m_socket, recvbuf, 32, 0 );
    cout << "Bytes Recv: " << bytesRecv << "\n" ;
    
    for (int i = 0; i <= 32; i++)
    {
        sendbuf[i] = recvbuf[i];
    }
    
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    cout << "Bytes Sent: " << bytesSent << "\n\n\n";
    cout << recvbuf << "\n\n" ;
    system ("PAUSE");
    return 0;
}

Code for the actual 'messenger' part:
#include <stdio.h>
#include <iostream>
#include "winsock2.h"

using namespace std;

int main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        cout << "Error at WSAStartup()\n";

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        cout << "Error at socket(): \n" << WSAGetLastError();
        WSACleanup();
        return 0;
    }

    // Connect to a server.
    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) {
        cout << "Failed to connect.\n";
        WSACleanup();
        return 0;
    }

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "";
    char recvbuf[32] = "";

    //cin.sync;
    cout << " Enter what you want to appear on the server\n>";
    //getline(cin,sendbuf); 
    cin >> sendbuf;
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    cout << "Bytes Sent:" << bytesSent ;

    while( bytesRecv == SOCKET_ERROR ) {
       bytesRecv = recv( m_socket, recvbuf, 32, 0 );
       if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
           cout << "Connection Closed.\n";
           break;
        }
        if (bytesRecv < 0)
            return 0;
        cout << "Bytes Recv: " << bytesRecv << "\n\n";
    }
    system ("PAUSE");
    return 0;
}

Basically i'm always logged onto the same box at school, so the ip address of the server would stay the same, but i want other people to open the messenger bit, type something and it appear on my screen.

I know i have to change the ip addresses but doesn't anyone know which one corresponds to which box?

Sorry to keep posting but i'm really interested in this stuff...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC