Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help with error- UDP

  Asked By: Darcy    Date: Oct 13    Category: Java    Views: 848
  

Iam trying to a develop a UDP chat client-server.
As a first step i have developed the basic outline .
--------------------------------------------------------
//UDP client

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

class UDPClient
{
public static void main (String args[]) throws Exception
{
BufferedReader inFromUser = new BufferedReader(new
InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress= InetAddress.getLocalHost();

byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

String sentence =inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket
(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket
(receiveData,receiveData.length);
clientSocket.receive(receivePacket);

String modifiedSentence = new String(
receivePacket.getData());
System.out.println("FROM SERVER : " +
modifiedSentence);
clientSocket.close();

}
}
----------------------------------------------------------
//UdpServer

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

class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket
(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket
(receiveData,receiveData.length);
serverSocket.receive(receivePacket);

String sentence = new String (receivePacket.getData
());
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();

String CapSentence =sentence.toUpperCase();

sendData = CapSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket
(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket);
}
}
--------------------------------------------------




Iam not able to run this program.Can somebody help me with this?

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Help with error- UDP Or get search suggestion and latest updates.




Tagged: