Java 简单客户机-服务器程序

Java 简单客户机-服务器程序,java,network-programming,Java,Network Programming,我制作了一个基于TCP/IP的基本项目,其中服务器侦听客户机,然后提供传入数据的大写语句 Server.java: import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(7948); Socket

我制作了一个基于TCP/IP的基本项目,其中服务器侦听客户机,然后提供传入数据的大写语句

Server.java:

import java.io.*;
import java.net.*;
public class Server 
{

    public static void main(String[] args) throws Exception
    {
        ServerSocket ss = new ServerSocket(7948);
        Socket s= ss.accept();
        System.out.print("Server connected\n");

        BufferedInputStream bis = new BufferedInputStream (s.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());

        while(true)
        {
            int a = bis.available();
            byte b[] = new byte[a];
            bis.read(b);
            String str = new String(b);
            str = str.toUpperCase();
            b = str.getBytes();
            bos.write(b,0,b.length);
            bos.flush();

            if(str.equals("BYE"))
                break;
            else
                continue;
        }
        System.out.print("\nServer Disconnecting");
        String str = "Adios Amigo";
        bos.write(str.getBytes());
        bos.flush();

        bis.close();
        bos.close();
        ss.close();
        s.close();
    }
}
Client.java:

import java.io.*;
import java.net.*;
public class Client 
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader clientStream = new BufferedReader(new InputStreamReader(System.in));
        String str; 
        int a;
        byte[] b;

        Socket s = new Socket(InetAddress.getLocalHost(), 7948);

        BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream (s.getInputStream());

        one:while(true)
        {
            str = clientStream.readLine();
            b =str.getBytes();
            bos.write(b);
            bos.flush();

            a=bis.available();
            b = new byte[a];
            bis.read(b);
            str = new String (b);
            str.trim();
            System.out.print("The server says: "+str);
            if (str.equals("BYE"))
            {
                bis.read(b);
                str = new String (b);
                System.out.print("The server says: "+str);
                break one;
            }
        }

        s.close();
        clientStream.close();
        bos.close();
        bis.close();
    }
}
    one:while(true)
    {
        str = clientStream.readLine();
        b =str.getBytes();
        bos.write(b);
        bos.flush();

        while (bis.available() <= 0)
        {
            // wait for data!
        }

        a=bis.available();
        b = new byte[a];
        bis.read(b);
        str = new String (b);
        str.trim();
        System.out.print("The server says: "+str);
        if (str.equals("BYE"))
        {
            bis.read(b);
            str = new String (b);
            System.out.print("The server says: "+str);
            break one;
        }
    }
程序运行正常,除了一个问题,客户端的输出在两个输入之后。这意味着我必须从客户端提供两个输入来获得第一个输出,这将继续下去。我无法追踪这个错误。
有人可以帮忙吗?

在客户端,您向服务器发送数据,然后立即调用
a.available()
-此函数不等待从服务器发送数据。由于在调用
.available()
时服务器不太可能已响应数据,因此函数返回零

因此,字节数组
b
(请以后使用更多描述性变量名)的长度为零

一旦创建了大小为零的数组,就可以通过调用
bis.read()
-.read()来等待数据。read()是一个阻塞调用。它将等待来自服务器的数据。由于要读入的数组大小为零,因此不会实际读取此数据。这将导致打印出一个空字符串

下面的代码将解决此问题,但对于将来,我不建议使用
.available()
——以我的经验,这是相当不可靠的。您应该通过简化尝试读取数据来检查数据是否可用

Client.java:

import java.io.*;
import java.net.*;
public class Client 
{

    public static void main(String[] args) throws Exception
    {
        BufferedReader clientStream = new BufferedReader(new InputStreamReader(System.in));
        String str; 
        int a;
        byte[] b;

        Socket s = new Socket(InetAddress.getLocalHost(), 7948);

        BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream (s.getInputStream());

        one:while(true)
        {
            str = clientStream.readLine();
            b =str.getBytes();
            bos.write(b);
            bos.flush();

            a=bis.available();
            b = new byte[a];
            bis.read(b);
            str = new String (b);
            str.trim();
            System.out.print("The server says: "+str);
            if (str.equals("BYE"))
            {
                bis.read(b);
                str = new String (b);
                System.out.print("The server says: "+str);
                break one;
            }
        }

        s.close();
        clientStream.close();
        bos.close();
        bis.close();
    }
}
    one:while(true)
    {
        str = clientStream.readLine();
        b =str.getBytes();
        bos.write(b);
        bos.flush();

        while (bis.available() <= 0)
        {
            // wait for data!
        }

        a=bis.available();
        b = new byte[a];
        bis.read(b);
        str = new String (b);
        str.trim();
        System.out.print("The server says: "+str);
        if (str.equals("BYE"))
        {
            bis.read(b);
            str = new String (b);
            System.out.print("The server says: "+str);
            break one;
        }
    }
one:while(true)
{
str=clientStream.readLine();
b=str.getBytes();
书面报告(b);
bos.flush();

while(bis.available()谢谢Alex,我在以后命名变量时会记住你的建议:)