简体   繁体   中英

UDP client and server connection

I am currently writing two java classes (client and server). The client takes an input number form keyboard and sends it to the server. The server then multiplies this number by two and sends it back to the client. The numbers should also be printed to screen along the way, for example if I input the number 3 I should get

"From Client: 3" "From Server: 6"

They should continuously do this unless a negative number is received by the client, say for example the number -3 is sent to the server and it returns -6.

The code I have for the two classes so far is:

 import java.io.*;
 import java.net.*;
 import java.nio.ByteBuffer;
 import java.util.Scanner;

 class Client {
   public static void main(String args[]) throws Exception 
  {  
    DatagramSocket clientSocket = new DatagramSocket();

    System.out.println("Insert number: ");  
    Scanner s= new Scanner(System.in);
    int num = s.nextInt();

    byte[] sendData = ByteBuffer.allocate(4).putInt(num).array();
    InetAddress IPAddress = InetAddress.getByName("localhost");
    int port = 1999;
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length , IPAddress ,1999);
    clientSocket.send(sendPacket);

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

    byte[] receiveData = new byte[4];
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    clientSocket.receive(receivePacket);

    int numberReceived = ByteBuffer.wrap(receivePacket.getData()).getInt();
    System.out.println("FROM SERVER:" + numberReceived);
    clientSocket.close();
   }
}

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;


class Server {
  public static void main(String args[]) throws Exception
  {
    DatagramSocket serverSocket = new DatagramSocket(1999);
  while(true)
  {
        byte[] receiveData = new byte[4];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        int num = ByteBuffer.wrap(receivePacket.getData()).getInt();
        System.out.println("FROM Client:" + num);

        InetAddress IPAddress = receivePacket.getAddress();
        int port = receivePacket.getPort();

        int numtosend = num * 2;
        byte[] sendData = ByteBuffer.allocate(4).putInt(numtosend).array();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,port);
        serverSocket.send(sendPacket);      
    }
  }
}

Currently, when I run the program all I get is an output of the number first entered.

Can anyone tell me where I am going wrong?

Many thanks in advance :)

Where you are going wrong is that you haven't finished the program yet.

Your client only reads one number, sends one number, receives one reply.

You need a loop.

You also need to implement the part about stopping on a negative number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM