Sign in to follow this  
Jary

Problem recieving data from different sockets

Recommended Posts

Hello everyone, I have checked your articles, and you have really nice ones about socket. Well I'm still new with sockets. I'm currently making a different bots connecting on IRC on same server. I can make a connection (so a new port) by bot, using an array. I have problem to recieve data, as I use recv(). I only get messages from the last connected bot, and I would like to fix this. So far, my code looks like:
Quote:
unsigned numbot = 0; SOCKET sock[100]; // some stuff... ++numbot; //Assign a new socket number for a bot sock[numbot] = socket(AF_INET, SOCK_STREAM, 0); //Set socket for one bot // some stuff.. connect(sock[numbot], (SOCKADDR *)&sin, sizeof(sin)); //Connect to IRC
This sets a socket for each bot, and connects. This works fine. Now I try to recieve data, with a loop:
Quote:
while (1) { int pos = 0, buf_len; char c = '\0', * buf_str; buf_str = (char *)malloc(10 * sizeof(char)); buf_len = 10; while (c != '\n') { recv(sock[numbot], &c, sizeof(char), 0); buf_str[pos] = c; pos++; if(pos == buf_len) { buf_len += 10; buf_str = (char *)realloc(buf_str,buf_len * sizeof(char)); } } buf_str[pos] = '\0';
But there it only checks for
Quote:
recv(sock[numbot], &c, sizeof(char), 0);
; which is the last socket used. I tried making a while to check all socket, but it didn't work. I would like to please know if there is a function to get all of it, or how to read all the lines being send, and maybe if they could be a simpler code for what I'm doing. I tried to look how people gets more than one socket, but it was only server part, or joining a group (such as multicasting); not sure it fits with what I need. I thank you very much in advance. Thanks

Share this post


Link to post
Share on other sites
If you have a program that connects to many other systems, the logic is very similar to the logic a server uses. The only difference is that you don't call listen or accept. All the questions about how to listen to multiple sockets apply.

The first question is whether or not you're using blocking sockets. If you are, your program will get to the first recv() call and do absolutely nothing until it gets information from that particular client. You need to either check the socket for available data before you read from it first, or you need to use nonblocking sockets.

Also, looking at your code, it looks like it only reads the same socket repeatedly, that being whatever the value of numbot is whenever it gets there. With TCP, you only get data on a particular socket from the one remote party you connected to and nobody else, so you will in fact need to read from all of them, but like I said, only when there's data or only if they're nonblocking.

Also, remember to handle some of the sockets closing while others stay open. If socket 57 out of 100 goes down, don't just try reading it over and over for the rest of the program.

You may want to look up the select macro.

Share this post


Link to post
Share on other sites
What I use, is blocking sockets I think.
The code I posted is working for one client, when I remove "[numbot]" everywhere. If I connect a few, it just shows all the message recieved by the last connected to IRC (which is the higher numbot).

For my code, I think it is fine because when I cout buf_str, I get a new line everytime, and not the same one being repeated.

I'm sorry but I don't really get how to check every socket. Can you send me a link or post me code so I can see something a little less abstract please ?

Thank you

Share this post


Link to post
Share on other sites
Well, in the unix world, you'd want to use the select macro. It's explained all over, google gave me this first: http://www.lowtek.com/sockets/select.html

I've never actually tried to do it in Windows, but I understand there are equivalents, and they have some upsides and downsides, especially if you want to have a great deal of sockets. Take a look at question 11 on the forum.

Lastly, you may want to consider multithreading.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this