मैंने JavaFX का उपयोग करके Flappy Bird के समान गेम बनाया। अब मैं इसे लोकलहोस्ट आईपी का उपयोग करके खेलना चाहता हूं। मैं फ्लैपीबर्ड में क्लास को क्लाइंट में कैसे स्थानांतरित कर सकता हूं ताकि फ्लैपीबर्ड क्लाइंट बन जाए?
हम इसका उपयोग करके कई क्लाइंट कैसे बना सकते हैं? यह कोड एक सरल है जिसे मैंने इसके पीछे सरल अवधारणा के साथ बनाया है, लेकिन जो मुझे समझ में नहीं आता है वह यह है कि मैं सॉकेट प्रोग्रामिंग में फ्लैपी बर्ड के खेल के रूप में एक वर्ग कैसे बना सकता हूं। मैं फ्लैपी बर्ड से क्लाइंट तक सब कुछ कैसे लागू करूं ताकि क्लाइंट फ्लैपी बर्ड ऑब्जेक्ट बन जाए
ग्राहक:
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**
1 उत्तर
मुझे जवाब मिल गया है
मुख्य। मुख्य (शून्य);
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।