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!");
}
}
sockets jframe message-queue datagram inetaddress
add a comment |
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!");
}
}
sockets jframe message-queue datagram inetaddress
add a comment |
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!");
}
}
sockets jframe message-queue datagram inetaddress
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
sockets jframe message-queue datagram inetaddress
edited Nov 8 at 20:11
asked Nov 8 at 9:17
Pildora
92
92
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password