亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch18.htm

?? 有關于游戲開發的教程我閱讀后感覺不錯所以就拿出來與大家共享。
?? HTM
?? 第 1 頁 / 共 3 頁
字號:
In doing so, you'll have reliable, reusable code that can easily
be applied to provide functionality specific to a particular game
communication protocol.
<P>
As you learned earlier, the primary purpose of sockets is to provide
a channel of communication through a particular port. You also
learned that sockets have associated input and output streams
that actually handle the details of transferring information via
the socket. Even though the standard Java socket classes provide
much of this support, some code still must be handled on your
end; this is the code you're going to focus on at this point.
<P>
The first layer of code necessary to facilitate network communications
comes in the form of a socket helper class that handles the details
of initializing a socket and managing the associated data streams.
The <TT><FONT FACE="Courier">SocketAction</FONT></TT> class was
developed by Greg Turner to help ease the pain in establishing
a communication channel using Java sockets. The <TT><FONT FACE="Courier">SocketAction</FONT></TT>
class is derived from <TT><FONT FACE="Courier">Thread</FONT></TT>
so that it has its own thread of execution. Let's start by looking
at the member variables of <TT><FONT FACE="Courier">SocketAction</FONT></TT>,
which follow:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">private DataInputStream inStream = null;
<BR>
protected PrintStream&nbsp;&nbsp;&nbsp;outStream = null;<BR>
private Socket&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket
= null;</FONT></TT>
</BLOCKQUOTE>
<P>
The first two members, <TT><FONT FACE="Courier">inStream</FONT></TT>
and <TT><FONT FACE="Courier">outStream</FONT></TT>, are the input
and output streams used to receive and send data through the socket.
The third member variable, <TT><FONT FACE="Courier">socket</FONT></TT>,
is the socket object itself.
<P>
The constructor for <TT><FONT FACE="Courier">SocketAction</FONT></TT>
takes a <TT><FONT FACE="Courier">Socket</FONT></TT> object as
its only parameter:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public SocketAction(Socket sock) {<BR>
&nbsp;&nbsp;super(&quot;SocketAction&quot;);<BR>
&nbsp;&nbsp;try {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;inStream = new DataInputStream(new<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedInputStream(sock.getInputStream(),
1024));<BR>
&nbsp;&nbsp;&nbsp;&nbsp;outStream = new PrintStream(new<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedOutputStream(sock.getOutputStream(),
1024), true);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;socket = sock;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;catch (IOException e) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Couldn't initialize
SocketAction: &quot; + e);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;System.exit(1);<BR>
&nbsp;&nbsp;}<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The constructor creates the buffered data streams and initializes
the <TT><FONT FACE="Courier">socket</FONT></TT> member variable
with the <TT><FONT FACE="Courier">Socket</FONT></TT> object passed
in. If there was a problem in initializing the streams, the constructor
detects it by using the <TT><FONT FACE="Courier">catch</FONT></TT>
clause. Note that if there is an error in creating the streams,
something is seriously wrong, which explains why the entire program
exits.
<P>
The <TT><FONT FACE="Courier">send</FONT></TT> and <TT><FONT FACE="Courier">receive</FONT></TT>
methods are possibly the most useful methods in <TT><FONT FACE="Courier">SocketAction</FONT></TT>,
even though they contain very little code:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void send(String s) {<BR>
&nbsp;&nbsp;outStream.println(s);<BR>
}<BR>
<BR>
public String receive() throws IOException {<BR>
&nbsp;&nbsp;return inStream.readLine();<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">send</FONT></TT> method simply sends
a string out over the socket connection by using the output data
stream. Similarly, the <TT><FONT FACE="Courier">receive</FONT></TT>
method receives a string by using the input data stream.
<P>
The <TT><FONT FACE="Courier">closeConnections</FONT></TT> method
simply closes the socket:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void closeConnections() {<BR>
&nbsp;&nbsp;try {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;socket.close();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;socket = null;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;catch (IOException e) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Couldn't close
socket: &quot; + e);<BR>
&nbsp;&nbsp;}<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">isConnected</FONT></TT> method verifies
that the input and output streams, as well as the <TT><FONT FACE="Courier">socket</FONT></TT>
object, are valid:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean isConnected() {<BR>
&nbsp;&nbsp;return ((inStream != null) &amp;&amp; (outStream !=
null) &amp;&amp; (socket != null));<BR>
}<BR>
Finally, the finalize method closes the socket as an added safety
precaution:<BR>
protected void finalize () {<BR>
&nbsp;&nbsp;if (socket != null) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;try {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;socket.close();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;catch (IOException e) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Couldn't
close socket: &quot; + e);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;socket = null;<BR>
&nbsp;&nbsp;}<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
And that's all there is to the <TT><FONT FACE="Courier">SocketAction</FONT></TT>
class. That wasn't too bad, was it? As you saw, the <TT><FONT FACE="Courier">SocketAction</FONT></TT>
class is pretty elementary. Nevertheless, its simple function
of providing a clean management class for sockets and their associated
streams will make life much easier tomorrow when you build a complete
network game.
<H2><A NAME="Summary"><B><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></B></A>
</H2>
<P>
Today you broke away from the confines of programming for a single
user and moved into the world of network programming. You learned
that Java makes network programming surprisingly easy by providing
standard classes that hide most of the nastiness typically associated
with network programming. You began the lesson with some network
fundamentals, progressing onward to learn all about sockets and
how to use them. You then finished the lesson by developing a
reusable socket class that provides much of the overhead involved
in network communications.
<P>
Even though little of today's lesson had anything to do with game
programming specifically, you learned enough about Java network
programming to move on to tomorrow's lesson, which does deal with
game programming. In fact, tomorrow's lesson is entirely devoted
to converting the Connect4 game from <A HREF="ch16.htm" >Day 16</A>'s
lesson into a multiplayer network game playable over the Internet.
<H2><A NAME="QA"><B><FONT SIZE=5 COLOR=#FF0000>Q&amp;A</FONT></B></A>
<BR>
</H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Why is the client/server paradigm so important in Java network programming?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>The client/server model was integrated into Java because it has proven time and again to be superior to other networking approaches. By dividing the act of serving data from the act of viewing and working with 
data, the client/server approach provides network developers with the freedom to implement a wide range of solutions to common network problems.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Does the client/server strategy really make sense for games?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>Based on the traditional role of a server strictly providing information to a &quot;dumb&quot; client, the answer is no. When it comes to games, however, the concept of what the clients and server are 
responsible for changes somewhat. For example, a game server is responsible for receiving client player events and dispatching them to the other players. In turn, the clients are responsible for generating and sending the events to the server, as well as 
updating themselves based on other player events received from the server. When you view the client/server model from this admittedly altered perspective, it makes complete sense for games.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>Q</B></TD><TD><B>Why aren't datagram sockets suitable for network game communications?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=50><B>A</B></TD><TD>The primary reason is speed, because you have no way of knowing when information transferred through a datagram socket will reach its destination. Admittedly, you don't really know for sure when stream socket 
data will get to its destination either, but you can rest assured it will be faster than with the datagram socket. Also, datagram socket transfers have the additional complexity of requiring you to reorganize the incoming data, which is an unnecessary and 
time-consuming annoyance for games.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><B><FONT SIZE=5 COLOR=#FF0000>Workshop</FONT></B></A>
</H2>
<P>
The Workshop section provides questions and exercises to help
you get a firmer grasp on the material you learned today. Try
to answer the questions and at least think about the exercises
before moving on to tomorrow's lesson. You'll find the answers
to the questions in appendix A, &quot;Quiz Answers.&quot;
<H3><A NAME="Quiz"><B>Quiz</B></A></H3>
<OL>
<LI>What is a port?
<LI>What is the significance of using sockets for network communications?
<LI>What is the difference between stream sockets and datagram
sockets?
<LI>What's the big deal about writing a reusable socket class?
</OL>
<H3><A NAME="Exercises"><B>Exercises</B></A></H3>
<OL>
<LI>If you have a dedicated Internet connection, find out what
your numeric IP address is.
<LI>Try out some networked Java applets and see if you can figure
out how they are using Java sockets. Note: You can find many networked
Java applets at the Gamelan Web site (<TT><A HREF="http://www.gamelan.com">http://www.gamelan.com</A></TT>).
<LI>Spend some time relaxing, because tomorrow's lesson dives
straight into developing a complete network game.
</OL>
<P>
<HR WIDTH="100%"></P>

<CENTER><P><A HREF="ch17.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch19.htm"><IMG 
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>

<P>
<HR WIDTH="100%"></P>

</BODY>
</HTML>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产成人精品在线看| 丰满少妇在线播放bd日韩电影| 这里只有精品免费| 成人性生交大片免费看中文 | 欧美欧美欧美欧美首页| 久久无码av三级| 欧美日韩在线播| 成人免费视频国产在线观看| 青青草97国产精品免费观看无弹窗版| 在线看国产一区| 亚洲国产日产av| 国产精品久久影院| 色狠狠色狠狠综合| 成人性视频网站| 国产精品66部| 国产真实精品久久二三区| 国产嫩草影院久久久久| av毛片久久久久**hd| 亚洲自拍与偷拍| 一色桃子久久精品亚洲| 精品国产一区二区三区久久影院| 日本va欧美va欧美va精品| 亚洲综合色自拍一区| 国产欧美日韩不卡免费| www国产精品av| 日韩欧美不卡在线观看视频| 国产精品一区二区久久不卡 | 日韩一区二区免费视频| 在线欧美日韩精品| 免费人成精品欧美精品| 国产亚洲欧洲一区高清在线观看| av中文字幕不卡| 天天射综合影视| 国产亚洲精品aa| 久久久久久久久久久久久久久99| av激情成人网| 91丝袜美腿高跟国产极品老师 | 欧美亚洲综合色| 91一区一区三区| 91蝌蚪porny| av电影一区二区| 91蜜桃在线观看| 日本韩国精品在线| 国产老妇另类xxxxx| 亚洲自拍偷拍图区| 久久久亚洲欧洲日产国码αv| 色综合咪咪久久| 欧美性猛交xxxxxx富婆| 91麻豆精品国产91久久久久久| 欧美日韩第一区日日骚| 不卡电影免费在线播放一区| 成人精品亚洲人成在线| 麻豆精品在线播放| 蜜臀99久久精品久久久久久软件 | 91免费在线播放| 久久66热偷产精品| 国产精品系列在线观看| 福利一区福利二区| 91久久免费观看| 成人a级免费电影| 91黄色激情网站| 国产成人精品影院| 99riav一区二区三区| 欧美三级日韩在线| 欧美精品xxxxbbbb| 久久综合九色综合欧美98| 在线电影欧美成精品| 成年人网站91| 国产福利一区二区三区视频在线 | 国产欧美一区在线| 一区二区三区精品视频在线| 男人操女人的视频在线观看欧美| 国产最新精品免费| 91啪亚洲精品| 精品理论电影在线观看| 欧美午夜精品电影| 91免费看`日韩一区二区| 不卡一区中文字幕| 91精品黄色片免费大全| 国产精品久久久久一区 | 黄一区二区三区| 色婷婷亚洲一区二区三区| 国产精品一区2区| 国产一区视频在线看| 毛片av中文字幕一区二区| 成人高清av在线| gogo大胆日本视频一区| 欧美三级韩国三级日本三斤| 精品日韩99亚洲| 26uuu色噜噜精品一区| 中文字幕日韩一区| 亚洲人成人一区二区在线观看| 国产婷婷色一区二区三区四区| 欧美精品一区二区三区视频| 日韩精品一区二区三区在线播放| 国产精品情趣视频| 日本va欧美va精品| 欧美亚洲综合网| 这里是久久伊人| 亚洲欧美日韩国产另类专区| 蜜桃视频免费观看一区| 欧美伊人久久久久久久久影院| 久久久国产精品麻豆| 日韩 欧美一区二区三区| 91免费观看在线| 国产精品视频在线看| 日韩精品1区2区3区| 美美哒免费高清在线观看视频一区二区| 成人h动漫精品一区二| 2020国产成人综合网| 国产欧美日韩中文久久| 国产精品成人午夜| 精品一区二区三区在线播放| 欧美日韩中字一区| 亚洲女同女同女同女同女同69| 一区二区三区四区不卡在线 | 日韩av中文字幕一区二区三区| 99久久99久久久精品齐齐| 色爱区综合激月婷婷| 国产精品久久久久aaaa| 国产一区二区伦理| 欧美一区二区在线免费观看| 一区二区高清免费观看影视大全 | 国产欧美日韩精品一区| 极品销魂美女一区二区三区| 欧美精品久久99久久在免费线 | 国产在线精品免费| 成人精品视频一区| 欧美午夜精品一区二区三区 | 日本女优在线视频一区二区| 91麻豆精品一区二区三区| 欧美在线|欧美| 久久综合给合久久狠狠狠97色69| 日本三级亚洲精品| 国产成人精品亚洲777人妖| 久久婷婷国产综合精品青草| 韩日欧美一区二区三区| 欧美在线一区二区三区| 亚洲综合免费观看高清完整版| 日本欧美加勒比视频| 7777精品伊人久久久大香线蕉完整版 | 国产呦精品一区二区三区网站| 盗摄精品av一区二区三区| 欧美午夜不卡在线观看免费| wwwwxxxxx欧美| 亚洲一区二区三区视频在线| 在线观看日韩国产| 亚洲成精国产精品女| 欧美喷潮久久久xxxxx| 国产精品久久久久久久久久久免费看| 国产91精品露脸国语对白| 国产精品久久久久久久久果冻传媒| 欧美精品一区二| 麻豆免费看一区二区三区| 日韩欧美卡一卡二| 亚洲精品videosex极品| 紧缚捆绑精品一区二区| 欧美日韩国产美| 免费一级片91| 91精品国产全国免费观看| 久久99国内精品| 6080午夜不卡| 国产精品一区二区三区99| 中文字幕av一区 二区| 日本亚洲视频在线| 欧美一级一级性生活免费录像| 欧美一级专区免费大片| 欧美一区二区啪啪| 欧美天天综合网| 国产不卡在线播放| 91性感美女视频| 精品国一区二区三区| 精品福利在线导航| 国产色91在线| 欧美一个色资源| 成人黄色免费短视频| 日韩激情中文字幕| 在线观看不卡视频| 国产精品蜜臀在线观看| 久久先锋影音av| 亚洲国产精品一区二区久久| 久久精品久久综合| 久久久精品欧美丰满| 99精品视频中文字幕| 中文字幕一区二区三区精华液 | www.爱久久.com| 在线观看亚洲专区| 日韩一区二区三区免费观看| 97国产一区二区| 欧美日韩亚洲综合在线 | 国产成人在线网站| 中文字幕成人av| 国产精品久久看| 欧美电影在线免费观看| 精品国产青草久久久久福利| ㊣最新国产の精品bt伙计久久| 国产精品伦一区二区三级视频| 99精品视频在线播放观看| 精品欧美一区二区在线观看| 国产成人自拍在线|