![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Beej's Guide is considered by many to be the tutorial for beginning networking.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|