我更喜欢Java。我将解释TCP:
基本概念是必须在计算机上运行“服务器”。该服务器接受等待连接的客户端。每个连接都通过一个端口(您希望,我希望…)。
始终使用高于1024的端口,因为大多数时间低于1025的端口是标准协议(例如HTTP(80),FTP(21),Telnet等)保留的时间。
但是,用Java创建服务器是通过以下方式完成的:
ServerSocket server = new ServerSocket(8888); // 8888 is the port the server will listen on.
如果您想进行研究,则可能会使用“套接字”这个词。
要将客户端连接到服务器,您必须编写以下代码:
Socket connectionToTheServer = new Socket("localhost", 8888); // First param: server-address, Second: the port但是现在,仍然没有连接。服务器必须接受等待中的客户端(如我在上文中注意到的):
Socket connectionToTheClient = server.accept();
做完了!您的连接已建立!通讯就像File-IO。您唯一需要记住的是,您必须决定何时刷新缓冲区并真正通过套接字发送数据。
使用PrintStream进行文本编写非常方便:
OutputStream out = yourSocketHere.getOutputStream();PrintStream ps = new PrintStream(out, true); // Second param: auto-flush on write = trueps.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是一个很好的选择(最好*):
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:当然,必须为IOExceptions捕获所有网络代码。
编辑:
我忘了写为什么它并不总是最好的选择。BufferedReader使用缓冲区,并尽可能多地读取缓冲区。但是有时您不希望BufferedReader窃取换行符之后的字节并将其放入自己的缓冲区中。
简短示例:
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()将返回读取器缓冲区中最后一个字节之后的字节。
因此,在这种情况下,最好的解决方案是使用
DataInputStream和管理它自己的方式,以了解字符串将有多长,并仅读取该数量的字节并将其转换为字符串。或者:您使用
DataInputStream。 ~~readLine()~~
此方法不使用缓冲区,而是逐字节读取并检查换行符。因此,此方法不会从基础InputStream窃取字节。



