[技术][讨论] 如何选择服务器与客户端之间的网络通信协议?

29 views
Skip to first unread message

Alex

unread,
Oct 24, 2011, 12:00:24 AM10/24/11
to game.dev.top
感觉问题问的可能有点笼统,很多游戏服务器与客户端采用的是socket通信,走tcp协议;或者是http协议,长短连接。
1:用socket 直接通信还是 http; 不要管是不是一个层次的东西,当然http基于tcp,socket走的是tcp
2:如果是客户端移植,服务器端采用socket或者http长连接,那么客户端是否可以采取不同的方式,有什么利弊呢?
服务器端socket,客户端http,或者反过来。

Zhangming Niu

unread,
Oct 24, 2011, 3:25:30 AM10/24/11
to gamed...@googlegroups.com
no much diffs, Http uses socket also

2011/10/24 Alex <wangh...@gmail.com>



--
--------------------------------------------------------------------
Best Regards,

Zhangming Niu




Zhangming Niu

unread,
Oct 24, 2011, 3:26:35 AM10/24/11
to gamed...@googlegroups.com
HTTP is just kind of special text format, like XML, but it is a special and well defined and widely used XML

2011/10/24 Zhangming Niu <niuzha...@gmail.com>

Milo Yip

unread,
Oct 24, 2011, 3:48:57 AM10/24/11
to gamed...@googlegroups.com
HTTP is NOT HTML... It is a transfer protocol, like FTP.
HTTP can be used to transfer any data, such as a JPEG image file.

For details about HTTP, please refer to RFC2616
http://tools.ietf.org/html/rfc2616

2011/10/24 Zhangming Niu <niuzha...@gmail.com>:


> HTTP is just kind of special text format, like XML, but it is a special and
> well defined and widely used XML

--
Milo Yip

http://www.cnblogs.com/miloyip/
http://weibo.com/miloyip/
http://twitter.com/miloyip/

Zhangming Niu

unread,
Oct 24, 2011, 3:56:20 AM10/24/11
to gamed...@googlegroups.com
i know it is not, but it follows much of the xml format, isn't it

2011/10/24 Milo Yip <mil...@gmail.com>

Milo Yip

unread,
Oct 24, 2011, 4:01:14 AM10/24/11
to gamed...@googlegroups.com
No... it does not like XML. HTTP request and response is in plain-text.

You may check a HTTP example from
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session

2011/10/24 Zhangming Niu <niuzha...@gmail.com>:

sail tsao

unread,
Oct 24, 2011, 4:02:43 AM10/24/11
to gamed...@googlegroups.com
国外游戏喜欢使用UDP协议为主,为UDP添加一种模拟TCP的确保发送成功的机制,主要原因是国外喜欢制作实时性很强的游戏,TCP对于实时性强的游戏服务器来说有弊端,tcp的原罪,在此就不细说了,所以国外用UDP的多。
国内因为制作的网游很多实时性不高,所以TCP为主。
除了tcp和udp以外,HTTP之流的协议的游戏服务器还真没见过,因为我实在想不出用HTTP的需求
--
Sail Tsao
I'm Just a Simple Person.
My Webpage:
http://www.sailtsao.com/

lixin China

unread,
Oct 24, 2011, 4:11:56 AM10/24/11
to game.dev.top
HTTP我能想到的好处是穿透性强,再无其他,就协议本身来讲废话很多

On 10月24日, 下午4时02分, sail tsao <sail.t...@gmail.com> wrote:
> 国外游戏喜欢使用UDP协议为主,为UDP添加一种模拟TCP的确保发送成功的机制,主要原因是国外喜欢制作实时性很强的游戏,TCP对于实时性强的游戏服务器 来说有弊端,tcp的原罪,在此就不细说了,所以国外用UDP的多。


> 国内因为制作的网游很多实时性不高,所以TCP为主。
> 除了tcp和udp以外,HTTP之流的协议的游戏服务器还真没见过,因为我实在想不出用HTTP的需求
>

> 在 2011年10月24日 下午3:48,Milo Yip <milo...@gmail.com>写道:
>
>
>
>
>
>
>
>
>
> > HTTP is NOT HTML... It is a transfer protocol, like FTP.
> > HTTP can be used to transfer any data, such as a JPEG image file.
>
> > For details about HTTP, please refer to RFC2616
> >http://tools.ietf.org/html/rfc2616
>

> > 2011/10/24 Zhangming Niu <niuzhangm...@gmail.com>:

Zhangming Niu

unread,
Oct 24, 2011, 4:18:55 AM10/24/11
to gamed...@googlegroups.com
it depends on how you understand it.
the <connection> closed  </connection> // in xml
equals:
connection: closed  // in http

I am from the information point of view, just different format of spreading infos



2011/10/24 lixin China <small...@gmail.com>

Zhangming Niu

unread,
Oct 24, 2011, 4:21:40 AM10/24/11
to gamed...@googlegroups.com
I was highlighting the message contention itself,
there are too many message formats, it is true that  they look diff, but they do do the same work


2011/10/24 Zhangming Niu <niuzha...@gmail.com>

Zhangming Niu

unread,
Oct 24, 2011, 4:23:04 AM10/24/11
to gamed...@googlegroups.com
HTTP is sometimes used for webpage games

2011/10/24 Zhangming Niu <niuzha...@gmail.com>

Zhangming Niu

unread,
Oct 24, 2011, 5:30:26 AM10/24/11
to gamed...@googlegroups.com
here is a good place to start with, a short example:

Server response


 HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8


all of these infos could be also formatted  using struct, Json, pro buf, xml.
if one have exp if doing the CGI stuff, he will understand the root reason: only the information itself is important.
as no matter what kind of format you choose to use.

eg.
(1)the bad design of a mesage:
struct HttpResponse// to many to translate, just take 3 attribute for a quick example
{
int httpVersion; // int is too big, little endian problem potential
char responseCode[10]; // 200 OK, could not pass string in c/c++ struct
unsigned int contentLenth; //438, big little endian problem potential
};

(2)a better one
struct HttpHeader
{
uint16_t contentLenth; //438

};
struct HttpBody
{
uint16_t httpVersion;
char responseCode[10];
uint16_t rCLength;// sometimes used
};

struct Http{
HttpHeader header;
HttpBody body;
};
we can do some translation of the versions, as float/double is not transferred.although there are many ways to do so.

3.1
at the sending side:
****************************************
Http http;
http.body.httpVersion = 1;
http.body.responseCode  = "200 OK";
http.header.contentLength = 438;

 send(socket, char *(&http), sizeof(Http), 0) // need to add validation stuff
**********************************************



at the receiving side :
****************************************
enum httpVersion
{
version1point1 = 0,
version1point2 = 1
};

HttpHeader httpHeader_recv;
HttpBody httpBody_recv

// receive http header, get the body length for the next step
 recv(socket, char *(&httpHeader_recv), sizeof(httpHeader_recv), 0)// need to add validation stuff
 
uint16_t bodyLength = sizeof(httpHeader_recv.contentlength) ;
 
// receive http body, use the body length contention
 recv(socket, char *(&httpBody), bodyLength, 0)// need to add validation stuff
  if (successful)
  {
        
         switch(recvHttp.body.httpVersion)
         {
         case httpVersion::version1point1 :
         //dosth
         case httpVersion::version1point2 :
            if ( 0== strcmp (recvHttp.body.responseCode,"200 OK") )
              {

              //do sth
              }
}






2011/10/24 Zhangming Niu <niuzha...@gmail.com>

dogvane Huang

unread,
Oct 24, 2011, 6:47:01 AM10/24/11
to gamed...@googlegroups.com
感觉区别应该是是否是自定义协议(二进制socket),还是使用已知协议(HTML或者XML)

Zhangming Niu

unread,
Oct 24, 2011, 6:54:38 AM10/24/11
to gamed...@googlegroups.com
or better separate them as (1)http, html, sip
versus (2)self defined messages, Json, struct, class,xml etc.

2011/10/24 dogvane Huang <dog...@gmail.com>

Alex

unread,
Oct 24, 2011, 9:48:53 PM10/24/11
to game.dev.top

TO ==> Zhangming Niu
HTTP底层是socket,这个是毋庸置疑的,至于HTTP和XML的关系,似乎不用过于深究,毕竟HTTP是为了识别传输传输的数据内容,可能起到
的作用上和XML是相似的。
TO ==> sail tsao
的确,国内网游多数使用的是socket采用tcp协议,实时性要求一般,HTTP做服务器的倒是也有,我见到有几款社区游戏服务器采用的就是http
长连接实现的。
TO ==> dogvane Huang
感觉是一个选项,自定义数据格式,二进制数据传输采用socket是最好的,无论采用udp或者tcp,http传输的毕竟是明文,安全性不高,容易被
干掉。

Reply all
Reply to author
Forward
0 new messages