开始使用客户端 - 服务器网络 [英] Getting started with client-server networking

查看:122
本文介绍了开始使用客户端 - 服务器网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个优秀的程序员,但我有零的网络体验。

I'm a good programmer, but I have zero network experience.

基本上,我想进入客户端 - 服务器网络。例如,我想尝试获得一个服务器进程将它允许客户通过互联网连接并发送ping命令到所有其他连接的客户端。那么也许我会尝试开发一个简单的聊天客户端,或一些简单的多人游戏,我会从那里。

Basically, I'd like to get into client-server networking. For example, I'd like to try getting a server process going which allows clients to connect over the internet and send pings to all of the other connected clients. Then maybe I'll try developing a simple chat client, or some simple multiplayer game and I'll go from there.

语言我心里很清楚,可能是有用的:用Java,C ++,C

Languages I know very well that might be useful: Java, C++, C.

我该如何开始?我想学习最佳实践的前面,这么好的学习资源可以推荐(如书籍,网络资料等)将是巨大的。

How do I get started? I want to learn best-practices up front, so good learning resources you can recommend (eg books, online materials, etc) would be great.

编辑:我应该也考虑某种形式的虚拟机来模拟各种机器相互交融

Should I also look into some kind of VM to emulate various machines interacting with each other?

编辑2:我已经提出了一个50-REP赏金。一些伟大的答案已经投入了迄今为止 - 我在寻找更详细的解答了,所以希望这将鼓励。例如,通过与人在这种类型的东西,比较了不同的学习方法经验的答案将是非常有益的。谢谢!我也可以得到整个VM事情一些建议吗?

Edit 2: I've put up a 50-rep bounty. Some great answers have been put up so far - I'm looking for more detailed answers though, so hopefully this will encourage that. For example an answer by someone with experience in this type of stuff that compares different learning approaches would be really helpful. Thanks! Also could I get some feedback on the whole VM thing?

推荐答案

我preFER的Java。我要解释一下TCP:结果
基本概念是,你必须运行一个机器上的一个服务器。该服务器接受客户等待连接。每个连接都要通过一个端口(你知道,我希望......)。结果
始终使用1024以上的端口,因为端口比1025低最的标准协议保留(如HTTP(80),FTP(21),远程登录,...)

I prefer Java. I'm going to explain TCP:
The basic concept is that you have to run a "Server" on a machine. That server accepts clients waiting for a connection. Each connection goes over a port (you know, I hope...).
Always use ports above 1024 because ports lower than 1025 are most of the time reserved for standard protocols (like HTTP (80), FTP (21), Telnet, ...)

但是,创建Java中的一个服务器做这样的:

However, creating a Server in Java is done this way:

ServerSocket server = new ServerSocket(8888); // 8888 is the port the server will listen on.

套接字是你可能寻找,如果你想要做研究的话。结果
和你的客户端连接到服务器,你必须这样写:

"Socket" is the word you are probably looking for if you want to do research.
And to connect your client to a server you have to write this:

Socket connectionToTheServer = new Socket("localhost", 8888); // First param: server-address, Second: the port

但是,现在还没有还的连接。该服务器具有接受等待客户端(如我在这里注意到上述情况):

But now, there isn't still a connection. The server has to accept the waiting client (as I noticed here above):

Socket connectionToTheClient = server.accept();

完成!你建立连接!通信就像文件-IO。你要记住的唯一的事情是,你必须决定何时要刷新缓冲区,真正通过套接字发送数据。结果
使用文本写作一个PrintStream是非常方便的:

Done! Your connection is established! Communicating is just like File-IO. The only thing you have to keep in mind is that you have to decide when you want to flush the buffer and really send the data through the socket.
Using a PrintStream for text-writing is very handy:

OutputStream out = yourSocketHere.getOutputStream();
PrintStream ps = new PrintStream(out, true); // Second param: auto-flush on write = true
ps.println("Hello, Other side of the connection!");
// Now, you don't have to flush it, because of the auto-flush flag we turned on.

一个文本阅读的BufferedReader是好(最好的*)选项:

A BufferedReader for text-reading is the good (best*) option:

InputStream in = yourSocketHere.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
System.out.println(line); // Prints "Hello, Other side of the connection!", in this example (if this would be the other side of the connection.

希望你可以用这个信息网络开始!结果
PS:当然,所有的网络code已被尝试,为钓到的IOExceptions

Hopefully you can start with networking with this information!
PS: Of course, all networking code have to be try-catched for IOExceptions.

编辑:我忘了写,为什么它并不总是最好的选择。一个BufferedReader使用缓冲区并不亚于它可以读入缓冲区。但有时候你不希望这样的BufferedReader中的换行符抢断后的字节数,并把它们纳入自己的缓冲区。结果
简单的例子:

I forgot to write why it isn't always the best option. A BufferedReader uses a buffer and read as much as it can into the buffer. But sometimes you don't want that the BufferedReader steals the bytes after the newline and put them into his own buffer.
Short example:

InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// The other side says hello:
String text = br.readLine();
// For whatever reason, you want to read one single byte from the stream,
// That single byte, just after the newline:
byte b = (byte) in.read();

但BufferedReader中早已是字节,你想读的,在他的缓冲。因此调用 in.read()继在阅读器的缓冲区中的最后一个字节将返回字节。

But the BufferedReader has already that byte, you want to read, in his buffer. So calling in.read() will return the byte following on the last byte in the buffer of the reader.

所以,在这种情况下,最好的解决办法是使用 DataInputStream以和管理它自己的方式知道该字符串的长度和仅读取的字节数和将它们转换成一个字符串。或者:您可以使用

So, in this situation the best solution is to use DataInputStream and manage it your own way to know how long the string will be and read only that number of bytes and convert them into a string. Or: You use

DataInputStream.readLine()

这个方法不使用缓冲器和由字节并检查一个新行读取字节。因此这种方法不从基础InputStream偷字节

This method doesn't use a buffer and reads byte by byte and checks for a newline. So this method doesn't steal the bytes from the underlying InputStream.

这篇关于开始使用客户端 - 服务器网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