i'm a beginner for coding at java . net
now i got a problem with input the port

plz see the code

package test;

import java.net.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPCliet {

	final int buffSize = 10001;//buff size
        final int TIMEOUT = 3000;
        final int MAXTIRES = 1;
        boolean receivedResponse = false;
	DatagramSocket sock;
	String request;
	String response;
	InetAddress servAddr;
	int servPort;
UDPCliet(DatagramSocket s, String sName,int sPort) throws UnknownHostException
		sock = s;
		servAddr = InetAddress.getByName(sName);

void makeRequest(){
	request = "Hello ";//code
void sendRequest() throws IOException
		byte[] sendBuff = new byte [buffSize];
		sendBuff = request.getBytes();
		DatagramPacket sendPacket = new DatagramPacket(sendBuff,sendBuff.length,servAddr,servPort);
	catch (SocketException ex)
		System.err.println("Exception in getRequest");
	void getResponse()  throws IOException{
			byte[] recvBuff = new byte[buffSize];
			DatagramPacket recvPacket = new DatagramPacket (recvBuff,buffSize);
			recvBuff = recvPacket.getData();
			response = new String(recvBuff,0,recvBuff.length);
		catch (SocketException ex)
			System.err.println("Response Exception");
	void useResponse(){
		System.out.println(response);//code for response
	void close ()


//	public static void main(String args []) throws IOException
//	{
  //     final int servPort = 10001;//add port
//	final String servName = "" ; //add servername
//	DatagramSocket sock = new DatagramSocket ();
//	UDPCliet client = new UDPCliet (sock, servName,servPort);
//	client.makeRequest();
//	client.sendRequest();
//	client.getResponse();
//	client.useResponse();
//	client.close();

As you can see if i set
int servPort = 10001;//add port
String servName = "" ; //add server name

it will run ok

but if i change to this statement
servPort = input.nextInt();
servName = input.nextLine();
seems like i can't set the port value and server name by using Scanner keyboard

Forgive me if i use really bad english

5 Years
Discussion Span
Last Post by enterpise

1. Print out both those values after they are input so you can see exactly what values you are trying to use.
2. In your catch blocks don't just print "there was a problem", use ex.printStackTrace(); to see all the info related to the problem.

ps: The server name may surprise you when you print it ;-)

Edited by JamesCherrill: ps


Thx you sir but if i really want to input for the port
Do I need to redo the whole class or anything i can do plz help me:)


Did you do what I said?
You have a very simple bug in your user input code. You need to add one very short method call and it will work.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.