|
我用的SOCKET编程,现在有个问题:怎样控制发送端让发送端发送的数据包的大小是一样的
尽管我做了设置
[code:1]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/tcp.h>
#define BUFSIZE 1024
#define SERVER_IP "192.9.200.132"
#define SERVER_PORT 5320
int main()
{
struct sockaddr_in server;
int listen_fd;
char send_buf[BUFSIZE];
const int on = 1;
int n;
int i;
if( (listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Error: socket()");
exit(EXIT_FAILURE);
}
n = setsockopt(listen_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
if(n<0)
{
printf("setsockopt");
return;
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(SERVER_IP);
server.sin_port = htons(SERVER_PORT);
if(connect(listen_fd, (struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("Error: connect()");
close(listen_fd);
exit(EXIT_FAILURE);
}
printf("Connection to '%s'\n", inet_ntoa(server.sin_addr));
for(i = 0; i < 20; i++)
{
// getchar();
memset(send_buf, 0, BUFSIZE);
if(i == 19)
{
n = sprintf(send_buf, "quit");
}
else
{
n = sprintf(send_buf, "Message: %d", i);
}
if(send(listen_fd, send_buf, BUFSIZE, 0) <= 0)
{
break;
}
printf("Send: '%s'\n", send_buf);
}
close(listen_fd);
return(EXIT_SUCCESS);
}
[/code:1]
我从抓取到的包来看,如果发送的速度不快的话,发送的包的大小都是我设定的1024,如果发包速度很快,即把getchar();注释掉,发送的包的大小出现不是1024的包,我已经禁用了Negel算法,请问还要做什么工作,才能达到包大小一致的要求,谢谢 |
|