代码下载路径为:csdn_code.rar-互联网文档类资源-CSDN下载?
包含TCP测试速率代码,udpclient、udpserver代码? ssl 测试速率代码。
tcpclient端测试发送数据代码:
inet_pton(AF_INET, "192.168.1.10", &client_addr.sin_addr); 目的IP默认为 192.168.1.10
#define SERVER_PORT 8000 端口默认为 8000
?
#include <netinet/in.h> // sockaddr_in
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <stdio.h> // printf
#include <stdlib.h> // exit
#include <string.h> // bzero
#include <time.h> // bzero
#include <math.h> // bzero
#define SERVER_PORT 8000
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 2048
#define FILE_NAME_MAX_SIZE 512
int main(void)
{
int nsend_buf_len = 13 * 1024;
int nlen = sizeof(int);
int byte_recv = 0, nread = 0;
struct timespec time_start = {0, 0}, time_end = {0, 0};
struct sockaddr_in client_addr;
bzero(&client_addr, sizeof(client_addr));
client_addr.sin_family = AF_INET;
inet_pton(AF_INET, "192.168.1.10", &client_addr.sin_addr);
client_addr.sin_port = htons(SERVER_PORT);
int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket_fd < 0){
perror("Create Socket Failed:");
exit(1);
}
while (1){
char *buffer = NULL;
buffer = (char *)malloc(BUFFER_SIZE + 1);
memset(buffer, 0, BUFFER_SIZE);
int nwrite = 0;
int byte_send = 0, byte_recv = 0;
int ret;
ret = connect(client_socket_fd, (void*)&client_addr, sizeof(client_addr));
if(ret < 0) {
perror("connect fail\n");
exit(1);
}
printf("client connect: 192.168.1.10\n");
// setsockopt(client_socket_fd,SOL_SOCKET,SO_SNDBUF,(char*)&nsend_buf_len,nlen);
while (1){
#if 0
nread = recv(client_socket_fd, buffer, BUFFER_SIZE, 0);
if (nread > 0) {
// printf("nread %d\n", nread);
}else{
printf("--1--connect_close?-- %d", nread);
break;
}
#endif
nwrite = send(client_socket_fd, buffer, BUFFER_SIZE, 0);
if (nwrite > 0) {
// printf("send %d\n", nwrite);
}else{
printf("--2--connect_close?-- %d", nwrite);
break;
}
}
free(buffer);
printf("socket close\n");
}
close(client_socket_fd);
printf("socket close\n");
return 0;
}
?
tcpserver端接收数据,间隔5s计算速率代码:
#define SERVER_PORT 8000? ? ?端口默认8000
#include <netinet/in.h> // sockaddr_in
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <stdio.h> // printf
#include <stdlib.h> // exit
#include <string.h> // bzero
#include <time.h> // bzero
#include <math.h> // bzero
#define SERVER_PORT 9090
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 4096
#define FILE_NAME_MAX_SIZE 512
int main(void)
{
unsigned long l = 0;
double r = 0.0f;
unsigned long tick1;
unsigned long tick2;
unsigned long t = 0;
int byte_recv = 0, nread = 0;
struct timespec time_start = {0, 0}, time_end = {0, 0};
// 声明并初始化一个服务器端的socket地址结构
struct sockaddr_in server_addr;
#if defined(__linux__) || defined(_WIN32)
tick1 = time(NULL);
#else
tick1 = clock();
#endif
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
// 创建socket,若成功,返回socket描述符
int server_socket_fd = socket(PF_INET, SOCK_STREAM, 0);
if (server_socket_fd < 0) {
perror("Create Socket Failed:");
exit(1);
}
int opt = 1;
setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// 绑定socket和socket地址结构
if (-1 == (bind(server_socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))) {
perror("Server Bind Failed:");
exit(1);
}
// socket监听
if (-1 == (listen(server_socket_fd, LENGTH_OF_LISTEN_QUEUE))) {
perror("Server Listen Failed:");
exit(1);
}
while (1) {
// 定义客户端的socket地址结构
struct sockaddr_in client_addr;
socklen_t client_addr_length = sizeof(client_addr);
// 接受连接请求,返回一个新的socket(描述符),这个新socket用于同连接的客户端通信
// accept函数会把连接到的客户端信息写到client_addr中
int new_server_socket_fd = accept(server_socket_fd, (struct sockaddr *)&client_addr, &client_addr_length);
if (new_server_socket_fd < 0) {
perror("Server Accept Failed:");
break;
}
printf("acces succes\n");
// printf("server: got connection from %s, port %d, socket %d\n",
// inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port),new_server_socket_fd);
// recv函数接收数据到缓冲区buffer中
char *buffer = NULL;
int kk = 0;
buffer = (char *)malloc(BUFFER_SIZE + 1);
memset(buffer, 0, BUFFER_SIZE);
int tv_sec_pre = 0;
int tv_nsec_pre = 0;
int nwrite = 0;
int flag_t = 1;
while (1) {
#if 1
nread = recv(new_server_socket_fd, buffer, BUFFER_SIZE, 0);
if (nread > 0)
{
// printf("nread %d\n", nread);
}
else {
printf("--1--connect_close?-- %d", nwrite);
break;
}
#endif
#if 0
if(flag_t<10)
{
sleep(1);
printf("first connect %d \n ",flag_t);
}
nwrite = send(new_server_socket_fd, buffer, nread, 0);
nread = nwrite;
if (nwrite > 0)
{
}
// printf("send %d\n", nwrite);
else {
printf("--2--connect_close?-- %d", nwrite);
break;
}
#endif
#if 1
#if defined(__linux__) || defined(_WIN32)
tick2 = time(NULL);
r = tick2 - tick1;
#endif
l += nread;
if (r > 5)
{
// printf("r=%5.1lf nread %d\n", r,nread);
r = l / r;
r = r / 1024;
r = r / 1024;
printf("%lu %lu len:%lu\n", tick1, tick2, l);
printf("rate:%8.3lfMB/s\n", r);
l = 0;
tick1 = tick2;
}
#endif
}
// // 关闭监听用的socket
// close(new_server_socket_fd);
// printf("socket close\n");
free(buffer);
}
// 关闭监听用的socket
close(server_socket_fd);
printf("socket close\n");
return 0;
}
|