简体   繁体   English

Java套接字将数据从客户端发送到服务器

[英]Java socket send data from client to server

Why gets NullPointerException ? 为什么会出现NullPointerException When I send data from server to client it all ok. 当我从服务器向客户端发送数据时,一切正常。

Error java.lang.NullPointerException
at NetworkClient.Klient.wyslijDane(Klient.java:35)

EDIT Code I Added complete server class and client class In other class i create a new Object Client and Server 编辑代码我添加了完整的服务器类和客户端类在其他类中,我创建了一个新的对象客户端和服务器

klient=new Klient(); klient = new Klient(); and then call method WyslijDane. 然后调用方法WyslijDane。 Server it's ok, and client throw exception. 服务器正常,客户端抛出异常。

Client Class 客户类别

public class Klient {

  public static final int PORT=50007;
  public static Socket sock;
  public static int msg=10;



public Klient() throws UnknownHostException, IOException
{
    Socket sock=new Socket("localhost", PORT);
}



 public static void wyslijDane(int GraczK) throws IOException
{

      DataInputStream in=new DataInputStream(sock.getInputStream());
       DataOutputStream out=new DataOutputStream(sock.getOutputStream());

      //    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));



          out.writeInt(GraczK);
         //out.writeBoolean(flaga);
          out.flush();
         System.out.println("Wyslano dane "+ GraczK );
}
}

Server Class 服务器等级

 public class Server
  {
   public static final int PORT=50007;
   public static MainWarSever main=new MainWarSever();
   static Socket sock;   
             public Server() throws IOException
 {
     //tworzenie gniazda serwerowego                                        
     ServerSocket serv = null;                                                     
     if (serv==null)
     serv=new ServerSocket(PORT);
     else 
     {
         System.out.println("SErver już zajety brrrr");
         serv.close();
     }




                                                                    sock=serv.accept();                                                    
    System.out.println("Jest polaczenie: "+sock);   

 }


 public static void wyslijDane(int GraczK) throws IOException
 {
      DataInputStream in=new DataInputStream(sock.getInputStream());
      DataOutputStream out=new DataOutputStream(sock.getOutputStream());

        boolean flaga=true;
        int msg;
        int i=0;
        int msgOut;
         msgOut=GraczK;
         out.writeInt(msgOut);
         out.writeBoolean(flaga);
         out.flush();
         System.out.println("Wyslano dane "+ GraczK + "Flage" + flaga);
}

What is sock set to? sock要做什么? If sock is not the reason for the NullPointerException, code with line numbers would be helpful. 如果sock不是NullPointerException的原因,则行号代码将很有帮助。

EDIT: After you've added your complete source code ... when calling static wyslijDane(int GraczK) your Klient() constructor setting sock must not have been called. 编辑:添加完完整的源代码后,在调用静态wyslijDane(int GraczK)必须未调用Klient()构造函数设置袜子。 Making your variables and methods not-static would clarify this. 使变量和方法不是静态的可以澄清这一点。

EDIT: BTW in your client class you define two sock variables, one static and one in the constructor, the latter is destroyed after leaving the constructor. 编辑:顺便说一句,在您的客户端类中,您定义了两个袜子变量,一个静态变量,一个在构造函数中,后者在离开构造函数后被销毁。

1) Your are getting the NullPointerException because when you say sock.getOutputStream() or sock.getInputStream() you're using the sock variable declared outside the constructor wich is not initialized. 1)您正在获取NullPointerException因为当您说sock.getOutputStream()sock.getInputStream()您使用的是未初始化在构造函数外部声明的sock变量。

So instead of creating a new sock variable in the constructor use the one you already have: 因此, sock在构造函数中创建一个新的sock变量, sock使用已有的变量:

//Change this line
Socket sock=new Socket("localhost", PORT);

//To this
Socket sock=new Socket("localhost", PORT);

2) Your sock variable shouldn't be declared as static and the wyslijDane should be called after the constructor is executed (the socket created) so it shouldn't be static. 2)不应将您的sock变量声明为static变量,并且应在构造函数执行后(创建套接字)调用wyslijDane因此它不应为static变量。

3) Make sure you call the wyslijDane method after you create the Socket . 3)确保在创建Socket之后调用wyslijDane方法。

4) First create and flush the DataOutputStream and then create the DataInputStream . 4)首先创建并刷新DataOutputStream ,然后创建DataInputStream They should be in the same order both in the client and in the server code: 它们在客户端和服务器代码中的顺序应相同:

DataOutputStream out=new DataOutputStream(sock.getOutputStream());
out.flush();
DataInputStream in=new DataInputStream(sock.getInputStream());

Since you said the exception is thrown when you create the DataOutputStream the problem should be solved. 由于您说的是在创建DataOutputStream时引发了异常,因此应该解决该问题。


Suggestion: 建议:

As a good practice, don't create the DataOutputStream and the DataInputStream everytime you call the wyslijDane method. 好的做法是,不要在每次调用wyslijDane方法时都创建DataOutputStreamDataInputStream Instead create the DataOutputStream and the DataInputStream just after you create the socket. 而是在创建套接字后立即创建DataOutputStreamDataInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将数据从Java Socket Server发送到NodeJS客户端 - Can't send data from Java Socket Server to NodeJS client 将数据从Java Socket Server发送到Python客户端 - Send data from Java Socket Server to Python client 为什么使用Java套接字创建的服务器不打印从客户端发送的数据(如果客户端终止,则显示该消息)? - Why server created using java socket doesn't not print data send from client (it dislays if client terminate)? 从服务器套接字java中的客户端获取数据 - get data from client in server socket java 使用Java中的套接字编程将数据流从客户端程序(在VM中运行)发送到服务器程序(在Host OS上) - Send stream of Data from client program (running in VM) to server program(on Host OS) using socket programming in Java 无法从Java套接字服务器接收数据到C套接字客户端 - Trouble receiving data to C socket client from Java socket server Java Socket服务器未从客户端套接字接收数据 - Java Socket server not recieving data from client socket 使用套接字将服务器JAVA的图像发送到android客户端 - send image from server JAVA to android client using socket 通过 Socket Outputstream 从 Swift 客户端向 Java 服务器发送消息 - Send message through Socket Outputstream from Swift Client to Java Server Java将数据从服务器发送回客户端 - Java Send Data from server back to client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM