Assistance with a GUI to send and receive messages/datagrams: no server











up vote
0
down vote

favorite












This is the code to do this through the console, but my mission is to develop a GUI using the methods already provided in my Socket class. This is not a client/server app. I am stuck nowhere near the light with the little knowledge I posses using JFrame implementing ActionListeners to have a decent GUI for this project. I must declare up front through the GUI to what IP address the message is going (socket will be unique at 64000 for outgoing and incoming messages) and display the messages sent and received on different chat windows (frames) for each IP address that connects to me or that I connect to. I know I need a potential JTextField for the IP address where the message is going, another JTextField for the message being sent, a JTextArea for the messages coming in and going out to be displayed, a JButton to send the messages and the title of my window should show the IP Address I am talking to and the socket number being used. It might be so simple for many of you but I would appreciate it a lot if you share a bit of your knowledge as I am only trying to learn and get better.



Socket.java





import java.net.*;
import java.util.*;
import java.util.concurrent.*;





public class Socket {

private InetAddress myAddress;
private int port;
private DatagramSocket socket = null;
private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();


public Socket(int port) {
this.port = port;
try {
this.myAddress = InetAddress.getLocalHost();
this.socket = new DatagramSocket(port, this.myAddress);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}

Thread receiveThread = new Thread(
new Runnable() {
public void run() {
receiveThread();
}
});
receiveThread.setName("Receive Thread For Port = " + this.port);
receiveThread.start();
}


public void receiveThread() {

do {
byte inBuffer = new byte[1024];
for ( int i = 0 ; i < inBuffer.length ; i++ ) {
inBuffer[i] = ' ';
}

DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);

try {
// this is a blocking call
socket.receive(inPacket);
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}

String message = new String(inPacket.getData());
System.out.println("ReceiveThread - Message on port = " + this.port +"n"+
" message = " + message + "n" +
" From IP = " + inPacket.getAddress() + " -"+
" From Port = " + inPacket.getPort()
);
messageQueue.add(inPacket);
} while(true);
}


public DatagramPacket receive() {
return messageQueue.poll();
}


