无法在java中的客户端服务器程序中从客户端获取消息

无法在java中的客户端服务器程序中从客户端获取消息,java,Java,我正在用java编写一个使用TCP/IP的客户机-服务器程序。为此,我编写了以下代码: serversock.java: import java.net.*; import java.io.*; public class serversock { public static void main(String[] args) throws IOException { ServerSocket sersock = null; try

我正在用java编写一个使用TCP/IP的客户机-服务器程序。为此,我编写了以下代码:

serversock.java:

import java.net.*;
import java.io.*;

public class serversock  {
     public static void main(String[] args) throws IOException
    {
        ServerSocket sersock = null;

        try 
        {
            sersock = new ServerSocket(10007);
        }

        catch (IOException e)
        {
            System.out.println("Can't listen to port 10007");
            System.exit(1);
        }

        Socket clientSocket = null;
        System.out.println("Waiting for connection....");


        try
        {
            clientSocket = sersock.accept();
        }

        catch ( IOException ie)
        {
            System.out.println("Accept failed..");
            System.exit(1);
        }

        System.out.println("Conncetion is established successfully..");
        System.out.println("Waiting for input from client...");

        PrintWriter output = new PrintWriter(clientSocket.getOutputStream());

        BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine = input.readLine();

        while ( inputLine  != null)
        {
            output.println(inputLine);
            System.out.println("Server: " + inputLine);
            inputLine = input.readLine();
        }
        input.close();
        clientSocket.close();
        sersock.close();

    }
}
import java.util.*;
import java.io.*;
import java.net.*;

public class clientsock 
{
    public static void main(String[] args) throws IOException
    {
        Socket sock = new Socket("localhost",10007);

        // Scanner scan = new Scanner(System.in);
        PrintWriter output = new PrintWriter(sock.getOutputStream(),true);
        BufferedReader input = new BufferedReader( new InputStreamReader(sock.getInputStream()));
        String line = null;

        BufferedReader stdInput = new BufferedReader( new InputStreamReader(System.in));

        System.out.println("Enter data to send to server: ");
        line = stdInput.readLine();
        while ( (line) != "bye")
        {
            output.println(line.getBytes());
            line = stdInput.readLine();
            System.out.println("Server sends: " + input.read());
        }
        sock.close();

    }
}
clientsock.java:

import java.net.*;
import java.io.*;

public class serversock  {
     public static void main(String[] args) throws IOException
    {
        ServerSocket sersock = null;

        try 
        {
            sersock = new ServerSocket(10007);
        }

        catch (IOException e)
        {
            System.out.println("Can't listen to port 10007");
            System.exit(1);
        }

        Socket clientSocket = null;
        System.out.println("Waiting for connection....");


        try
        {
            clientSocket = sersock.accept();
        }

        catch ( IOException ie)
        {
            System.out.println("Accept failed..");
            System.exit(1);
        }

        System.out.println("Conncetion is established successfully..");
        System.out.println("Waiting for input from client...");

        PrintWriter output = new PrintWriter(clientSocket.getOutputStream());

        BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine = input.readLine();

        while ( inputLine  != null)
        {
            output.println(inputLine);
            System.out.println("Server: " + inputLine);
            inputLine = input.readLine();
        }
        input.close();
        clientSocket.close();
        sersock.close();

    }
}
import java.util.*;
import java.io.*;
import java.net.*;

public class clientsock 
{
    public static void main(String[] args) throws IOException
    {
        Socket sock = new Socket("localhost",10007);

        // Scanner scan = new Scanner(System.in);
        PrintWriter output = new PrintWriter(sock.getOutputStream(),true);
        BufferedReader input = new BufferedReader( new InputStreamReader(sock.getInputStream()));
        String line = null;

        BufferedReader stdInput = new BufferedReader( new InputStreamReader(System.in));

        System.out.println("Enter data to send to server: ");
        line = stdInput.readLine();
        while ( (line) != "bye")
        {
            output.println(line.getBytes());
            line = stdInput.readLine();
            System.out.println("Server sends: " + input.read());
        }
        sock.close();

    }
}
现在在运行这些程序时,我得到了以下输出: 服务器:

客户:

shahjahan@shahjahan-AOD270:~/Documents/java$ java clientsock
Enter data to send to server: 
qwe
sdf
^Cshahjahan@shahjahan-AOD270:~/Documents/java$
服务器正在接收不同的符号,而客户端什么也没有接收。请帮我解决它

在客户端替换中:

output.println(line.getBytes());


是的,你有很多问题。查找调试器。。。尝试下载netbeans并在循环内设置断点。。。看看到底发生了什么。