Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send() sending huge packets

Status
Not open for further replies.

Strogian

Technical User
Nov 11, 2000
36
US
Can anyone help me out? I wrote test server/client programs to see how send and recv worked, and they work how I'd expect them to. (when trying to send() a large packet, it will only send part of it) However, if I try to use telnet/internet explorer as the client instead, send() will send a packet of any size. Oh, one other thing. For some reason, the client (whether it's telnet or my own client) won't receive the whole packet unless I pause on the server for awhile before closing the socket. Oh, and another thing: :) send() returns -1 (error) when I'm using my own client. (even though it DOES send a packet) It doesn't do that in Linux, though...

Here's the source code of the server:
#include <stdio.h>
#include <string.h>
#include <winsock.h>

#define MSGLEN 1048576
#define PORT 1500

int main()
{
char msg[MSGLEN];
int i;
int sockfd, commfd;
struct sockaddr_in addr;
WSADATA wsaData;

if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, &quot;WSAStartup failed.\n&quot;);
return 1;
}

for (i = 0; i < MSGLEN - 2; i++)
msg = i % 26 + 65;
msg[i++] = '!';
msg = '\0';

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(&quot;socket&quot;);
return 1;
}

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
memset(addr.sin_zero, 0, sizeof addr.sin_zero);
if (bind(sockfd, (struct sockaddr *) &addr, sizeof addr) == -1) {
perror(&quot;bind&quot;);
return 1;
}

if (listen(sockfd, 5) == -1) {
perror(&quot;listen&quot;);
return 1;
}

i = sizeof addr;
if ((commfd = accept(sockfd, (struct sockaddr *) &addr, &i)) == -1) {
perror(&quot;accept&quot;);
return 1;
}
close(sockfd);

if ((i = send(commfd, msg, MSGLEN, 0)) == -1) {
perror(&quot;send&quot;);
return 1;
}
printf(&quot;%d bytes sent&quot;, i);

close(commfd);
return 0;
}

And then here's the client:
#include <stdio.h>
#include <winsock.h>

#define MSGLEN 1048576
#define PORT 1500
#define SERVIP &quot;192.168.1.100&quot;

int main()
{
char msg[MSGLEN];
int commfd;
int i;
struct sockaddr_in addr;
WSADATA wsaData;

if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, &quot;WSAStartup failed.\n&quot;);
return 1;
}

if ((commfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(&quot;socket&quot;);
return 1;
}

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(SERVIP);
memset(&addr.sin_zero, 0, sizeof addr.sin_zero);

if (connect(commfd, (struct sockaddr *) &addr, sizeof addr) == -1) {
perror(&quot;connect&quot;);
return 1;
}

if ((i = recv(commfd, msg, MSGLEN, 0)) == -1) {
perror(&quot;recv&quot;);
return 1;
}
printf(&quot;%d bytes received\n&quot;, i);

close(commfd);
return 0;
}
 
Update: I have just found that if I change my client to loop on the recv() call, it will receive numerous packets, but the server will send the whole thing with only one call to send(). Has send() been changed to always send the entire thing in a single call?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top