public void send(String s, InetAddress destinationIP, int destinationPort) {

byte outBuffer;

try {
outBuffer = s.getBytes();
DatagramPacket outPacket = new DatagramPacket(outBuffer, s.length());
outPacket.setAddress(destinationIP);
outPacket.setPort(destinationPort);
socket.send(outPacket);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}


//Driver.java



import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

public class Driver {

public static void main(String args) {
Socket socket = new Socket(64000);

InetAddress myAddress = null;

try {
//myAddress = InetAddress.getByName("192.168.1.xxx"); //Router B
myAddress = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}

socket.send("This is Me", myAddress, 64000);
socket.send("I am IP 192.168.1.xxx", myAddress, 64000);


try {
System.out.println("Main is sleeping");
TimeUnit.SECONDS.sleep(2);
System.out.println("Main is awake");
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}

DatagramPacket packet;

do {
packet = socket.receive();
if (packet != null) {
String message = new String(packet.getData());
System.out.println("Message = " + message);
}
} while(packet != null);

System.out.println("End of fun!");
}

}









share|improve this question




























    up vote
    0
    down vote

    favorite












    This is the code to do this through the console, but my mission is to develop a GUI using the methods already provided in my Socket class. This is not a client/server app. I am stuck nowhere near the light with the little knowledge I posses using JFrame implementing ActionListeners to have a decent GUI for this project. I must declare up front through the GUI to what IP address the message is going (socket will be unique at 64000 for outgoing and incoming messages) and display the messages sent and received on different chat windows (frames) for each IP address that connects to me or that I connect to. I know I need a potential JTextField for the IP address where the message is going, another JTextField for the message being sent, a JTextArea for the messages coming in and going out to be displayed, a JButton to send the messages and the title of my window should show the IP Address I am talking to and the socket number being used. It might be so simple for many of you but I would appreciate it a lot if you share a bit of your knowledge as I am only trying to learn and get better.



    Socket.java





    import java.net.*;
    import java.util.*;
    import java.util.concurrent.*;





    public class Socket {

    private InetAddress myAddress;
    private int port;
    private DatagramSocket socket = null;
    private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();


    public Socket(int port) {
    this.port = port;
    try {
    this.myAddress = InetAddress.getLocalHost();
    this.socket = new DatagramSocket(port, this.myAddress);
    } catch (Exception e) {
    e.printStackTrace();
    System.exit(-1);
    }

    Thread receiveThread = new Thread(
    new Runnable() {
    public void run() {
    receiveThread();
    }
    });
    receiveThread.setName("Receive Thread For Port = " + this.port);
    receiveThread.start();
    }


    public void receiveThread() {

    do {
    byte inBuffer = new byte[1024];
    for ( int i = 0 ; i < inBuffer.length ; i++ ) {
    inBuffer[i] = ' ';
    }

    DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);

    try {
    // this is a blocking call
    socket.receive(inPacket);
    } catch(Exception e) {
    e.printStackTrace();
    System.exit(-1);
    }

    String message = new String(inPacket.getData());
    System.out.println("ReceiveThread - Message on port = " + this.port +"n"+
    " message = " + message + "n" +
    " From IP = " + inPacket.getAddress() + " -"+
    " From Port = " + inPacket.getPort()
    );
    messageQueue.add(inPacket);
    } while(true);
    }


    public DatagramPacket receive() {
    return messageQueue.poll();
    }


    public void send(String s, InetAddress destinationIP, int destinationPort) {

    byte outBuffer;

    try {
    outBuffer = s.getBytes();
    DatagramPacket outPacket = new DatagramPacket(outBuffer, s.length());
    outPacket.setAddress(destinationIP);
    outPacket.setPort(destinationPort);
    socket.send(outPacket);
    } catch (Exception e) {
    e.printStackTrace();
    System.exit(-1);
    }
    }
    }


    //Driver.java



    import java.net.DatagramPacket;
    import java.net.InetAddress;
    import java.util.concurrent.TimeUnit;

    public class Driver {

    public static void main(String args) {
    Socket socket = new Socket(64000);

    InetAddress myAddress = null;

    try {
    //myAddress = InetAddress.getByName("192.168.1.xxx"); //Router B
    myAddress = InetAddress.getLocalHost();
    } catch (Exception e) {
    e.printStackTrace();
    System.exit(-1);
    }

    socket.send("This is Me", myAddress, 64000);
    socket.send("I am IP 192.168.1.xxx", myAddress, 64000);


    try {
    System.out.println("Main is sleeping");
    TimeUnit.SECONDS.sleep(2);
    System.out.println("Main is awake");
    } catch (Exception e) {
    e.printStackTrace();
    System.exit(-1);
    }

    DatagramPacket packet;

    do {
    packet = socket.receive();
    if (packet != null) {
    String message = new String(packet.getData());
    System.out.println("Message = " + message);
    }
    } while(packet != null);

    System.out.println("End of fun!");
    }

    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      This is the code to do this through the console, but my mission is to develop a GUI using the methods already provided in my Socket class. This is not a client/server app. I am stuck nowhere near the light with the little knowledge I posses using JFrame implementing ActionListeners to have a decent GUI for this project. I must declare up front through the GUI to what IP address the message is going (socket will be unique at 64000 for outgoing and incoming messages) and display the messages sent and received on different chat windows (frames) for each IP address that connects to me or that I connect to. I know I need a potential JTextField for the IP address where the message is going, another JTextField for the message being sent, a JTextArea for the messages coming in and going out to be displayed, a JButton to send the messages and the title of my window should show the IP Address I am talking to and the socket number being used. It might be so simple for many of you but I would appreciate it a lot if you share a bit of your knowledge as I am only trying to learn and get better.



      Socket.java





      import java.net.*;
      import java.util.*;
      import java.util.concurrent.*;





      public class Socket {

      private InetAddress myAddress;
      private int port;
      private DatagramSocket socket = null;
      private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();


      public Socket(int port) {
      this.port = port;
      try {
      this.myAddress = InetAddress.getLocalHost();
      this.socket = new DatagramSocket(port, this.myAddress);
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      Thread receiveThread = new Thread(
      new Runnable() {
      public void run() {
      receiveThread();
      }
      });
      receiveThread.setName("Receive Thread For Port = " + this.port);
      receiveThread.start();
      }


      public void receiveThread() {

      do {
      byte inBuffer = new byte[1024];
      for ( int i = 0 ; i < inBuffer.length ; i++ ) {
      inBuffer[i] = ' ';
      }

      DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);

      try {
      // this is a blocking call
      socket.receive(inPacket);
      } catch(Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      String message = new String(inPacket.getData());
      System.out.println("ReceiveThread - Message on port = " + this.port +"n"+
      " message = " + message + "n" +
      " From IP = " + inPacket.getAddress() + " -"+
      " From Port = " + inPacket.getPort()
      );
      messageQueue.add(inPacket);
      } while(true);
      }


      public DatagramPacket receive() {
      return messageQueue.poll();
      }


      public void send(String s, InetAddress destinationIP, int destinationPort) {

      byte outBuffer;

      try {
      outBuffer = s.getBytes();
      DatagramPacket outPacket = new DatagramPacket(outBuffer, s.length());
      outPacket.setAddress(destinationIP);
      outPacket.setPort(destinationPort);
      socket.send(outPacket);
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }
      }
      }


      //Driver.java



      import java.net.DatagramPacket;
      import java.net.InetAddress;
      import java.util.concurrent.TimeUnit;

      public class Driver {

      public static void main(String args) {
      Socket socket = new Socket(64000);

      InetAddress myAddress = null;

      try {
      //myAddress = InetAddress.getByName("192.168.1.xxx"); //Router B
      myAddress = InetAddress.getLocalHost();
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      socket.send("This is Me", myAddress, 64000);
      socket.send("I am IP 192.168.1.xxx", myAddress, 64000);


      try {
      System.out.println("Main is sleeping");
      TimeUnit.SECONDS.sleep(2);
      System.out.println("Main is awake");
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      DatagramPacket packet;

      do {
      packet = socket.receive();
      if (packet != null) {
      String message = new String(packet.getData());
      System.out.println("Message = " + message);
      }
      } while(packet != null);

      System.out.println("End of fun!");
      }

      }









      share|improve this question















      This is the code to do this through the console, but my mission is to develop a GUI using the methods already provided in my Socket class. This is not a client/server app. I am stuck nowhere near the light with the little knowledge I posses using JFrame implementing ActionListeners to have a decent GUI for this project. I must declare up front through the GUI to what IP address the message is going (socket will be unique at 64000 for outgoing and incoming messages) and display the messages sent and received on different chat windows (frames) for each IP address that connects to me or that I connect to. I know I need a potential JTextField for the IP address where the message is going, another JTextField for the message being sent, a JTextArea for the messages coming in and going out to be displayed, a JButton to send the messages and the title of my window should show the IP Address I am talking to and the socket number being used. It might be so simple for many of you but I would appreciate it a lot if you share a bit of your knowledge as I am only trying to learn and get better.



      Socket.java





      import java.net.*;
      import java.util.*;
      import java.util.concurrent.*;





      public class Socket {

      private InetAddress myAddress;
      private int port;
      private DatagramSocket socket = null;
      private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();


      public Socket(int port) {
      this.port = port;
      try {
      this.myAddress = InetAddress.getLocalHost();
      this.socket = new DatagramSocket(port, this.myAddress);
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      Thread receiveThread = new Thread(
      new Runnable() {
      public void run() {
      receiveThread();
      }
      });
      receiveThread.setName("Receive Thread For Port = " + this.port);
      receiveThread.start();
      }


      public void receiveThread() {

      do {
      byte inBuffer = new byte[1024];
      for ( int i = 0 ; i < inBuffer.length ; i++ ) {
      inBuffer[i] = ' ';
      }

      DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);

      try {
      // this is a blocking call
      socket.receive(inPacket);
      } catch(Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      String message = new String(inPacket.getData());
      System.out.println("ReceiveThread - Message on port = " + this.port +"n"+
      " message = " + message + "n" +
      " From IP = " + inPacket.getAddress() + " -"+
      " From Port = " + inPacket.getPort()
      );
      messageQueue.add(inPacket);
      } while(true);
      }


      public DatagramPacket receive() {
      return messageQueue.poll();
      }


      public void send(String s, InetAddress destinationIP, int destinationPort) {

      byte outBuffer;

      try {
      outBuffer = s.getBytes();
      DatagramPacket outPacket = new DatagramPacket(outBuffer, s.length());
      outPacket.setAddress(destinationIP);
      outPacket.setPort(destinationPort);
      socket.send(outPacket);
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }
      }
      }


      //Driver.java



      import java.net.DatagramPacket;
      import java.net.InetAddress;
      import java.util.concurrent.TimeUnit;

      public class Driver {

      public static void main(String args) {
      Socket socket = new Socket(64000);

      InetAddress myAddress = null;

      try {
      //myAddress = InetAddress.getByName("192.168.1.xxx"); //Router B
      myAddress = InetAddress.getLocalHost();
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      socket.send("This is Me", myAddress, 64000);
      socket.send("I am IP 192.168.1.xxx", myAddress, 64000);


      try {
      System.out.println("Main is sleeping");
      TimeUnit.SECONDS.sleep(2);
      System.out.println("Main is awake");
      } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
      }

      DatagramPacket packet;

      do {
      packet = socket.receive();
      if (packet != null) {
      String message = new String(packet.getData());
      System.out.println("Message = " + message);
      }
      } while(packet != null);

      System.out.println("End of fun!");
      }

      }






      sockets jframe message-queue datagram inetaddress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 20:11

























      asked Nov 8 at 9:17









      Pildora

      92




      92





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204660%2fassistance-with-a-gui-to-send-and-receive-messages-datagrams-no-server%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204660%2fassistance-with-a-gui-to-send-and-receive-messages-datagrams-no-server%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?