在Java中使用对象的实例作为客户端

编程入门 行业动态 更新时间:2024-10-07 19:27:48
本文介绍了在Java中使用对象的实例作为客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个任务,我必须使用它来创建一个多线程的hang子手游戏 服务器和客户端.我在想,我将创建一个用于制作游戏(Hangman)的类,以及一个使用main方法的任何try catch块的Server类.客户端类将具有连接到端口并可以播放的客户端.

I have an assignment where I have to create a multithreaded hangman game with a server and client. I am thinking that will I create a class where I will make the game(Hangman), and a Server class with the main method any the try catch block. A client class will have the client which will connect to the port and will be able to play.

我的问题是,如何在how子手游戏中创建客户端,并开始将请求作为对象而不是作为单独的程序发送到服务器?

My question is, how can I create the client in my hangman game and start sending the requests to the server as objects and not as a separate program?

推荐答案

在Java中,称为 Sockets 可用于通过网络将数据发送到特定的IP和端口.该数据将由 ServerSocket

In Java, objects called Sockets can be used to send data across a network to a specific IP and port. That data will be received by a ServerSocket that is listening on the port.

套接字与服务器套接字连接后,它将排队所有拨出电话,直到您调用 ServerSocket#accept , 它将接受远程连接并允许应答呼叫(如果在将所有呼叫排队之前调用accept,它将等待请求再返回). accept方法返回一个常规套接字对象,该对象同时具有用于发送和接收数据的输入流和输出流.

Once a socket connects with a server-socket it will queue any outgoing calls until you invoke ServerSocket#accept, which will accept the remote connection and allow calls to be answered (if you invoke accept before any calls are queued, it will wait for a request before returning). The accept method returns a regular socket object which has both input and output streams for sending and receiving data.

注意::每次调用时,多个套接字可以同时将调用排队 accept()服务器将接受 next 挂起的套接字.这 accept方法的工作方式很像 Scanner 的next()方法.它是 令人困惑,但您可以在此处了解更多信息: ServerSocket侦听时不接受accept().

NOTE: Multiple sockets can queue calls at the same time, every time you invoke accept() the server will accept the next pending socket. The accept method works a lot like a Scanner's next() method. It is confusing, but you can learn more about it here: ServerSocket listens without accept().

我建议构建一个 ObejctInputStream (从accept获取)和 ObjectOutputStream 来自客户端套接字的输出流.

I would suggest constructing an ObejctInputStream from the server-socket's input stream (obtained from accept), and an ObjectOutputStream from the client-socket's output stream.

如果同时需要两个套接字来发送和来接收数据,则只需在服务器端创建一个ObjectOutputStream,在客户端创建一个ObjectInputStream即可.您将需要使用循环或某种 Runnable 侦听两个流(您的分配是多线程的,因此Runnable是最佳选择).

If need both sockets to send and receive data, then simply create an ObjectOutputStream on the server side, and an ObjectInputStream on the client side. You will need to use a loop, or some type of Runnable to listen on both streams (Your assignment is multithreaded, so Runnable is the best option).

通常,将以类似于以下方式的方式来访问客户端套接字:

Typically one would approach client sockets in a manner similar to this:

public class ObjectSocket{ private Socket socket; private ObjectOutputStream streamOut; private ObjectInputStream streamIn; public ObjectSocket(String IP, int port) throws UnknownHostException, IOException { //Request connection. this.socket = new Socket(InetAddress.getByName(IP), port); } public void sendObject(Object obj) throws IOException { if(this.streamOut == null) this.streamOut = new ObjectOutputStream(this.socket.getOutputStream()); //Make sure the connection is still there and the socket is still open. if(this.socket.isConnected() && !this.socket.isClosed()) //Send it! this.streamOut.writeObject(obj); } public Object recieveObject() throws IOException, ClassNotFoundException { if(this.streamIn == null) this.streamIn = new ObjectInputStream(this.socket.getInputStream()); //Cast appropriately. return this.streamIn.readObject(); } public void shutdown() throws IOException { //End the connection this.socket.close(); } }

使用完套接字后,调用shutdown方法很重要.保持端口开放很危险.

It is important to call the shutdown method when you are finished using the socket. Leaving ports open can be dangerous.

使用几乎相同的方法访问服务器端套接字:

The server side sockets are approached almost the same way:

public class ServerObjectSocket { private ServerSocket servSocket; private Socket socket; private ObjectOutputStream streamOut; private ObjectInputStream streamIn; public ServerObjectSocket(int port) throws IOException { //Listen on this port : 0 will listen on a random port. this.servSocket = new ServerSocket(port); } public void acceptConnectionRequest() throws IOException { //Connect with any client socket that is trying this port this.socket = servSocket.accept(); } public Object recieveObject() throws IOException, ClassNotFoundException { if(this.streamIn == null) this.streamIn = new ObjectInputStream(this.socket.getInputStream()); //Cast appropriately. return this.streamIn.readObject(); } public void sendObject(Object obj) throws IOException { if(this.streamOut == null) this.streamOut = new ObjectOutputStream(this.socket.getOutputStream()); //Make sure the connection is still there and the socket is still open. if(this.socket.isConnected() && !this.socket.isClosed()) //Send it! this.streamOut.writeObject(obj); } public void shutdown() throws IOException { //Close the port and end the connection : never leave a port open. this.servSocket.close(); } }

请记住,仅当服务器套接字在转发端口上运行时,才能在不同网络上的套接字之间建立连接.在LAN上运行的套接字不会遇到此问题,但防火墙可能会遇到困难.

Keep in mind, a connection between sockets on different networks is only possible when the server socket in running on a forwarded port. Sockets running on LANs do not face this issue, but might have difficulty with the firewall.

如果您需要更好的理解或示例,只需在Internet上搜索Java Socket教程即可.网络上有很多有用的教程,并不难找到.

If you need a better understanding or example, just search the internet for Java Socket tutorials. There are plenty of useful tutorials floating around the web, and they are not hard to find.

更多推荐

在Java中使用对象的实例作为客户端

本文发布于:2023-11-13 02:44:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   实例   对象   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!