java-udp programming -sending message from server to the client

I made connection between server and client properly and i send message from client to the server,but how can i send messages from server to client.I mean how can i make server act like a client too.i tried to copy the client methods to the another class that server can invoke.but i couldnt then i tried to create a new package to use the client code in server class.any advices?

ps:Sorry about my english.

public class Entrance_Server extends JFrame{

JButton buton = new JButton("Create");
JButton buton2 = new JButton("Join");
JPanel butonpanel = new JPanel();
DatagramSocket sockServer = null;
DatagramSocket sockClient = null;
int port = 7777;
String s;
BufferedReader cin = new BufferedReader(new InputStreamReader(;

public Entrance_Server() {

    setLayout(new GridLayout(2,1));


    buton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            Choosing c = new Choosing();

            System.out.println("Server socket created. Waiting for incoming data...");


    buton2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Choosing c = new Choosing();



public static void main(String[] args){

    Entrance_Server e = new Entrance_Server();



public void connect (){

                sockServer = new DatagramSocket(7777);
                byte[] buffer = new byte[65536];
                DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

            byte[] data = incoming.getData();
            String s = new String(data, 0, incoming.getLength());

            //echo the details of incoming data - client ip : client port - client message
            System.out.println(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " - " + s);

            s = "OK : " + s;
            DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());

            Entrance_Client_in_Server ec = new Entrance_Client_in_Server();

            }catch(IOException i){
                System.err.println("IOException " + i);




On your client u need to wait on the server response by using socket.Receive()

You can identify a client after he has send a packet to the server like you are doing. You can then indentify the client like this: InetAddress address = packet.getAddress(); int port = packet.getPort();

And use it to send a packet back to the client, which will read the response using the socket.Receive();

For further information about Client/Server connection using UDP DatagramSockets check Client-Server Datagram sockets

Need Your Help

maven - Failed to execute cargo:start

java maven servlets deployment cargo

I'm trying to configure cargo automatic deployment in my servlet project and I have these in pom.xml file:

Syntax highlighting HTML with links

html syntax-highlighting highlighting pygments codehighlighter

Is there a tool available which would convert the sources given into HTML with links?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.