![]() |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 310
| Multi-Socket Server If you don't understand me here's some pseudo-code of what I mean Code: int socket[10];
int i;
while (still listening)
{
for (i = 0; i < 10; i++)
{
if (socket[i] == invalid_socket)
{
socket[i] = accept();
break;
}
}
}
Code: char buffer[2048];
int numbytes = 0;
int result;
do
{
result = recv(sck, buffer, 2048, 0);
if (result > 0)
{
if ((numbytes + result) > 2048)
{
//Send Bad Request
break;
}
numbytes += result;
if (last 4 bytes are \r\n\r\n)
{
sendtoparser(buffer, numbytes);
break;
}
}
}
while (result > 0);
Thanks! |
| carrotcake1029 is offline | |
| | #2 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| What do you mean by "packet fragmentation?" The normal sense of fragmentation means "IP fragmentation," something which is resolved by the TCP/IP stack automatically. Do you just mean integrating the complete packets into a full request?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 310
| Exactly. I want to start processing the data until I see the typical http terminator (\r\n\r\n) |
| carrotcake1029 is offline | |
| | #4 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| Code: if (last 4 bytes are \r\n\r\n) |
| bithub is offline | |
| | #5 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| I don't think you understand how listening sockets work. You have one socket listening on any particular port on any particular interface at any time. No more. When you call accept() you get an entirely new socket - you now have two sockets: a listener and a client. Something like: Code: socket_type listener; socket_type clients[10]; // Somewhere client[x] = accept(listener); // both client[x] and listener are valid here, // but are two different things. listener is still // listening for connections, while client[x] // is a ready connection.
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #6 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,228
| I thought the same thing when I first read this. Quote:
If there is a connection, it is already on a particular socket, handed out by accept() as CactusHugger mentioned. If you want to pick another socket which is also connected, that connection will be a different connection. There is no way to tell "someone is about to connect", so "when a connection is incoming" is not meaningful. | |
| MK27 is offline | |
| | #7 |
| Registered User Join Date: Apr 2008
Posts: 310
| Ok. I think I did a poor job of explaining. I have previously made servers that can support x number of connections simultaneously. What my thoughts are on this topic is that for a server, you need a listening socket, which in turn can, through functions and whatnot, create new sockets which are connections with other computers. Typically, when a connection is established, I create a thread dedicated for the whole connection. My question is whether or not there is an easier way (cleaner too) to assign these connection sockets to variables. What I do now is loop through my array of potential sockets, and whichever one is invalid, I use that one. Am I more clear? lol. I may be going about this entirely wrong. This would not be the first time. Self-taught has its disadvantages... Basically what I am asking is what is a typical flowchart of how a distinguished public application would do something along these lines. I always feel like my code is so hacky. Edit: Was just thinking. I think I made an important realization. Can I just pass the value of the socket along to the thread? When I get time, I'll try and test this out. Last edited by carrotcake1029; 04-17-2009 at 12:44 PM. |
| carrotcake1029 is offline | |
| | #8 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,228
| Quote:
Also, a socket variable is just an int; why would you care about "reusing them"? | |
| MK27 is offline | |
| | #9 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| typically, this is done like: Code: // pseudo code
int main(void)
{
int server_socket = socket();
listen();
bind();
while(running)
{
int new_socket = accept(server_socket);
struct connection* c = malloc(); // create a struct to represent this connection
c->socket = new_socket;
c->thread_id = begin_thread(c);
add_to_connection_list(c); // save all connections in a container of some kind
}
return 0;
}
// thread procedure
void thread_proc(void* param)
{
struct connection* c = param;
while(connection_active)
{
c.data = recv(socket);
process_data(data);
send(socket); // send some kind of response
}
// Since this connection is no longer active
remove_from_connection_list(c);
}
|
| bithub is offline | |
| | #10 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,643
|
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #11 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 902
| ew. ;-) epoll(), for the win. Or, if you don't want to bother with writing the code, (and can wrap your head around it) boost::asio (and gain the cross-platformyness).
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Socket Server | carrotcake1029 | Windows Programming | 2 | 07-21-2008 11:46 AM |
| Server Architecture | coder8137 | Networking/Device Communication | 2 | 01-29-2008 11:21 PM |
| Where's the EPIPE signal? | marc.andrysco | Networking/Device Communication | 0 | 12-23-2006 08:04 PM |
| socket newbie, losing a few chars from server to client | registering | Linux Programming | 2 | 06-07-2003 11:48 AM |
| socket, udp,Halflife Server | Tolpan | C Programming | 2 | 06-21-2002 09:02 AM |