GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Aug-2007, 03:36
lockd lockd is offline
New Member
 
Join Date: Jul 2007
Posts: 14
lockd is an unknown quantity at this point

Connecting multiple sockets to a server


this is what i tried for 20 sockets

for(int x = 0; x < 20; x++)
{
SOCKET sSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

what is the correct way?
  #2  
Old 12-Aug-2007, 10:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Connecting multiple sockets to a server


Quote:
Originally Posted by lockd
what is the correct way

The correct way to what?

If you are thinking about a server that can communicate with 20 clients (each with its own socket/port), then I suggest you visit http://beej.us/guide/bgnet

Download the excellent Beej's Guide to Network Programming. A brand-new revision was released a few days ago, but for some years now Beej's Guide has been one of the absolute best jump-starters for beginning to learn sockets programming.

There is a multi-client server example, but I suggest that you not try to skip ahead to its section; work your way through the Guide. Read and try to understand and compile (and execute) all of the examples.

If you run into problems, then show us what you tried and explain what it is that you don't understand about what did or did not happen when you tried your code.

Regards,

Dave
Last edited by davekw7x : 12-Aug-2007 at 11:40.
  #3  
Old 12-Aug-2007, 17:13
lockd lockd is offline
New Member
 
Join Date: Jul 2007
Posts: 14
lockd is an unknown quantity at this point

Re: Connecting multiple sockets to a server


Hi Dave, I have read Beej's guide, it's good but I'm learning Winsock...

Yes he does give a multi-connection server example, but my goal is to connect multiple sockets from a single client to a single server and keep them open...

that's why I looped the socket, but I'm not sure if that is the correct way since I get errors... maybe it's just minor errors I need to fix or maybe the entire looping concept is impossible?

SOCKET sSocket[25];

for(int iCount = 0; iCount < 20; iCount++)
{
sSocket[iCount] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
}
  #4  
Old 12-Aug-2007, 20:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Connecting multiple sockets to a server


Quote:
Originally Posted by lockd
...I have read Beej's guide...
So, you have a model for a server that can accept connections from any number of clients: A single "listener socket to accept connection requests. And then "accept" to create a new socket for each incoming request. A loop to monitor the "listener" socket and all of the established clients connections.

The whole example in Beej is a little more than 100 lines of "straight" C code. See footnote.

Of course you put into the loop whatever it is that you want to do with the sockets that communicate to the 20 client ports.

If you want to make a single client program that makes multiple socket connections to a server, you can certainly do it in a loop if you want to. For each connection you have a socket fd and a sock_addr_in structure.

If you show us your code and tell us what the errors were (and maybe tell a little about the application), maybe someone can help.

Regards,

Dave

Footnote:
I have run a number of Beej's examples (including selectserver.c) on Windows XP with Borland and Microsoft compilers, but my present platform is Linux. If I can't give you specific Windows answers, maybe someone else can.
  #5  
Old 12-Aug-2007, 20:47
lockd lockd is offline
New Member
 
Join Date: Jul 2007
Posts: 14
lockd is an unknown quantity at this point

Re: Connecting multiple sockets to a server


Hi, I think I everything should be fine but there are still errors:

CPP / C++ / C Code:
#include <windows.h>
#include <winsock.h>
#include <iostream>

using namespace std;

#pragma comment(lib, "wsock32.lib")

#define CS_ERROR 1
#define CS_OK 0

void sError(char*);

int main()
{

	WORD version;
	WSADATA wsaData;
	int rVal=0;

	version = MAKEWORD(2,0);

	WSAStartup(version,(LPWSADATA)&wsaData);
cout<<"Press Enter to continue";
cin.get();
	LPHOSTENT hostEntry;

	//store information about the server
	hostEntry = gethostbyname("127.0.0.1");

	if(!hostEntry)
	{
		sError("Failed gethostbyname()");
		//WSACleanup();
		return CS_ERROR;
	}

	//create the socket and Loop it
	SOCKET theSocket[25];                      //<----

for(int iCount = 0; iCount < 20; iCount++)     //<----
{
theSocket[iCount] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);  //<---

	SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);  // <---
  
	if(theSocket == SOCKET_ERROR)
	{
		sError("Failed socket()");
		return CS_ERROR;
	}

	//Fill in the sockaddr_in struct
	SOCKADDR_IN serverInfo;

	serverInfo.sin_family = PF_INET;
	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

	serverInfo.sin_port = htons(80);

	rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
	if(rVal==SOCKET_ERROR)
	{
		sError("Failed connect()");
		return CS_ERROR;
	}
cout<<"Connected";
cin.get();


	closesocket(theSocket);
	cout << "closing client"<< endl;
	WSACleanup();
	
	return CS_OK;
}

void sError(char *str)
{                           //  <---  error starts here  line 79
	MessageBox(NULL, str, "SOCKET ERROR", MB_OK);
	WSACleanup();
}}



The errors include:
In function 'int main()'
79: a function definition is not allowed here before '{' token
79: expected '.' or ';' before '{' token
  #6  
Old 12-Aug-2007, 21:19
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Connecting multiple sockets to a server


Quote:
Originally Posted by lockd
Hi, I think I everything should be fine but there are still errors:

CPP / C++ / C Code:
// code removed ...

	return CS_OK;
}

void sError(char *str)
{                           //  <---  error starts here  line 79
	MessageBox(NULL, str, "SOCKET ERROR", MB_OK);
	WSACleanup();
}}


The errors include:
Code:
In function 'int main()' 79: a function definition is not allowed here before '{' token 79: expected '.' or ';' before '{' token

The last } actually closes main(). I think you want sError() to be outside of your main, not inside.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #7  
Old 12-Aug-2007, 21:44
lockd lockd is offline
New Member
 
Join Date: Jul 2007
Posts: 14
lockd is an unknown quantity at this point

Re: Connecting multiple sockets to a server


Hi Paul thx for the response, I edited the code too:
CPP / C++ / C Code:
	closesocket(theSocket);
	cout << "closing client"<< endl;
	WSACleanup();
	
	return CS_OK;
}}

void sError(char *str)
{                     
	MessageBox(NULL, str, "SOCKET ERROR", MB_OK);
	WSACleanup();
}

and the result = the error :too many decimal points in number
I'm lost on this
  #8  
Old 13-Aug-2007, 09:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Connecting multiple sockets to a server


Quote:
Originally Posted by lockd

and the result = the error :too many decimal points in number
I'm lost on this
Is this a compiler error? Post the actual code that you are trying to compile.

Note that, regardless of compiler error(s), your code appears to try to make one connection then closes the socket and exits. Huh?

The following makes no sense:

CPP / C++ / C Code:
    SOCKET theSocket[25];
    for (int iCount = 0; iCount < 20; iCount++)
    {
        theSocket[iCount] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
.
.  // call connect for theSocket
.
        closesocket(theSocket);
        cout << "closing client" << endl;
        WSACleanup();

        return CS_OK;
    }




Since you said that you wanted 20 sockets open at the same time, wouldn't you need something more like:
CPP / C++ / C Code:
    for (int iCount = 0; iCount < 20; iCount++)
    {
        theSocket[iCount] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

.
. // call connect for theSocket[iCount]
.
    }
.
. // do whatever you want with the 20 sockets that are now connected
.
    cout << "closing sockets" << endl;
    for (int i = 0; i < iCount; i++) {
        closesocket(theSocket[iCount]);
    }
    WSACleanup();

Regards,

Dave
  #9  
Old 13-Aug-2007, 15:54
lockd lockd is offline
New Member
 
Join Date: Jul 2007
Posts: 14
lockd is an unknown quantity at this point

Re: Connecting multiple sockets to a server


I got it to work perfect thx for your replies helped so much
  #10  
Old 13-Aug-2007, 17:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Connecting multiple sockets to a server


Well, I hope you were able to overcome my obviously incorrect closing loop.

Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
. // do whatever you want with the 20 sockets that are now connected
.
    cout << "closing sockets" << endl;
    for (int i = 0; i < iCount; i++) {
        closesocket(theSocket[iCount]);
    }
    WSACleanup();


That is, I hope that you would have seen that the loop should be more like

CPP / C++ / C Code:
    for (int i = 0; i < 20; i++) {
        closesocket(theSocket[i]);
    }

I regret the error, but I hope the main idea was clear.


Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dediwebhost.com -- Dual Core Server Blowout. Must see to believe Dediwebhost.com Managed / Dedicated Servers 0 15-May-2007 12:51
Develop a simple chat application consisting of a client and a server. HELP MEEEEEEE elcrazy C++ Forum 1 11-Mar-2007 22:42
Microsoft SQL Server :: SQL Server message 241, state 1, severity 16 lyuboe MySQL / PHP Forum 0 11-May-2005 05:31
· Windows 2003 Server Reseller Special: Unlimited Domains/2 GB Space/for $19.99 contactsonia Web Hosting Advertisements & Offers 0 09-Jan-2004 06:46
web server in secure mode not connecting to http application tcsasp Apache Web Server Forum 0 06-Nov-2003 01:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:25.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.