Java服务器/客户端字符串延迟 [英] Java Server/Client string delay

查看:246
本文介绍了Java服务器/客户端字符串延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个LAN游戏,接受字符串并从结构化英语中解析它们,并在网格上显示它们。我创建了服务器和客户端,它的工作,但我有一些问题。当我发送一个字符串它不出现在其他机器上。由于某种原因,字符串只有在其他机器发送结束后才发送到另一台机器。我不知道为什么会发生这种情况。你能帮我找出为什么它不立即发送。感谢

i am creating a LAN game that accepts strings and parses them from structured english and displays them on a grid. i have created the server and client and it works but im having some issues. when i send a string it doesnt appear on the other machine right away. for some reason the string is only sent to the other machine once the other machine sends something over. i dont know why this happens. Could you please help me find out why it doesnt send straight away. Thanks

服务器代码:

import java.awt.Point;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class studentServer{
static ServerSocket serverSocket;
static Socket socket;
static PrintWriter printWriter;
static BufferedReader bufferedReader;
static Thread thread;
Console console = new Console();


public ServerPlayergameMain gm;
    public static void main(String args[]) throws Exception{
}
public void run(String commandMessage){
    while(true){
        try{
            printWriter.println(commandMessage+"\n");
            String input = bufferedReader.readLine();//reads the input from textfield
            console.readLine("Client message: "+input);//Append to TextArea
        }catch(Exception e){}
    }
}
    public void serverStartActionPerformed() {
    System.out.println("Server has started!");
    try{
        serverSocket = new ServerSocket (8888); // socket for the server
        socket = serverSocket.accept(); // waiting for socket to accept client
        JOptionPane.showMessageDialog(null, "Your opponent has connected!", "Opponent Connection!", JOptionPane.INFORMATION_MESSAGE);
        gm = new ServerPlayergameMain();
        gm.setVisible(true);
        bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // reads line from input streamer
        printWriter = new PrintWriter(socket.getOutputStream(),true);
    }catch(IOException | HeadlessException e){
        System.out.println("Server not running!"); //print message if server is not running
    }
}
}

客户代码:

import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class StudentClient {
static Socket socket;
static PrintWriter printWriter;
static BufferedReader bufferedReader;
static Thread thread;
Console console = new Console();


public ClientPlayergameMain gm;

public void Clients(String address) {
    try{
        socket=new Socket("localhost",8888);//Socket for client
        //below line reads input from InputStreamReader
        bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //below line writes output to OutPutStream
        printWriter=new PrintWriter(socket.getOutputStream(),true);
        JOptionPane.showMessageDialog(null, "Connected to server successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
        gm = new ClientPlayergameMain();
        gm.setVisible(true);
        System.out.println("Connected");//debug code
    }catch(Exception e){
       JOptionPane.showMessageDialog(null, "No Connection to server", "Error", JOptionPane.ERROR_MESSAGE);
        System.out.println("Not Connected");
    }    
}
public static void run(String commandMessage){
        while(true){
            try{
                printWriter.println(commandMessage+"\n");
                String input = bufferedReader.readLine();
                System.out.println("From server:" +input);
            }catch(Exception e) {}
        }
}
}


b $ b

代码工作,但我不知道为什么有其他机器发送的东西的条件。

The code works but i dont know why there is a condition for the other machine to send something.

感谢您的时间。

推荐答案

在你的代码中。一些类和对象缺少解决。

A lot of compilation problems are there in you code. Some of the classes and objects are missing to resolve.

仍然我已经尝试找出问题。

Still I have tried it to figure out the issue.

这可能是原因:


  • 发送换行字符 \ n printWriter.println(commandMessage +\\\
    );
    \\ n 。

  • 客户端和服务器都在 printWriter.println(commandMessage +\\\
    ); c $ c>语句,使其在任何类中最后

  • sending new line character \n in printWriter.println(commandMessage+"\n"); statement, just remove \n.
  • client and server both are writing first in printWriter.println(commandMessage+"\n"); statement, make it last in anyone class

以下是代码:

StudentServer.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class StudentServer {
    static ServerSocket serverSocket;
    static Socket socket;
    static PrintWriter printWriter;
    static BufferedReader bufferedReader;
    static Thread thread;

    public static void main(String args[]) throws Exception {
        StudentServer studentServer = new StudentServer();
        studentServer.serverStartActionPerformed();
        studentServer.run("server");
    }

    public void run(String commandMessage) {
        if (true) {
            try {
                printWriter.println(commandMessage);
                String input = bufferedReader.readLine();// reads the input from textfield
                System.out.println("Client message: " + input);// Append to TextArea
            } catch (Exception e) {
            }
        }
    }

    public void serverStartActionPerformed() {
        System.out.println("Server has started!");
        try {
            serverSocket = new ServerSocket(8888); // socket for the server
            socket = serverSocket.accept(); // waiting for socket to accept client
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // reads
                                                                                                 // line
                                                                                                 // from
                                                                                                 // input
                                                                                                 // streamer
            printWriter = new PrintWriter(socket.getOutputStream(), true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Server not running!"); // print message if server is not running
        }
    }
}

StudentClient.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class StudentClient {
    static Socket socket;
    static PrintWriter printWriter;
    static BufferedReader bufferedReader;
    static Thread thread;

    public void clients() {
        try {
            socket = new Socket("localhost", 8888);// Socket for client
            // below line reads input from InputStreamReader
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // below line writes output to OutPutStream
            printWriter = new PrintWriter(socket.getOutputStream(), true);
            System.out.println("Connected");// debug code
        } catch (Exception e) {
            System.out.println("Not Connected");
        }
    }

    public void run(String commandMessage) {
        if (true) {
            try {
                String input = bufferedReader.readLine();
                System.out.println("From server:" + input);
                printWriter.println(commandMessage);
            } catch (Exception e) {
            }
        }
    }

    public static void main(String args[]) throws Exception {
        StudentClient studentClient = new StudentClient();
        studentClient.clients();
        studentClient.run("client");
    }
}

这篇关于Java服务器/客户端字符串延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