[英]How can i put my class in the client in socket programming?
I made a game similar to Flappy Bird by using JavaFX.我用JavaFX做了一个类似Flappy Bird的游戏。 Now I want to play it by using localhost IP.
现在我想使用 localhost IP 来播放它。 How can I move the class in FlappyBird to the client so that the flappybird becomes the client?
如何将 FlappyBird 中的 class 移动到客户端,以便 flappybird 成为客户端?
also how can we make multiple clients using this?还有我们如何让多个客户使用它? This code is a simple one i made with simple concept behind it but what i don't understand is how can i make a class as in a game of flappy bird in the socket programming.
这段代码是我用它背后的简单概念制作的一个简单代码,但我不明白的是如何在套接字编程中制作一个 class 游戏中的飞鸟游戏。 How do i implement everything from the flappy bird to the client so the client becomes a flappy bird object
我如何实现从飞扬的鸟到客户端的所有内容,以便客户端成为飞扬的鸟 object
Client :客户:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class FlappyClient
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public FlappyClient(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
//=============
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
FlappyClient client = new FlappyClient("localhost", 5000);
}
}
````````````````````````````````````````````````
````````````````````````````````````````````````
public class FlappyServer
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public FlappyServer(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
FlappyServer server = new FlappyServer(5000);
}
}
`````````````````````````````````````````````
**The flappy bird class is too big. Lets call it FlappyBird I want to make this flappybird a client for the server**
I have found the answer我找到了答案
Main.main(null); Main.main(null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.