三种网络数据传输方式比较(byte stream,protobuf,json)

三种网络数据传输方式比较(byte stream,protobuf,json) 2014-08-27 10:39:04

分类: LINUX

原文地址:三种网络数据传输方式比较(byte stream,protobuf,json) 作者:yuyunliuhen

针对于tinynet进行了三种数据传输方式的测试,包括最初的byte stream,protobuf,以及比较流行json方式。跟之前的几次测试一样,模型为echo模型,都是以epoll为例,每个连接每秒发送10个包,每个数据包约100bytes,数据包括包头以及数据,包头12bytes,包括长度以及其他8bytes信息,客户端连接为每秒5个,此次测试总计连接10000个.

之前的测试报告:

tinynet (V1.0.0) 一个轻量级的网络库(测试报告)

tinynet V1.0.1 一个轻量级的网络库(测试报告2)

libevent vs tinynet echo server 性能测试 (1)

工程源代码:

https://github.com/yuyunliuhen/tinynet

服务器是多线程模型,主线程负责接收处理网络数据,并添加的输入缓冲区中;读线程从输入缓冲中读取数据并进行处理,将处理的数据添加的输出缓冲区中;写线程将输出缓冲区的数据发送到指定的客户端连接。由于数据处理只是在读线程处理,所以只需要变更读线程的处理代码就可以了。( 默认读写线程的sleep时间为500ms)

下面分别为服务器端读线程byte stream,protobuf,json三种方式的部分代码:

