简体   繁体   English

如何在套接字编程中将我的 class 放入客户端?

[英]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.

相关问题 如何在我的网站上放置Server类,以便我的Client类可以从不同的计算机与它通信? - How do I put a Server class on my website so my Client class can communicate with it from different computers? 我如何使用 java 中的客户端服务器套接字编程在网络上使用 stream 镶木地板文件 - How can i stream parquet file over network using client server socket programming in java Java-套接字编程-如何使客户端从多个服务器接收消息? - Java - Socket Programming - How can I make a client receive msg from multiple servers? 在 Java 套接字编程中哪里可以找到我的服务器的主机名 - Where can I find the hostname of my server in Java Socket Programming 如何在套接字编程中传递自定义参数? - How can I pass custom parameters in socket programming? 客户-服务器套接字编程 - CLIENT -SERVER SOCKET PROGRAMMING 如何修改此代码以允许我的客户端套接字程序将字符串发送到服务器? - How can I modify this code to allow my client socket program to send a string to the server? 套接字编程客户端名称 - Socket programming client name 服务器客户端套接字编程 - Server Client Socket Programming 如何等待客户端处理程序上的其他客户端输入? 插座 - How can I wait for additional client input on client handler ? Socket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM