I am trying to write a simple program about UDP Connections to learn about them. I have implemented some basic things but when I try to send and get back what I sent but I face some problems like,
When I do this ;
send a string
"asd" to server I get back asdxxxxxxxxxx
and when I try to print What I get in the server I get [B#5f186fab
How can I solve this problem ?
To be more clear I am sending you a few lines of code ,
In client;
Scanner in = new Scanner(System.in);
String result = in.nextLine();
// send request
byte[] buf = new byte[1000];
String read = result;
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(result.getBytes(), result.getBytes().length, address, 4445);
socket.send(packet);
// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// display response
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
In server ;
byte[] buf = new byte[1000];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
byte[] received = packet.getData();
System.out.println(received.toString());
// figure out response
// send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(received, received.length, address, port);
socket.send(packet);
Thank you all
EDIT 1 I think I have problems with my buffer but I dont know how to solve .
You can use
System.out.println(Arrays.toString(received));
but what you probably want is
System.out.println(new String(received, o, lengthRead, "UTF-8"));
Have you fixed this?
Otherwise, what I've found is that if you declare a receiving byte[] buf with a capacity that's greater than the length string you're actually receiving, you'll end up with the rest of the buffer full of unwanted bytes.
Eg. if you declare byte[] received = new byte[1000]; but only receive a string of 4 bytes, you'll end up with 996 unwanted bytes.
One quick way around this is to do something like
byte[] received = packet.getData();
System.out.println(received.toString().trim());
trim() did the trick for me. Hope that helps you!
Related
I am trying to create a a client and server chat program using UDP. I have followed a tutorial making a similar program using TCP and tried to then translate my knowledge over to make one in similar fashion using UDP.
I have completed a client and server side with both showing no errors and will run, but once running neither will message the other or receive messages... can someone help me see what i'm doing wrong?
Server side for sending messages:
try{
//creates the packet to be sent
byte[] buf = new byte[256];
String msgout = serverText.getText().trim();
buf = msgout.getBytes();
//uses the socet.receive method to get the packet to retrieve information to send
DatagramPacket packet = new DatagramPacket(buf, buf.length);
ss.receive(packet);
InetAddress address = packet.getAddress();
int port = packet.getPort();
//uses packet information to create and send packet
DatagramPacket packetSend = new DatagramPacket(buf, buf.length, address, port);
ss.send(packetSend);
//Displays the message in the chat area and clears the text area
serverArea.setText(serverArea.getText().trim()+"\n Server: "+msgout);
serverText.setText("");
}catch (Exception e){
}
and then the main for setting the socket and receiving/printing:
String msgin = "";
try{
ss = new DatagramSocket(1420); // Sets socket at 1420
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
ss.receive(packet); //Receives the packet from the socket
//Converts the byte array into a string
String clientMsg = new String(packet.getData(), 0, packet.getLength());
while(!msgin.equals("exit")){
//Displays the message
msgin = clientMsg;
serverArea.setText(serverArea.getText().trim()+"\n Client: "+msgin); //displays client message
}
}catch(Exception e){
}
Here is the client side code, ill combine its send and receive areas into one block:
try{
//Creates the message out using the known socket that the Server creates and the known local address
String msgout = clientText.getText().trim();
sendBuf = msgout.getBytes();
InetAddress address = InetAddress.getLocalHost();
DatagramPacket sp = new DatagramPacket(sendBuf, sendBuf.length, address, 1420);
s.send(sp);
//Displays the text and clears the text field
clientchat.setText(clientchat.getText().trim()+"\n Server: "+msgout);
clientText.setText("");
}catch (Exception e){
}
String msgin = "";
try{
//Creates a socket
DatagramSocket s = new DatagramSocket();
//Receives the message from the server
byte[] buf = new byte[256];
DatagramPacket rp = new DatagramPacket(buf, buf.length);
s.receive(rp);
//Converts byte array to message
String clientMsg = new String(rp.getData(), 0, rp.getLength());
while(!msgin.equals("exit")){
//Displays the message
msgin = clientMsg;
clientchat.setText(clientchat.getText().trim()+"\n Server: "+msgin); //displays client message
}
}catch (Exception e){
}
Any help and tips will be greatly appreciated!
If nothing is happening, and you are unable to send/receive messages, it is likely that there are exceptions that are being generated.
However, since you have a try-catch block that catches all exceptions, and then simply does nothing, you will have no idea what exceptions are thrown, if any are thrown at all.
Rather than simply ignoring exceptions, you should at least be printing their cause.
In your catch statements, add the following and you will be able to more easily debug.
e.printStackTrace();
I know TCP is better to send file but I have a homework about sending file via udp protocol . Is there any code example in C# or Java about sending file?
I have server-client example to send and recieve message. I tried to send the file using the same way but could not succeed. I may need an algorithm to divide the file small parts and send them via datagram, and I have an idea to put "md5" of the part as header of the array to check if the packet is lost or not.
Here is my try , my server side in java;
// 1. creating a server socket, parameter is local port number
sock = new DatagramSocket(7777);
// buffer to receive incoming data
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
byte []bigByteArray=new byte[1024*1024*1024*1024];
// 2. Wait for an incoming data
echo("Server socket created. Waiting for incoming data...");
ByteBuffer target = ByteBuffer.wrap(bigByteArray);
// communication loop
while(true)
{
try
{
sock.receive(incoming);
String s = new String(incoming.getData());
if(s=="finish") break;
target.put(incoming.getData());
}
catch(Exception e)
{
}
}
fos.write(bigByteArray);
fos.close();echo("RECIEVED");
and my client side;
String s;
Path path=Paths.get("C:\\Users\\Toshiba\\Desktop\\aa.txt");
byte[] data = Files.readAllBytes(path);
try
{
sock = new DatagramSocket();
InetAddress host = InetAddress.getByName("localhost");
//take input and send the packet
byte [] part;
for (int i = -1; i < data.length; i=i+100)
{
if(sock.isConnected())
{
part=Arrays.copyOfRange(data,i+1,i+100 );
}
else i=i-100;
}
byte [] f="finish".getBytes();
DatagramPacket finalpac = new DatagramPacket(f ,f.length , host , port);
sock.send(finalpac);
}
Thank you in advance.
Several issues:
The following isn't correct:
sock.receive(incoming);
String s = new String(incoming.getData());
The final line should be
String s = new String(incoming.getData(), incoming.getOffset(), incoming.getLength());
and if you aren't receiving text you shouldn't be converting the data to a String at all.
Remove the sock.isConnected() test. DatagramSockets are not usually connected, and you certainly haven't connected this one.
The loop in which this is embedded does nothing useful. You are only sending the word "finish".
I'm starting to feel real stupid - and hopefully this question's got a simple answer.
I'm trying to send the coordinates of a Point object over UDP. The sending works great:
public void send(Point p) throws IOException {
String data = Integer.toString(p.x) + " " + Integer.toString(p.y);
InetAddress IPAddress = InetAddress.getByName(this.remoteHost);
byte[] sendData = new byte[1024];
sendData = data.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, this.remotePort);
socket.send(sendPacket);
}
And I can receive the data on the other end:
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
this.socket.receive(receivePacket);
As you might see I'm sending the String "X Y", for example "329 456". I now need to parse those values to integers, so I can use them on the other end:
String[] parts = data.split(" ");
int x = Integer.parseInt(new String(parts[0]));
int y = Integer.parseInt(new String(parts[1]));
But this gives me a NumberFormatException on the y integer ("For input string: '456'"). Why? Is there anything I'm missing here? I've been thinking about he actual encoding of the characters send - could that be the reason why Integer won't understand the value?
Thank you for any help.
I guess you don't take packet length into account when converting packet data to String.
You should do it as follows:
String data = new String(receivePacket.getData(), 0, receivePacket.getLength());
Also, it would be better to specify character encoding explicitly when sending and receiving the message, to prevent issues when machines have different default encodings:
sendData = data.getBytes("UTF-8");
...
String data = new String(receivePacket.getData(), 0, receivePacket.getLength(), "UTF-8");
Are you reading data in this way?
String data = new String(receivePacket.getData(), 0, receivePacket.getLength());
I'm trying to send a long over a multicast.
The connection should work, because it's possible to send a String.
This is my serverside code:
currentServerStatusId = Server.getServerStatusVersionId();
buf = ByteBuffer.allocate(8).putLong(currentServerStatusId).array(); //long should be 8 bytes in Java
InetAddress group = InetAddress.getByName(multicastAddress);
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
socket.send(packet);
and this is on the client side (the receiver):
byte[] buf = new byte[256];
serverIpPacket = new DatagramPacket(buf, buf.length);
System.out.println("waiting to receive");
multicastSocket.receive(serverIpPacket);
receivedIp = serverIpPacket.getAddress().getHostAddress();
currentServerStatusId = ByteBuffer.allocate(8).put(serverIpPacket.getData()).getLong();
//new String(serverIpPacket.getData(), 0, serverIpPacket.getLength());
System.out.println("received current ServerStatusId: " + currentServerStatusId);
This gives me a BufferUnderflowException.
Apparently it does work when I double the size from 8 to 16 in the allocate method on the receiver/client side.
But then it returns 0 instead of my testing value (something like 68763)
Okay, sorry, for the trouble but I just found the answer myself:
I have to put this in the client side:
ByteBuffer buffer = ByteBuffer.wrap(serverIpPacket.getData());
currentServerStatusId = buffer.getLong();
that's all
Hey everyone, I'm having a bit of a problem with UDP and Datagrams. I'm supposed to make a server that will get a request from the client to send a file in the same directory. The UDP Server will then get this file (a video), put it into a datagram and send it. I think I know how to do it, but I can't put the file in the datagram. I'm putting it in Binary form, so keep that in mind.
Here's my code so far:
edit: This is the server by the way, and I keep having trouble with BufferedInputReader and OutputReader, so keep that in mind :)
Scanner inFromUser = new Scanner(System.in);
int port = 12345;
DatagramSocket server = new DatagramSocket(port);
// Read name of file supplied by client (must be a line of text):
Scanner in = new Scanner(new DataInputStream(server.getInputStream()));
String filename = in.nextLine();
DatagramSocket request = server.accept();
// Create buffer, then we're ready to go:
// Puts file into binary form
BufferedInputStream inbinary =
new BufferedInputStream(new FileInputStream("poop.txt"));
// Outputs the binary form
BufferedOutputStream outbinary =
new BufferedOutputStream(request.getOutputStream());
int numbytes;
int countblocks = 0;
int countbytes = 0;
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length, port);
server.receive(packet);
while ((numbytes = inbinary.read(buf,0,1024)) >= 0)
{
// receive packet from client, telling it to send the video file
server.receive(packet);
InetAddress address = packet.getAddress();
packet = new DatagramPacket(buf, buf.length, address, port);
server.send(packet);
countblocks++; // keep statistics on file size
countbytes += numbytes;
outbinary.write(buf,0,numbytes); // write buffer to socket
}
outbinary.flush(); // FLUSH THE BUFFER
server.close(); // done with the socket
System.out.println(countblocks + " were read; " + countbytes + " bytes");
}
}
I haven't done datagrams in a while, but I'm pretty sure the accept() call is wrong. That's for TCP servers.
I'd recommend cribbing from Sun's excellent tutorial: http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html