点击(此处)折叠或打开

  1. // byte stream
  2. void Server_Impl::_read_thread()
  3. {
  4. static const int __head_size = 12;
  5. char __read_buf[max_buffer_size_] = {};
  6. while (true)
  7. {
  8. lock_.acquire_lock();
  9. #ifdef __DEBUG
  10. struct timeval __start_timeval;
  11. gettimeofday(&__start_timeval, NULL);
  12. long __start_time = __start_timeval.tv_usec;
  13. #endif //__DEBUG
  14. for (std::vector<Buffer*>::iterator __it = connects_copy.begin(); __it != connects_copy.end(); ++__it)
  15. {
  16. if(*__it)
  17. {
  18. Buffer::ring_buffer* __input = (*__it)->input_;
  19. Buffer::ring_buffer* __output = (*__it)->output_;
  20. if (!__input || !__output)
  21. {
  22. continue;
  23. }
  24. #ifdef __DEBUG
  25. if(0 == (*__it)->fd_ % 1000)
  26. {
  27. printf("fd =%d,rpos = %d, wpos = %d\n",(*__it)->fd_,__input->rpos(),__input->wpos());
  28. }
  29. #endif //__DEBUG
  30. while (!__input->read_finish())
  31. {
  32. int __packet_length = 0;
  33. int __log_level = 0;
  34. int __frame_number = 0;
  35. unsigned char __packet_head[__head_size] = {};
  36. int __head = 0;
  37. unsigned int __guid = 0;
  38. if(!__input->pre_read((unsigned char*)&__packet_head,__head_size))
  39. {
  40. //    not enough data for read
  41. break;
  42. }
  43. memcpy(&__packet_length,__packet_head,4);
  44. #if 0
  45. memcpy(&__head,__packet_head + 4,4);
  46. memcpy(&__guid,__packet_head + 8,4);
  47. #endif
  48. if(!__packet_length)
  49. {
  50. printf("__packet_length error\n");
  51. break;
  52. }
  53. #if 0
  54. __log_level = (__head) & 0x000000ff;
  55. __frame_number = (__head >> 8);
  56. #endif
  57. memset(__read_buf,0,max_buffer_size_);
  58. if(__input->read((unsigned char*)__read_buf,__packet_length + __head_size))
  59. {
  60. __output->append((unsigned char*)__read_buf,__packet_length + __head_size);
  61. }
  62. else
  63. {
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. #ifdef __DEBUG
  70. struct timeval __end_timeval;
  71. gettimeofday(&__end_timeval, NULL);
  72. long __end_time = __end_timeval.tv_usec;
  73. long __time_read = __end_time - __start_time;
  74. printf("start time = %ld, end time = %ld,server impl time read = %ld\n",__start_time,__end_time,__time_read);
  75. #endif //__DEBUG
  76. lock_.release_lock();
  77. #ifdef __LINUX
  78. usleep(max_sleep_time_);
  79. #endif // __LINUX
  80. }
  81. }
  82. // protobuf
  83. void Server_Impl::_read_thread()
  84. {
  85. transfer::Packet __packet_protobuf;
  86. std::string      __string_packet;
  87. static const int __head_size = 4;
  88. while (true)
  89. {
  90. lock_.acquire_lock();
  91. #ifdef __DEBUG
  92. struct timeval __start_timeval;
  93. gettimeofday(&__start_timeval, NULL);
  94. long __start_time = __start_timeval.tv_usec;
  95. #endif //__DEBUG
  96. for (std::vector<Buffer*>::iterator __it = connects_copy.begin(); __it != connects_copy.end(); ++__it)
  97. {
  98. if(*__it)
  99. {
  100. Buffer::ring_buffer* __input = (*__it)->input_;
  101. Buffer::ring_buffer* __output = (*__it)->output_;
  102. if (!__input || !__output)
  103. {
  104. continue;
  105. }
  106. #ifdef __DEBUG
  107. if(0 == (*__it)->fd_ % 1000)
  108. {
  109. printf("fd =%d,rpos = %d, wpos = %d\n",(*__it)->fd_,__input->rpos(),__input->wpos());
  110. }
  111. #endif //__DEBUG
  112. while (!__input->read_finish())
  113. {
  114. int __packet_length = 0;
  115. int __log_level = 0;
  116. int __frame_number = 0;
  117. unsigned char __packet_head[__head_size] = {};
  118. int __head = 0;
  119. unsigned int __guid = 0;
  120. if(!__input->pre_read(__packet_head,__head_size))
  121. {
  122. //    not enough data for read
  123. break;
  124. }
  125. __packet_length = (int)*__packet_head;
  126. if(!__packet_length)
  127. {
  128. printf("__packet_length error\n");
  129. break;
  130. }
  131. #if 0
  132. //    easy_bool read(easy_uint8* des,size_t len)
  133. char __read_buf[max_buffer_size_] = {};
  134. if(__input->read((unsigned char*)__read_buf,__packet_length + __head_size))
  135. {
  136. __string_packet = __read_buf + __head_size;
  137. __packet_protobuf.ParseFromString(__string_packet);
  138. __output->append((unsigned char*)__read_buf,__packet_length + __head_size);
  139. #else
  140. //    easy_bool read(std::string& des,size_t len)
  141. __packet_protobuf.Clear();
  142. __string_packet.clear();
  143. if(__input->read(__string_packet,__packet_length + __head_size))
  144. {
  145. __packet_protobuf.ParseFromString(__string_packet.c_str() + __head_size);
  146. __output->append((unsigned char*)__string_packet.c_str(),__packet_length + __head_size);
  147. #endif
  148. #ifdef __DEBUG
  149. printf("__packet_protobuf.head = %d\n",__packet_protobuf.head());
  150. printf("__packet_protobuf.guid = %d\n",__packet_protobuf.guid());
  151. printf("__packet_protobuf.content = %s\n",__packet_protobuf.content().c_str());
  152. #endif //__DEBUG
  153. }
  154. else
  155. {
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. #ifdef __DEBUG
  162. struct timeval __end_timeval;
  163. gettimeofday(&__end_timeval, NULL);
  164. long __end_time = __end_timeval.tv_usec;
  165. long __time_read = __end_time - __start_time;
  166. printf("start time = %ld, end time = %ld,server protobuf impl time read = %ld\n",__start_time,__end_time,__time_read);
  167. #endif //__DEBUG
  168. lock_.release_lock();
  169. #ifdef __LINUX
  170. usleep(max_sleep_time_);
  171. #endif // __LINUX
  172. }
  173. }
  174. // json
  175. void Server_Impl::_read_thread()
  176. {
  177. static const int __head_size = 4;
  178. while (true)
  179. {
  180. lock_.acquire_lock();
  181. #ifdef __DEBUG
  182. struct timeval __start_timeval;
  183. gettimeofday(&__start_timeval, NULL);
  184. long __start_time = __start_timeval.tv_usec;
  185. #endif //__DEBUG
  186. for (std::vector<Buffer*>::iterator __it = connects_copy.begin(); __it != connects_copy.end(); ++__it)
  187. {
  188. if(*__it)
  189. {
  190. Buffer::ring_buffer* __input = (*__it)->input_;
  191. Buffer::ring_buffer* __output = (*__it)->output_;
  192. if (!__input || !__output)
  193. {
  194. continue;
  195. }
  196. #ifdef __DEBUG
  197. if(0 == (*__it)->fd_ % 1000)
  198. {
  199. printf("fd =%d,rpos = %d, wpos = %d\n",(*__it)->fd_,__input->rpos(),__input->wpos());
  200. }
  201. #endif //__DEBUG
  202. while (!__input->read_finish())
  203. {
  204. int __packet_length = 0;
  205. int __log_level = 0;
  206. int __frame_number = 0;
  207. unsigned char __packet_head[__head_size] = {};
  208. int __head = 0;
  209. unsigned int __guid = 0;
  210. if(!__input->pre_read(__packet_head,__head_size))
  211. {
  212. //    not enough data for read
  213. break;
  214. }
  215. __packet_length = (int)*__packet_head;
  216. if(!__packet_length)
  217. {
  218. printf("__packet_length error\n");
  219. break;
  220. }
  221. char __read_buf[max_buffer_size_] = {};
  222. if(__input->read((unsigned char*)__read_buf,__packet_length + __head_size))
  223. {
  224. json_error_t* __json_error = NULL;
  225. json_t* __json_loads = json_loads(__read_buf + __head_size,JSON_DECODE_ANY,__json_error);
  226. __output->append((unsigned char*)__read_buf,__packet_length + __head_size);
  227. #ifdef __DEBUG
  228. json_t* __json_loads_head = json_object_get(__json_loads,"head");
  229. json_t* __json_loads_guid = json_object_get(__json_loads,"guid");
  230. json_t* __json_loads_content = json_object_get(__json_loads,"content");
  231. printf("json.head = %d\n",json_integer_value(__json_loads_head));
  232. printf("json.guid = %d\n",json_integer_value(__json_loads_guid));
  233. printf("json.content = %s\n",json_string_value(__json_loads_content));
  234. #endif //__DEBUG
  235. json_decref(__json_loads);
  236. }
  237. else
  238. {
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. #ifdef __DEBUG
  245. struct timeval __end_timeval;
  246. gettimeofday(&__end_timeval, NULL);
  247. long __end_time = __end_timeval.tv_usec;
  248. long __time_read = __end_time - __start_time;
  249. printf("start time = %ld, end time = %ld,server json impl time read = %ld\n",__start_time,__end_time,__time_read);
  250. #endif //__DEBUG
  251. lock_.release_lock();
  252. #ifdef __LINUX
  253. usleep(max_sleep_time_);
  254. #endif // __LINUX
  255. }
  256. }

nmon监控部分结果:

sys summ

byte stream

protobuf

json


    cpu summ

byte stream

protobuf

json


    memory

byte stream

protobuf

json


    net

byte stream

protobuf

json


    netpackets

byte stream

protobuf

json

我们主要通过cpu,内存,网络流量,网络发包数量来分析:

(1)    就cpu消耗来说,使用byte stream所消耗的cpu要比使用protobuf,json要小的多,主要是因为后者需要额外的创建对象,序列化,还原的开销;

(2)    内存变化都不太大,基本保持稳定;

(3)    就网络流量来说,byte stream使用流量比较小,也比较恒定,平均流量在5M左右,而protobuf,json方式平均值都在20M左右,而且前者每秒处理的数据包约为30000个,而后两者在150000~200000之间。这看起来比较奇怪,客户端都是每秒发送10个包,阻塞,服务器读写线程sleep的时间都是500ms,那么正常来说,如果按服务器的处理来看,每秒处理约2个数据包,客户端时间间隔再小,也是如此,这还是都能完全处理的情况下,那么,byte stream的结果应该更加接近事实。但是后两者与前者为何相差这么大呢?

带着疑问,进行了下面的测试,服务器相关代码只是更改了读线程部分,所以我在线程的每次tick前后加上时间戳,记录一次tick所花费的时间,并记录输入缓冲区读写的位置,因为连接数比较多,日志太多,而且刷屏太快,容易影响测试结果,仅仅采样fd为1000的倍数,下面是结果的截图:

byte stream

protobuf

json

由上述结果可知:byte stream每次tick时间为10+毫秒,而且输入缓冲区的r/w pos基本是一致的,也就是说数据每次都处理完毕;而protobuf每次tick时间为100+毫秒,r/w pos的位置差在600~700bytes左右; 使用json方式,每次tick时间多达数百毫秒,且r/w pos的位置差在2000~3000bytes之间。从这组数据基本可以确定,在传输效率上来说:byte stream > protobuf > json,其差距差不多在几倍到一个数量级之间。

为了进一步验证,又做了一组测试,对一个模拟的数据包分别按三种方式进行打包,解包操作,执行10000次,查看执行的总时间:

点击(此处)折叠或打开

  1. /****************************************************************************
  2. Copyright (c) 2013-2014 King Lee
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. general:
  19. $export LD_LIBRARY_PATH=$LD_LIBRARY_PATH../easy/dep/protobuf/src/.libs:../easy/dep/jansson/src/.libs
  20. $../easy/dep/protobuf/src/.libs/protoc -I./ --cpp_out=. transfer.proto
  21. compiler:
  22. $g++ -g -o byte_stream_vs_protobuf_vs_json transfer.pb.h transfer.pb.cc byte_stream_vs_protobuf_vs_json.cc -I../easy/dep/protobuf/src/ -I../easy/dep/jansson/src/ -L../easy/dep/protobuf/src/.libs -L../easy/dep/jansson/src/.libs -lprotobuf -ljansson
  23. ****************************************************************************/
  24. #include <stdlib.h>            //    exit
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <string>
  28. #include <sys/time.h>
  29. #include <string.h>
  30. #include "transfer.pb.h"
  31. #include <jansson.h>
  32. static std::string __random_string[] =
  33. {
  34. "[0x000085e4][T]AdvertisingIndentitifer: ‘‘, IdentifierForVendor: ‘‘, DeviceName: ‘PC‘, ModelName: ‘x86‘, SystemName: ‘‘, SystemVersion: ‘‘, HardwareID: ‘74d435046509‘",
  35. "nice to meet you!",
  36. "It is the tears of the earth that keep here smiles in bloom.",
  37. "The mighty desert is burning for the love of a blade of grass who shakes her head and laughs and flies away.",
  38. "If you shed tears when you miss the sun, you also miss the stars.",
  39. "Her wishful face haunts my dreams like the rain at night.",
  40. "Once we dreamt that we were strangers.We wake up to find that we were dear to each other.",
  41. "Sorrow is hushed into peace in my heart like the evening among the silent trees.",
  42. "Some unseen fingers, like an idle breeze, are playing upon my heart the music of the ripples.",
  43. "Listen, my heart, to the whispers of the world with which it makes love to you.",
  44. "Do not seat your love upon a precipice because it is high.",
  45. "I sit at my window this morning where the world like a passer-by stops for a moment, nods to me and goes.",
  46. "There little thoughts are the rustle of leaves; they have their whisper of joy in my mind.",
  47. "What you are you do not see, what you see is your shadow.",
  48. "My wishes are fools, they shout across thy song, my Master.Let me but listen.",
  49. "They throw their shadows before them who carry their lantern on their back.",
  50. "That I exist is a perpetual surprise which is life.",
  51. "We, the rustling leaves, have a voice that answers the storms,but who are you so silent?I am a mere flower.",
  52. "Do not blame your food because you have no appetite.",
  53. "Success is not final, failure is not fatal: it is the courage to continue that counts.",
  54. "I cannot tell why this heart languishes in silence.It is for small needs it never asks, or knows or remembers.",
  55. "The bird wishes it were a cloud.The cloud wishes it were a bird."
  56. };
  57. static const int __loop_count = 10000;
  58. static int __buf_size = 256;
  59. void byte_stream_test()
  60. {
  61. int __guid = 15;
  62. int __head = 567;
  63. int __length2 = 0;
  64. int __head2 = 0;
  65. int __guid2 = 0;
  66. char __buf[__buf_size];
  67. int __packet_head_size = 12;
  68. int __random_index = 0;
  69. unsigned char __packet_head[__packet_head_size];
  70. int __length = __random_string[__random_index].size();
  71. struct timeval __start_timeval;
  72. gettimeofday(&__start_timeval, NULL);
  73. long __start_time = __start_timeval.tv_usec;
  74. for(int __i = 0; __i < __loop_count; ++__i)
  75. {
  76. memset(__buf,0,__buf_size);
  77. memset(__packet_head,0,__packet_head_size);
  78. //    pack
  79. memcpy(__buf,(void*)&__length,4);
  80. memcpy(__buf + 4,(void*)&__head,4);
  81. memcpy(__buf + 8,(void*)&__guid,4);
  82. strcpy(__buf + __packet_head_size,__random_string[__random_index].c_str());
  83. //    unpack
  84. memcpy(&__length2,(void*)__packet_head,4);
  85. memcpy(&__head2,(void*)(__packet_head + 4),4);
  86. memcpy(&__guid2,(void*)(__packet_head + 8),4);
  87. }
  88. struct timeval __end_timeval;
  89. gettimeofday(&__end_timeval, NULL);
  90. long __end_time = __end_timeval.tv_usec;
  91. long __time_total = __end_time - __start_time;
  92. printf("start time = %ld, end time = %ld,byte stream total time = %ld\n",__start_time,__end_time,__time_total);
  93. }
  94. void protobuf_test()
  95. {
  96. transfer::Packet __packet_protobuf;
  97. std::string __string_packet;
  98. int __guid = 15;
  99. int __head = 567;
  100. int __length2 = 0;
  101. int __head2 = 0;
  102. int __guid2 = 0;
  103. char __buf[__buf_size];
  104. int __packet_head_size = 4;
  105. int __random_index = 0;
  106. unsigned char __packet_head[__packet_head_size];
  107. struct timeval __start_timeval;
  108. gettimeofday(&__start_timeval, NULL);
  109. long __start_time = __start_timeval.tv_usec;
  110. for(int __i = 0; __i < __loop_count; ++__i)
  111. {
  112. memset(__buf,0,__buf_size);
  113. //    pack
  114. __packet_protobuf.Clear();
  115. __string_packet.clear();
  116. __packet_protobuf.set_head(__head);
  117. __packet_protobuf.set_guid(__guid);
  118. __packet_protobuf.set_content(__random_string[__random_index]);
  119. __packet_protobuf.SerializeToString(&__string_packet);
  120. int __length = __string_packet.length();
  121. memcpy(__buf,(void*)&__length,4);
  122. //    unpack
  123. __packet_protobuf.Clear();
  124. __packet_protobuf.ParseFromString(__string_packet);
  125. __head2 = __packet_protobuf.head();
  126. __guid2 = __packet_protobuf.guid();
  127. }
  128. struct timeval __end_timeval;
  129. gettimeofday(&__end_timeval, NULL);
  130. long __end_time = __end_timeval.tv_usec;
  131. long __time_total = __end_time - __start_time;
  132. printf("start time = %ld, end time = %ld,protobuf total time = %ld\n",__start_time,__end_time,__time_total);
  133. }
  134. void json_test()
  135. {
  136. int __guid = 15;
  137. int __head = 567;
  138. int __length2 = 0;
  139. int __head2 = 0;
  140. int __guid2 = 0;
  141. char __buf[__buf_size];
  142. int __random_index = 0;
  143. struct timeval __start_timeval;
  144. gettimeofday(&__start_timeval, NULL);
  145. long __start_time = __start_timeval.tv_usec;
  146. for(int __i = 0; __i < __loop_count; ++__i)
  147. {
  148. //    pack
  149. json_t* __json_msg = json_object();
  150. json_t* __json_head = json_integer(__head);
  151. json_t* __json_guid = json_integer(__guid);
  152. json_t* __json_content = json_string(__random_string[__random_index].c_str());
  153. json_object_set(__json_msg, "head", __json_head);
  154. json_object_set(__json_msg, "guid", __json_guid);
  155. json_object_set(__json_msg, "content", __json_content);
  156. char* __json_dumps_string = json_dumps(__json_msg,0);
  157. int __length = strlen(__json_dumps_string);
  158. json_decref(__json_head);
  159. json_decref(__json_guid);
  160. json_decref(__json_content);
  161. json_decref(__json_msg);
  162. //    unpack
  163. json_error_t* __json_error = NULL;
  164. json_t* __json_loads = json_loads(__json_dumps_string,JSON_DECODE_ANY,__json_error);
  165. json_t* __json_loads_head = json_object_get(__json_loads,"head");
  166. json_t* __json_loads_guid = json_object_get(__json_loads,"guid");
  167. __head2 = json_integer_value(__json_loads_head);
  168. __guid2 = json_integer_value(__json_loads_guid);
  169. json_decref(__json_loads);
  170. free(__json_dumps_string);
  171. }
  172. struct timeval __end_timeval;
  173. gettimeofday(&__end_timeval, NULL);
  174. long __end_time = __end_timeval.tv_usec;
  175. long __time_total = __end_time - __start_time;
  176. printf("start time = %ld, end time = %ld,json total time = %ld\n",__start_time,__end_time,__time_total);
  177. }
  178. int main(int __arg_num, char** __args)
  179. {
  180. byte_stream_test();
  181. protobuf_test();
  182. json_test();
  183. }

其结果与上面的推断基本一致.

其他疑问:使用protobuf,json的方式,读线程的每个tick时间较长,意味着处理的数据可能更少,但是为什么带宽和处理数据包更多了,且接近于满值。目前想到的可能性是使用byte stream的方式服务器基本能及时处理客户端的请求,大部分时间都在sleep了,因此cpu使用量也比较低。(也额外做过测试,将服务器线程的sleep时间更改为10毫秒,这样带宽,以及数据包数量也与后两者相近了,是不是原因就如此呢?)

时间: 2024-08-05 11:40:44

三种网络数据传输方式比较(byte stream,protobuf,json)的相关文章

VMware三种网络配置方式

VMware是很受欢迎的虚拟机,在我们平时的工作中需要经常用到,此文简单总结了平时使用的三种网络配置方式,具体的原理没有去深究. 虚拟机系统安装的是Linux系统. 首先,我们在本机上查看所有网络配置连接,使用命令:ipconfig C:\Documents and Settings\user>ipconfig Windows IP Configuration Ethernet adapter VMware Network Adapter VMnet8: Connection-specific

VMWare的三种网络连接方式

VMWare和主机的三种网络连接方式 桥接 这种模式下,虚拟机通过主机的网卡与主机通信,如果主机能够上网,则虚拟机也能联网. 在虚拟机中,需要将虚拟机的IP配置为与主机处于同一网段. 虚拟机也可以与同网段的其他计算机通信. NAT 在这种模式下,虚拟机通过VMnet8与主机通信,如果主机能够上网,则虚拟机也能联网. 在虚拟机中,需要将虚拟机的IP配置为与VMnet8处于同一网段. 虚拟机不能与主机之外的计算机通信. Host-only 在这种模式下,虚拟机通过VMnet1与主机通信,虚拟机不能联

Linux虚拟机的三种网络连接方式

Linux虚拟机的三种网络连接方式 虚拟机网络模式 无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host-Only 桥接 桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,那么物理网卡和虚拟网卡就相当于处于同一个网段,虚拟交换机就相当于一台现实网络中的交换机,所以两个网卡的IP地址也要设置为同一网段. 所以当我们要在局域网使用虚拟机,对局域网

VMware运行Ubuntu 三种网络连接方式:bridge、NAT、Host-Only的区别【转】

原:http://villasy1989.iteye.com/blog/956746 我的Host是Windows7,安装VMware虚拟机,在虚拟机中装了Ubuntu10.04,首先介绍下VMware下的几个虚拟设备: VMnet0:VMware用于虚拟桥接网络下的虚拟交换机: VMnet1:VMware用于虚拟Host-Only网络下的虚拟交换机: VMnet8:VMware用于虚拟NAT网络下的虚拟交换机: VMware Network Adapter VMnet1:这是Host用于与Ho

vmware虚拟机三种网络连接方式

目录 一.概述 二.Bridged(桥接模式) 三.NAT(地址转换模式) 四.Host-Only(仅主机模式) 正文 一.概述 vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式). 打开vmware虚拟机,我们可以在选项栏的“编辑”下的“虚拟网络编辑器”中看到VMnet0(桥接模式).VMnet1(仅主机模式).VMnet8(NAT模式),那么这些都是有什么作用呢?其实,我们现在看到的VMnet0表示的

Vmware 的三种网络连接方式

VMWare提供了三种工作模式,host-only(主机模式).NAT(网络地址转换模式).bridged(桥接模式). 1.host-only(主机模式) 在某些特殊的网络调试环境中,如何要求将真实环境和虚拟环境隔离开,这时你就可采用host-only模式.在host-only模式中,所有的虚拟系统是可以相互通信的,但虚拟系统和真实的网络是被隔离开的,VMWare虚拟机不能访问互联网. 提示:在host-only模式下,虚拟系统和宿主机器系统是可以相互通信的,相当于这两台机器通过双绞线互连.

(转) linux虚拟机中和主机三种网络连接方式的区别

在介绍网络模式之前,关于网络的几个简单命令的使用 ifup eth0   //启动网卡eth0 ifdown eth0 //关闭网卡eth0 /etc/network/interfaces  //网络配置文件 /etc/init.d/networking  //网络服务位置 /etc/init.d/networking restart  //重启网络 /etc/resolv.conf //DNS配置文件 ifconfig eth0 192.168.5.111 //重新配置网卡eth0的ip 一.

网络--三种网络通讯方式及Android的网络通讯机制

Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别介绍这些接口的功能和作用. 1.标准Java接口 java.net.*提供与联网有关的类,包括流.数据包套接字(socket).Internet协议.常见Http处理等.比如:创建URL,以及URLConnection/HttpURLConnection对象.设置链接参数.链接到服务器.向服务器写数据.从服务器读取

虚拟机中的三种网络详解

一.三种网络区别 1. vmnet0        vmnet0实际上就是一个虚拟的网桥,这个网桥有很若干个端口,一个端口用于连接你的Host,一个端口用于连接你的虚拟机,他们的位置是对等的,谁也不是谁的网关.所以在Bridged模式下,你可以让虚拟机成为一台和你的Host相同地位的机器. 2. vmnet1 vmnet1这是一个Host-Only网络模式,这是用于建立一个与世隔绝的网络环境所用到的,其中vmnet1也是一个虚拟的交换机,交换机的一个端口连接到你的Host上,另外一个端口连接到虚