| |

VerySource

 Forgot password?
 Register
Search
View: 915|Reply: 4

Is the program itself buggy or is there a bug in the java statement or method?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-31 17:00:01
| Show all posts |Read mode
Recently read a book, which contains an example on network programming, as follows:

1. Server-side DatagramServer1:

import java.io. *;
import java.net. *;
import java.util. *;
/ **
 * <p> Title: </ p>
 *
 * <p> Description: </ p>
 *
 * <p> Copyright: Copyright (c) 2017 </ p>
 *
 * <p> Company: </ p>
 *
 * @author not attributable
 * @version 1.0
 * /
public class DatagramServer1 extends Thread {
    DatagramSocket socket;
    DatagramPacket packet;
    int client;
    public DatagramServer1 (DatagramSocket socket, DatagramPacket packet, int client) {
        this.socket = socket;
        this.packet = packet;
        this.client = client;
        this.start ();
    }
    public void run () {
        try {
            byte [] received = packet.getData ();
            System.out.println ("Received from clinet =" + client + "" + new String (received));
            String response = "You are client" + client;
            byte [] buf = received;
            InetAddress address = packet.getAddress ();
            int port = packet.getPort ();
            packet = new DatagramPacket (buf, buf.length, address, port);
            socket.send (packet);
        } catch (IOException ex) {
            ex.printStackTrace ();
        }
    }
    public static void main (String [] args) throws Exception {
        DatagramSocket socket = new DatagramSocket (4444);
        System.out.println ("Server Started");
        byte [] buf = new byte [200];
        int i = 1;
        try {
            while (true) {
              DatagramPacket packet = new DatagramPacket (buf, buf.length);
              System.out.println ("Waiting for client =" + i);
              socket.receive (packet);
              new DatagramServer1 (socket, packet, i);
              i ++;
            }
        } catch (Exception ex) {
            ex.printStackTrace ();
        } finally {
            socket.close ();
        }
    }
}

Client


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

/ **
 * <p> Title: </ p>
 *
 * <p> Description: </ p>
 *
 * <p> Copyright: Copyright (c) 2017 </ p>
 *
 * <p> Company: </ p>
 *
 * @author not attributable
 * @version 1.0
 * /
public class DatagramClient {
    public DatagramClient () {
    }
    public static void main (String [] args) throws IOException {
        if (args.length! = 2) {
            System.out.println ("Usage:" + "java DatagramClient <hostname> <message>");
            return;
        }
        DatagramSocket socket = new DatagramSocket ();
        System.out.println ("My message =" + args [1]);
        byte [] buf = new byte [200];
        buf = args [1] .getBytes ();
        InetAddress address = InetAddress.getByName (args [0]);
        DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4444);
        socket.send (packet);
        packet = new DatagramPacket (buf, buf.length);
        socket.receive (packet);
        String received = new String (packet.getData ());
        System.out.println ("The server returns my message:" + received);
        socket.close ();
    }
}

The problem is:
For example: java DatagramClient hostName abc (hostName is the host name)
      The client output 3 characters abc to the server for the first time, both the server and the client display normally
      Second time: java DatagramClient hostName 12, the client is sincere, but the server shows 12c

      The server program does not seem to clear the tone! !! ?
      Related statement: byte [] received = packet.getData ();
            System.out.println ("Received from clinet =" + client + "" + new String (received));

As the question, I am very confused about this problem soon after I started, please ask prawn ~ Thank you! !!
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-31 13:15:02
| Show all posts
Should be a server-side issue, buf should be emptied after sending
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-22 05:30:01
| Show all posts
It should have nothing to do with buf? The error display is new String (received). Here, every new DatagramServer1 (new DatagramServer1 (socket, packet, i);) means that there is a local variable received in it. The default value should be empty?
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-7-24 10:15:01
| Show all posts
In the main method on the Server side, set byte[] buf=new byte[200]
Move down to while(true) and it should be no problem
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-8-5 10:15:01
| Show all posts
Thank you!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list