Changeset 5987 in orxonox.OLD for branches/network/src/subprojects
- Timestamp:
- Dec 8, 2005, 8:13:22 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/subprojects/network/network_unit_test.cc
r5831 r5987 8 8 #include "network_manager.h" 9 9 #include "network_socket.h" 10 #include "server_socket.h" 10 11 #include "network_stream.h" 11 12 #include "synchronizeable.h" … … 36 37 IPaddress ip; 37 38 SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); 38 NetworkSocket server;39 ServerSocket server; 39 40 server.listen(9999); 40 SDL_Delay(20); 41 42 NetworkSocket client(ip); 41 42 NetworkSocket client1(ip); 43 44 NetworkSocket server1 = server.getNewSocket(); 45 46 NetworkSocket client2(ip); 47 48 NetworkSocket server2 = server.getNewSocket(); 43 49 44 50 char buf[1024]; 45 51 46 printf("read from client before sending data\n");47 printf("result: %d bytes\n", client .readBytes((byte*)buf, 1024));48 49 printf("read from serverbefore sending data\n");50 printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024));52 printf("read from client1 before sending data\n"); 53 printf("result: %d bytes\n", client1.readPacket((byte*)buf, 1024)); 54 55 printf("read from client2 before sending data\n"); 56 printf("result: %d bytes\n", client2.readPacket((byte*)buf, 1024)); 51 57 52 58 int n; 53 char * str1 = "client to server"; 54 char * str2 = "server to client"; 55 n = client.writeBytes((byte*)str1, strlen(str1)+1); 56 printf("%d bytes send from client\n", n); 57 n = server.writeBytes((byte*)str2, strlen(str2)+1); 58 printf("%d bytes send from server\n", n); 59 char * str1 = "client1 to server"; 60 char * str2 = "server1 to client"; 61 char * str3 = "client2 to server"; 62 char * str4 = "server2 to client"; 63 n = client1.writePacket((byte*)str1, strlen(str1)+1); 64 printf("%d bytes send from client1\n", n); 65 n = server1.writePacket((byte*)str2, strlen(str2)+1); 66 printf("%d bytes send from server1\n", n); 67 n = client2.writePacket((byte*)str3, strlen(str3)+1); 68 printf("%d bytes send from client2\n", n); 69 n = server2.writePacket((byte*)str4, strlen(str4)+1); 70 printf("%d bytes send from server2\n", n); 59 71 SDL_Delay(1000); 60 72 61 printf("read from server \n");62 n = server .readBytes((byte*)buf, 1024);73 printf("read from server1\n"); 74 n = server1.readPacket((byte*)buf, 1024); 63 75 printf("read %d bytes\n", n); 64 76 if (n<0) 65 77 return -1; 66 78 67 printf("data: %s\n", buf);68 printf("read from client\n"); 69 n = client.readBytes((byte*)buf, 1024);70 79 printf("data: '%s'\n", buf); 80 81 printf("read from server2\n"); 82 n = server2.readPacket((byte*)buf, 1024); 71 83 printf("read %d bytes\n", n); 72 84 if (n<0) 73 85 return -1; 74 86 75 printf("data: %s\n", buf); 76 77 printf("testing a bigger amount of data\n"); 78 79 #define _N_ELEMENTS 212994 80 81 char sendbuf[_N_ELEMENTS+1]; 82 char recvbuf[_N_ELEMENTS+1]; 83 sendbuf[_N_ELEMENTS] = '\0'; 84 recvbuf[_N_ELEMENTS] = '\0'; 85 86 for (int i = 0; i<_N_ELEMENTS; i++) 87 sendbuf[i] = i%26 + 65; 88 89 printf("write\n"); 90 printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); 91 92 SDL_Delay(50); 93 94 printf("read\n"); 95 int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); 96 int offset = nbytes; 97 98 while (nbytes>0) 99 { 100 SDL_Delay(10); 101 //printf("read\n"); 102 nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); 103 offset += nbytes; 104 //printf("nbytes=%d, offset=%d\n", nbytes, offset); 105 } 106 107 printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); 108 109 //printf("%s\n%s\n", sendbuf, recvbuf); 110 111 for (int i = 0; i<_N_ELEMENTS; i++) 112 { 113 if (sendbuf[i]!=recvbuf[i]) 114 { 115 printf("byte %d is the first difference\n", i+1); 116 break; 117 } 118 } 87 printf("data: '%s'\n", buf); 88 89 printf("read from client1\n"); 90 n = client1.readPacket((byte*)buf, 1024); 91 printf("read %d bytes\n", n); 92 if (n<0) 93 return -1; 94 95 printf("data: '%s'\n", buf); 96 97 printf("read from client2\n"); 98 n = client2.readPacket((byte*)buf, 1024); 99 printf("read %d bytes\n", n); 100 if (n<0) 101 return -1; 102 103 printf("data: '%s'\n", buf); 104 105 //sending bigger packets than 255 is not supported 106 107 printf("try to send more than 255 bytes\n"); 108 printf("result: %d\n", client1.writePacket((byte*)buf, 1000)); 109 110 server1.writePacket((byte*)str1, strlen(str1)+1); 111 SDL_Delay(500); 112 printf("try to read with a too small buffer\n"); 113 printf("result: %d\n", client1.readPacket((byte*)buf, strlen(str1))); 119 114 120 115 return 0;
Note: See TracChangeset
for help on using the changeset viewer.