我打算解释TCP:
基本概念是,你必须在机器上运行“服务器”。该服务器接受等待连接的客户端。每个连接都通过一个端口(你知道,我希望...)。
始终使用端口1024以上的,因为端口比1025低最的标准协议保留的时间(如HTTP(80),FTP(21),远程登录,...)
但是,创建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 = 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文本阅读是良好的(最好的*)选项:
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:当然,所有网络代码都必须尝试处理IOException。
编辑:我忘了写它为什么不总是最好的选择。 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的字节。
编辑:您可以开发自己的协议,您可以在其中使用Java Reflexion请求方法调用。例如:
String className = ...;
String methodName = ...;
Class[] methodParamTypes = ...;
Object[] methodParams = ...;
Class cl = Class.forName(className);
Method me = cl.getDelcaredMethod(methodName, methodParamTypes);
Object returnValue = me.invoke(this, methodParams);
一旦你有你的对象,你可以把它送到与序列化连接的另一面:ObjectOuputStreams
和ObjectInputStreams
。通过这两个类,您可以通过流写入和读取对象。
Object obj = ...; // Your object you want to write through the stream. (Needs to implement java.io.Serializable)
ObjectOutputStream oos = new ObjectOuptputStream(socket.getOutputStream());
oos.writeObject(oos);
oos.reset(); // (***)
// Don't close it! Otherwise the connection will be closed as well.
而且在连接的另一端:
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Object object = ois.readObject(); // Read the object
// Don't close it!
// Then cast it to whatever you want.
(***)
:退房my question之进一步资料reset()
以及何时使用它。
我打算建议尝试JMS生产者/消费者方法,但你想要一些简单的东西。 – 2011-01-06 14:38:39