- 论坛徽章:
- 0
|
winsock做server端
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <Winsock2.h>
- #pragma comment(lib, "Ws2_32")
- #define PORT 12345
- #define MAXSOCKFD 10
- int main()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- wVersionRequested = MAKEWORD( 2, 2 );
-
- err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 ) {
- return -1;
- }
-
- if ( LOBYTE( wsaData.wVersion ) != 2 ||
- HIBYTE( wsaData.wVersion ) != 2 ) {
- WSACleanup( );
- return -1;
- }
- int sockfd;
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- WSACleanup( );
- return -1;
- }
- struct sockaddr_in addr;
- int addr_len = sizeof(struct sockaddr_in);
- memset(&addr, 0, addr_len);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- printf("sockfd: %d\n", sockfd);
- if(bind(sockfd, (sockaddr*)&addr, addr_len) < 0) {
- WSACleanup( );
- return -1;
- }
- if(listen(sockfd, MAXSOCKFD) < 0) {
- WSACleanup( );
- return -1;
- }
-
- int newsockfd = accept(sockfd, (sockaddr*)&addr, &addr_len);
- printf("newsockfd: %d\n", newsockfd);
- long int sum = 0;
- time_t start; time(&start);
- while(1) {
- char buf[1024] = {0};
- int ret = recv(newsockfd, buf, 1024, 0);
- if(ret == -1 || ret == 0) break;
- sum += ret;
- }
- time_t end; time(&end);
- sum = sum/1024;
- printf("sum: %d\n", sum);
- int sec = end - start;
- printf("time sec: %d\n", sec);
- Sleep(10000000);
-
- return 0;
- }
复制代码
linux做socket的client端
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define PORT 12345
- #define SERVER_IP "192.168.16.39"
- main()
- {
- int sockfd;
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("socket");
- exit(1);
- }
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT);
- addr.sin_addr.s_addr = inet_addr(SERVER_IP);
-
- if(connect(sockfd, (sockaddr*)&addr, sizeof(addr)) < 0) {
- perror("connect");
- exit(1);
- }
- printf("sockfd: %d\n", sockfd);
- for(uint i=0; i<1024*100; i++) {
- char buf[1024] = {0};
- send(sockfd, buf, 1024, 0);
- }
- }
复制代码
//////////////////////////////////////////////////////////////////////////////////////////////
linux做socket的server端
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <pthread.h>
- #define PORT 12345
- #define MAXSOCKFD 10
- int main()
- {
- int sockfd;
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror("socket");
- exit(1);
- }
- struct sockaddr_in addr;
- int addr_len = sizeof(struct sockaddr_in);
- memset(&addr, 0, addr_len);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT);
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- printf("sockfd: %d\n", sockfd);
- if(bind(sockfd, (sockaddr*)&addr, addr_len) < 0) {
- perror("bind");
- exit(1);
- }
- if(listen(sockfd, MAXSOCKFD) < 0) {
- perror("listen");
- exit(1);
- }
-
- int newsockfd = accept(sockfd, (sockaddr*)&addr, (socklen_t*)&addr_len);
- struct timeval start,end;
- gettimeofday(&start, 0);
- long long int sum = 0;
- while(1) {
- char buf[1024] = {0};
- int ret = recv(newsockfd, buf, 1024, 0);
- if(ret == 0 || ret == -1) break;
- sum += ret;
- }
- sum = sum/1024;
- printf("sum: %d\n", sum);
- gettimeofday(&end, 0);
- long ltime = (end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec;
- printf("time usec: %d ~= %d\n", ltime, ltime/1000000);
- return 0;
- }
复制代码
winsock做socket的client端
- #include <stdio.h>
- #include <stdlib.h>
- #ifdef WIN32
- #include "Winsock2.h"
- #else
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #endif
- #pragma comment(lib, "Ws2_32")
- #define PORT 12345
- #define SERVER_IP "127.0.0.1"
- int main()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- wVersionRequested = MAKEWORD( 2, 2 );
-
- err = WSAStartup( wVersionRequested, &wsaData );
- if ( err != 0 ) {
- return -1;
- }
-
- if ( LOBYTE( wsaData.wVersion ) != 2 ||
- HIBYTE( wsaData.wVersion ) != 2 ) {
- WSACleanup( );
- return -1;
- }
- int sockfd;
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- return -1;
- }
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT);
- addr.sin_addr.s_addr = inet_addr(SERVER_IP);
-
- if(connect(sockfd, (sockaddr*)&addr, sizeof(addr)) < 0) {
- return -1;
- }
- printf("sockfd: %d\n", sockfd);
- for(UINT i=0; i<1024*10; i++) {
- char buf[1024] = {0};
- send(sockfd, buf, 1024, 0);
- }
- WSACleanup( );
- return 0;
- }
复制代码
两台不同的机器,一台windows,一台linux,linux做server端的效率明显要比windows做server端要高,这是我程序的问题?还是windows加了许多自己的东西所导致的? |
|