[英]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
方法时都创建DataOutputStream
和DataInputStream
。 Instead create the DataOutputStream
and the DataInputStream
just after you create the socket. 而是在创建套接字后立即创建
DataOutputStream
和DataInputStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.