Re: Multiple writes to client socket
From: Fletcher Glenn (fletcher_at_removethisfoglight.com)
Date: 04/06/05
- Next message: Fletcher Glenn: "Re: Writing Client code, if IP address already knew"
- Previous message: Roger Leigh: "Re: undefined reference to shm_unlink()..."
- In reply to: Daniel Draper: "Re: Multiple writes to client socket"
- Next in thread: Barry Margolin: "Re: Multiple writes to client socket"
- Reply: Barry Margolin: "Re: Multiple writes to client socket"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 06 Apr 2005 19:34:37 GMT
More to the point, multiple sends can be combined into a single message.
At the receiving end you may have to extract several messages from a
single receive operation. It's also possible that a single send may be
broken into parts. The only way to be sure that you have all of a
message is to either send fixed sized blocks, or to prepend each message
with a length count.
--
Fletcher Glenn
Daniel Draper wrote:
> Of course. Yes, that makes sense. I'll put the receives in loop and
> see how I go. Thanks for your help!
>
> Barry Margolin <barmar@alum.mit.edu> wrote in message news:<barmar-458D4C.01361604042005@comcast.dca.giganews.com>...
>
>>In article <c2f6d80a.0504032107.4a078173@posting.google.com>,
>> daniel.draper@boxensystems.com (Daniel Draper) wrote:
>>
>>
>>>Hi Barry,
>>>
>>>I've attached the server source as well.
>>>
>>>
>>>>Maybe you need to post the server as well.
>>
>>You're only calling recv() once, immediately after you accept a new
>>connection.
>>
>>Another problem with your code, in both the client and server, is that
>>you assume you're going to get a whole message in a single call to
>>recv(). This is not guaranteed on stream sockets -- there's no required
>>correlation between calls to send() and calls to recv(). You need to
>>call recv() in a loop until you've gotten everything you need.
>>
>>
>>>>BTW, is it a TCP or UDP socket?
>>>
>>>Its a TCP Stream socket
>>>
>>>
>>>----
>>>
>>>int fipcp_start_server(uint16_t port)
>>>{
>>> int sock;
>>> fd_set active_fd_set, read_fd_set;
>>> int i;
>>> struct sockaddr_in clientname;
>>> size_t size;
>>> char *method_name;
>>> char *method_args;
>>> char *method_return;
>>>
>>> /* Create the socket and set it up to accept connections. */
>>> sock = create_server_socket (port);
>>> if (listen (sock, 1) < 0)
>>> {
>>> perror ("listen");
>>> exit (EXIT_FAILURE);
>>> }
>>>
>>> /* Initialize the set of active sockets. */
>>> FD_ZERO (&active_fd_set);
>>> FD_SET (sock, &active_fd_set);
>>>
>>> while (1)
>>> {
>>> /* Block until input arrives on one or more active sockets. */
>>> read_fd_set = active_fd_set;
>>> if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
>>> {
>>> perror ("select");
>>> exit (EXIT_FAILURE);
>>> }
>>>
>>> /* Service all the sockets with input pending. */
>>> printf("fd_setsize = %d\n\n\n", FD_SETSIZE);
>>> for (i = 0; i < FD_SETSIZE; ++i)
>>> {
>>> if (FD_ISSET (i, &read_fd_set))
>>> {
>>> if (i == sock)
>>> {
>>> /* Connection request on original socket. */
>>> int new;
>>> int n;
>>> char buffer[MAXMSG];
>>>
>>> size = sizeof (clientname);
>>> new = accept (sock, (struct sockaddr *)
>>>&clientname, &size);
>>>
>>> if (new < 0)
>>> {
>>> perror ("accept");
>>> exit (EXIT_FAILURE);
>>> }
>>>
>>> n = recv(new, buffer, sizeof(buffer), 0);
>>> printf("n = %d\n", n);
>>> if (n > 0)
>>> {
>>> printf("server recvd: %s\n", buffer);
>>>
>>> /* Parse method call */
>>> method_name = strtok(buffer, ":");
>>> method_args = strtok(NULL, ":");
>>>
>>> method_return = fipcp_execute_method(method_name,
>>>method_args);
>>>
>>> send(new, method_return, n, 0);
>>> free(method_return);
>>> }
>>>
>>> FD_SET (new, &active_fd_set);
>>> }
>>> else
>>> {
>>> close (i);
>>> FD_CLR (i, &active_fd_set);
>>> }
>>> }
>>> }
>>> }
>>>}
- Next message: Fletcher Glenn: "Re: Writing Client code, if IP address already knew"
- Previous message: Roger Leigh: "Re: undefined reference to shm_unlink()..."
- In reply to: Daniel Draper: "Re: Multiple writes to client socket"
- Next in thread: Barry Margolin: "Re: Multiple writes to client socket"
- Reply: Barry Margolin: "Re: Multiple writes to client socket"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
Loading