【问题标题】:errors in getting & sending Data (String ,Int ) in socket java在套接字 java 中获取和发送数据(String,Int)时出错
【发布时间】:2017-07-02 15:13:10
【问题描述】:

大家好,我有一个项目可以从客户端获得一些Stringint,我想将它们发送到我的服务器。但是我在获取这个时遇到了问题。我什么也没得到或错误的东西。请帮帮我。

客户:

//build a socket.
public void connectclient() throws IOException
{
    socket = new Socket("localhost", 9097);
    System.out.println("connect to server on port 9097");
}
public void startstreams() throws IOException , ClassNotFoundException      
{
in = socket.getInputStream();
out = socket.getOutputStream();
dos = new DataOutputStream(out);
dis = new DataInputStream(in);   
writer = new OutputStreamWriter(out);
reader = new InputStreamReader(in);
breader = new BufferedReader(reader);
bwriter = new BufferedWriter (writer);
w = new PrintWriter(out, true);
}
public void writeSocketMyJson(String n) throws IOException
{
   w = new PrintWriter(out, true);
   w.println(n);
   w.flush();
   w.close();
}

massage1 = "username";
//send username that get from login form to server.
public void sendusername() throws IOException
{
  writeSocketMyJson(massage1);
}

massage2 ="admin";
//send password that get from login form to server.
public void sendpassword() throws IOException
{
  writeSocketMyJson(massage2);
}

//send access level to server.
public void sendaccess(int l) throws IOException 
{
    dos.writeInt(l);
    dos.flush();
}
sendaccess(21);

服务器:

//build server.
public void connectserver() throws IOException
{
    listener = new ServerSocket(9097);
    System.out.println("Server is running on port 9097 ...");
}
//wait for new connection.
public void waitforclient() throws IOException
{
    socket = listener.accept();
System.out.println("A new client connected to the server");
}
public void startstreams() throws IOException
{
in = socket.getInputStream();
out = socket.getOutputStream(); 
dos = new DataOutputStream(out);
dis = new DataInputStream(in);    
writer = new OutputStreamWriter(out);
reader = new InputStreamReader(in);
bwriter = new BufferedWriter (writer);
breader = new BufferedReader (reader);
}
public String readSocket() throws IOException
{
        breader = new BufferedReader(reader);
        while (true)
        {   
            massage = new String();
            massage = breader.readLine();
            if (massage.equals(null) == false) 
            {
                break;
            }
        }
        return(massage);
}

//get username that client send it.
public String getusername() throws IOException, ClassNotFoundException
{ 
    try  
    {
       username = new String();
       username = readSocket();
       System.out.println("the username is : " + username);
    }
    catch(IOException IOE)
    {
            IOE.printStackTrace();//if there is an error, print it out
    } 
    return(username);
}

//get password that client send it.
public String getpassword() throws IOException, ClassNotFoundException
{ 
    try  
    {
        password = new String();
        password = readSocket();
        System.out.println("the password is : " + password);
    }
    catch(IOException IOE)
    {
            IOE.printStackTrace();//if there is an error, print it out
    }  
    return(password);
}

//get commend from client.
//admin or user send which commend 21-add new information 22-show information    
public int getaccess() throws IOException
{   
    System.out.println("server get access : " + dis.readInt());
    return(dis.readInt());
}

但是当我打电话给getpassword()我什么都得不到。 当我打电话给getaccess()我什么也得不到。 为什么?请帮我。 我也有控制订单的主类

主要:

    //build Server & Client
    Server server = new Server();
    Client client = new Client();

    //Start Server & Client
    server.connectserver();
    client.connectclient();

    //Server wait for new connection
    server.waitforclient();

    //start the Streams
    server.startstreams();
    client.startstreams();

    client.sendusername();
    String msg1 =server.readSocket();

    client.sendpassword();
    String msg2 =server.readSocket();

    client.sendaccess();
    int n = getaccess();

【问题讨论】:

    标签: java sockets io


    【解决方案1】:

    您的代码充满了废话。你做的事情像

    breader = new BufferedReader (reader);
    
    public String readSocket() throws IOException
    {
        breader = new BufferedReader(reader);
    

    喜欢

    w = new PrintWriter(out, true);
    
    public void writeSocketMyJson(String n) throws IOException
    {
       w = new PrintWriter(out, true);
    

    喜欢

        password = new String();
        password = readSocket();
    

    我建议你把它全部扔掉,从头开始,用一些 java 套接字的示例代码,interwebz 上有很多。

    要记住的是,你不能仅仅假装一个套接字是一个普通的流,你不能只是在它上面创建一个BufferedReader。在任何给定时刻,您都需要确切地知道发送了多少字节,因此需要确切地知道要接收多少字节。

    所以一般来说,在发送端你不能写一个字符串并且期望在接收端用一个行尾来检测字符串的结尾。在发送端,您需要首先将字符串的长度写为 32 位 int,因此在接收端,您知道您期望的正好是 4 个字节,其中包含后面的字符串的长度。然后,您可以读取构成字符串的正确字节数。当然,不要忘记在java中,一个字符是2个字节长的。

    【讨论】:

    • 感谢您的回答。我也有主类来控制他们的顺序,即我现在发送并获取哪种数据。你觉得够不够?
    猜你喜欢
    • 2013-09-07
    • 2023-03-19
    • 1970-01-01
    • 2016-04-28
    • 2023-04-04
    • 2011-08-01
    • 2015-07-24
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多