• Advertisement
Sign in to follow this  

Accept() accepts only one Client !!

This topic is 3032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi as you see in the title i created my Client and Server
   int err;
    WSAStartup(MAKEWORD(2, 2), &inite);
    socke = socket(AF_INET,SOCK_STREAM,0);
	if(socke == INVALID_SOCKET)
    {
        Sleep(4000);
        exit(0);
        return 0;
    }
	//if bool switch case finally try do register new gcnew int char sizeof return _asm union struct enum while class public private goto break continue float catch long
    i_socke.sin_family = AF_INET;
    i_socke.sin_addr.s_addr = htonl(INADDR_ANY);
    i_socke.sin_port = htons(Port);   
    err = bind(socke,(LPSOCKADDR)&i_socke,sizeof(i_socke));
    if(err != 0)
    {

        return 0;
    }
    err = listen(socke, 5);
    if(err != 0)
    {

        return 0;
    }
	int solen = sizeof(i_socke);
	socke2 = accept(socke, NULL, NULL);
    if(socke2 == INVALID_SOCKET)
    {
	
        return 0;
    }
 	socke = socke2;
return 1;
this is my server code but the probleme is that when i connect with the first Client it works and i can exchange informations but when i open another Client i can connect to the server but not sending and reciving data and when i close the first client then re-open it always the same thing, i can only connect but not sending and reciving data so anyone know how to accept more than one client please ? thanks

Share this post


Link to post
Share on other sites
Advertisement
There's this magical thing in programming. It's called a "loop".

Also helps if you learn to check for incoming connections and accept then, instead of just hanging your thread on a single accept.

Share this post


Link to post
Share on other sites
make sure that your socket is looping on a listen. I'm don't know what the code would look like in C/Cpp, but the loop will look different whether you are receiving synchronously, or asynchronously. If asynchronously, then this could be what's tripping you up. In C# I have an IAyncResult function which is declared in the BeginReceive method.

What I forgot to do and was causing me a great deal of grief, because I, too, forgot that it had to loop, was call my StartReceive() function after the message was received. If you don't then that system thread thinks it's done and goes back home. You have to tell the lazy S.O.B. to go back up and wait for another message. :)

Hope that gave you enough information to help out.

Share this post


Link to post
Share on other sites
ok thanks ddboarm
and i tries this

for(int max = 0; max < 6; max ++)
{
err = listen(socke, max);
if(err != 0)
{

return 0;
}
}

always the same
a
then this

for(socke2; socke2 < 6; socke ++=
{
socke2 = accept(socke, NULL, NULL);
if(socke2 == INVALID_SOCKET)
{

return 0;
}
socke = socke2;
}

and always the same
so this "magical thing in programming. It's called a "loop"." dosen't work with accpet
or juste you can give me an example

Share this post


Link to post
Share on other sites
Multi threading is as far as I know what you need. You could also use Select but your limited to like 63 connections which starting out couldn't be that bad.

Share this post


Link to post
Share on other sites
Quote:
Original post by James_Alex
	for(socke2; socke2 < 6; socke ++=
{
socke2 = accept(socke, NULL, NULL);
if(socke2 == INVALID_SOCKET)
{

return 0;
}
socke = socke2;
}
Of course it's not going to work if you just plug random integers into the loop counter variable and "hope". You have to actually use some logic.

Try this:
for(int i = 0; i < 6; i++)
{
int socket2 = accept(socke, NULL, 0);
if (socket2 == INVALID_SOCKET) {
return 0;
}

// do something with socket2.
}

Share this post


Link to post
Share on other sites
@ARC inc

Multi-threading is not necessary for this.

@James_Alex

Every time you call accept, it will give you back 1 client, or an error.

To accept() multiple clients, you do call it in a loop. You don't call listen() in a loop, listen() just sets up the socket so that you can call accept() on it later.

If you are not at the stage where this makes sense to you, then you don't have the necessary grasp on programming fundamentals yet.

I mean, if I said to you "write a program that reads a variable number of strings from the command prompt", you should be able to do that. This is similar, but instead of strings we have clients.

This isn't to put you down, I am just saying that you have to learn to walk before you can run.

Share this post


Link to post
Share on other sites
this is my code

for(int i = 0; i < 6; i++)
{
int socket2 = accept(socke, NULL, NULL);
if (socket2 == INVALID_SOCKET)
{
return 0;
}

socke = socke2;
int solen = sizeof(i_socke);
getpeername(socke, (sockaddr *)&i_socke, &solen);
printf("a Client has joined the server (IP: %s)", inet_ntoa(i_socke.sin_addr));
}

this dosen't work always, it accepts only 1 Client and it dosen't recv or send any information
but this


for(int i = 0; i < 6; i++)
{
int socket2 = accept(socke, NULL, NULL);
if (socket2 == INVALID_SOCKET)
{
return 0;
}

//socke = socke2;
int solen = sizeof(i_socke);
getpeername(socke, (sockaddr *)&i_socke, &solen);
printf("a Client has joined the server (IP: %s)", inet_ntoa(i_socke.sin_addr));
}

accepts an infinit number of Clients but it dosen't recv or send any information
so can you help me and tell me where the probleme please ?
thanks

Share this post


Link to post
Share on other sites
The second loop, with the socke = socke2 line commented out, is (marginally) better. You don't want to change your listening socket handle to the one just accepted from it!

As to your second question, why do you think it would send or receive data? You don't call send or recv.

Share this post


Link to post
Share on other sites
you don't call send/recv and you also aren't storing your sockets into a container so as soon as your done with that iteration the socket that was accepted goes out of scope and you cannot use t anymore ... you end up with a dead connection..

check out the link in my sig for "Beej's networking guide" it should give you most of the help you need with this.

Share this post


Link to post
Share on other sites
i called the 2 functions
i created a function named "Server::Recive" and "Server::Send" then i used it
in the main file after "Server::Start" and if i remove the loop i can exchange informations but only with 1 Client
and when i add it i cannot no more do it

Share this post


Link to post
Share on other sites
Stop.

Your understanding of C++ is too weak to allow you to proceed to make this program.

Seriously, take a step back and start writing some smaller programs. Learn the basics. If you don't know what a collection is, you won't be able to write this program. If you cannot write a correct loop, you won't be able to write this program.

These are things you have to learn before you can worry about the detail of interacting with the socket API. You don't appear to understand socket handles yet.

If you continue, with our help you might be able to piece together a simple program that kindof works under limited conditions. But:

  • It won't be your work.

  • You won't truely understand it.

  • You won't be able to extend it


Is that what you want?

Again, this is not to put you down. This is about getting you up to speed with C++ so you can properly pursue your goal of writing this program. When you are ready to learn, I will be more than willing to give you actual help.

Share this post


Link to post
Share on other sites
Quote:
Original post by James_Alex
i called the 2 functions
i created a function named "Server::Recive" and "Server::Send" then i used it
in the main file after "Server::Start" and if i remove the loop i can exchange informations but only with 1 Client
and when i add it i cannot no more do it


The fact that you're having trouble with this seems to indicate that you're in way over your head and need to take a step back.

1) Accept gives you a socket to a single client. You must always accept on the listening socket.
2) You must hold on to each socket for each client.
3) You must do send/receive calls on the specific socket for the specific client you want to communicate with.
4) Your program will not magically hold on to sockets, or route send/receives to the correct socket (client) for you. You must manage this yourself.

You need to add your sockets to some sort of container, and use send/receive on the correct socket for the client you wish to communicate with. This is pretty basic stuff, so if you *still* don't understand, you need to start again from the beginning until you do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement