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, "WSAStartup failed.\n"
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("socket"
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("bind"
return 1;
}
if (listen(sockfd, 5) == -1) {
perror("listen"
return 1;
}
i = sizeof addr;
if ((commfd = accept(sockfd, (struct sockaddr *) &addr, &i)) == -1) {
perror("accept"
return 1;
}
close(sockfd);
if ((i = send(commfd, msg, MSGLEN, 0)) == -1) {
perror("send"
return 1;
}
printf("%d bytes sent", 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 "192.168.1.100"
int main()
{
char msg[MSGLEN];
int commfd;
int i;
struct sockaddr_in addr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n"
return 1;
}
if ((commfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket"
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("connect"
return 1;
}
if ((i = recv(commfd, msg, MSGLEN, 0)) == -1) {
perror("recv"
return 1;
}
printf("%d bytes received\n", i);
close(commfd);
return 0;
}
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, "WSAStartup failed.\n"
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("socket"
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("bind"
return 1;
}
if (listen(sockfd, 5) == -1) {
perror("listen"
return 1;
}
i = sizeof addr;
if ((commfd = accept(sockfd, (struct sockaddr *) &addr, &i)) == -1) {
perror("accept"
return 1;
}
close(sockfd);
if ((i = send(commfd, msg, MSGLEN, 0)) == -1) {
perror("send"
return 1;
}
printf("%d bytes sent", 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 "192.168.1.100"
int main()
{
char msg[MSGLEN];
int commfd;
int i;
struct sockaddr_in addr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n"
return 1;
}
if ((commfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket"
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("connect"
return 1;
}
if ((i = recv(commfd, msg, MSGLEN, 0)) == -1) {
perror("recv"
return 1;
}
printf("%d bytes received\n", i);
close(commfd);
return 0;
}