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
Accept() accepts only one Client !!
hi
as you see in the title i created my Client and Server
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.
Also helps if you learn to check for incoming connections and accept then, instead of just hanging your thread on a single accept.
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.
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.
ok thanks ddboarm
and i tries this
always the same
a
then this
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
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
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.
Quote:Original post by James_AlexOf 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.for(socke2; socke2 < 6; socke ++= { socke2 = accept(socke, NULL, NULL); if(socke2 == INVALID_SOCKET) { return 0; } socke = socke2; }
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.}
@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.
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.
this is my code
this dosen't work always, it accepts only 1 Client and it dosen't recv or send any information
but this
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
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
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.
As to your second question, why do you think it would send or receive data? You don't call send or recv.
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.
check out the link in my sig for "Beej's networking guide" it should give you most of the help you need with this.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement