Jump to content
  • Advertisement
Sign in to follow this  
James_Alex

Accept() accepts only one Client !!

This topic is 3213 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!